如何实现从右到左的动画来启动活动

时间:2014-10-17 18:13:17

标签: android animation android-activity

我正在开发一个演示应用程序,我想在应用程序启动任何activity时应用动画。我在下面编写了代码,但这是为了从左到右动画活动。

left_to_right.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate android:fromXDelta="-100%" android:toXDelta="0%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="500"/>
</set>

right_to_left.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:duration="500"
        android:fromXDelta="0%"
        android:fromYDelta="0%"
        android:toXDelta="100%"
        android:toYDelta="0%" />
</set>

我在这里开始activity这样的

startActivity(new Intent(this, LoginActivity.class));
overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);

我想从右到左实现动画。如何做到这一点。

提前致谢。

4 个答案:

答案 0 :(得分:114)

对动画文件进行以下修改:

enter.xml:

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:duration="500"
        android:fromXDelta="100%"
        android:fromYDelta="0%"
        android:toXDelta="0%"
        android:toYDelta="0%" />
</set>

exit.xml:

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:duration="500"
        android:fromXDelta="0%"
        android:fromYDelta="0%"
        android:toXDelta="-100%"
        android:toYDelta="0%" />
</set>

您的第二项活动将从右向左滑动。

为了更好地理解如何使用动画的fromXDelta和toXDelta值,这里是值的一个非常基本的例子: Activity transition values on X axis

通过这种方式,您可以轻松了解添加android的原因:fromXDelta =&#34; 0%&#34;和android:toXDelta =&#34; -100%&#34;对于您当前的活动。这是因为你希望它从0%变为-100%的位置。

<强> [编辑]

因此,如果您想从ActivityA打开ActivityB,请执行以下操作(假设您有一个按钮):

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(ActivityA.this, ActivityB.class));
            overridePendingTransition(R.anim.enter, R.anim.exit);
        }
    });

现在,如果你想让#34;倒退&#34;第一个动画,当你离开活动B时,你需要2个新的动画文件和ActivityB的onBackPressed方法中的一些代码,如下所示:

首先是动画文件: left_to_right.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:duration="500"
        android:fromXDelta="-100%"
        android:fromYDelta="0%"
        android:toXDelta="0%"
        android:toYDelta="0%" />
</set>

right_to_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:duration="500"
        android:fromXDelta="0%"
        android:fromYDelta="0%"
        android:toXDelta="100%"
        android:toYDelta="0%" />
</set>

在ActivityB中执行以下操作:

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
}

此外,如果您启用了导航,那么您也必须在这种情况下添加动画:

您可以像这样启用向上导航:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().setDisplayHomeAsUpEnabled(true);
}

这也是你在这种情况下处理动画的方式:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
       //NavUtils.navigateUpFromSameTask(this);
       finish();
       overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
       return true;
    }
    return super.onOptionsItemSelected(item);
}

另请注意,即使您的代码没问题,您的手机也可能已关闭动画。然后再打开,执行以下操作:

  1. 打开“设置”并转到“开发者选项”
  2. 确保已启用(通过滑动右上角的切换按钮)
  3. 向下滚动并在“绘图”下,逐个点按这些选项:Windows动画比例,过渡动画比例和动画制作持续时间比例
  4. 选择&#34;动画比例1x&#34;
  5. 这有帮助吗?

答案 1 :(得分:7)

应该在&#34; target&#34;中调用{p> overridePendingTransition活动。例如:来自活动A - &gt; B,您可以将overridePendingTransition来电置于活动B的onCreate

请注意,如果用户在系统级别禁用了动画,则无法强制显示动画。

编辑:

示例如下:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    overridePendingTransition(R.anim.enter, R.anim.exit);
}

答案 2 :(得分:1)

试试这段代码,它对我有用

从右向左滑动

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >
    <translate
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:fromXDelta="0%"
        android:toXDelta="-50%" >
    </translate>
</set>

从左向右滑动

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="-50%"
    android:toXDelta="0%" >
</translate>

答案 3 :(得分:1)

这对我来说是完美的代码 Slideinleft

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="100%p"
    android:toXDelta="0"
    android:duration="800"/>

Slideinright

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0"
    android:toXDelta="-100%p"
    android:duration="800"/>

活动

Intent intent = new Intent(getApplicationContext(),termcondionactivity.class);
Bundle bndlAnimation = ActivityOptions.makeCustomAnimation(getApplicationContext(), R.anim.slideinleft, R.anim.slideinright).toBundle();

startActivity(intent, bndlAnimation);
相关问题