如何实现从右到左的动画来开始活动 [英] How to achieve right to left animation to start the activity

查看:165
本文介绍了如何实现从右到左的动画来开始活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on one demo application where I want to apply animation whenever app start any activity. I wrote below code but this is for to animate the activity from left to right.

left_to_right.xml

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

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>

我在这里开始活动

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

我想从右到左实现动画。

I want to achieve an animation right to left. How this can be done.

预先感谢。

推荐答案

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

Do these modifications to your animation files:

enter.xml:

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>

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

You'll have your second activity sliding in from right to the left.

为更好地理解如何使用动画的fromXDelta和toXDelta值,以下是这些值的非常基本的说明:

For a better understanding on how to play around with the fromXDelta and toXDelta values for the animations, here is a very basic illustration on the values:

这种方式可以轻松地理解为什么添加android: fromXDelta = 0%和android:toXDelta =-100%用于您当前的活动。这是因为您希望它从0%升到-100%位置。

This way you can easily understand why you add android:fromXDelta="0%" and android:toXDelta="-100%" for your current activity. And this is because you want it to go from 0% to the -100% position.

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

So if you want to open ActivityB from ActivityA you do the following(let's say you have a button):

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);
        }
    });

现在,如果要离开时的第一个动画的向后动画B,您将需要2个新的动画文件和ActivityB的onBackPressed方法中的一些代码,如下所示:

Now, if you want to have the "backwards" animation of the first one, when you leave Activity B, you'll need 2 new animation files and some code in the ActivityB's onBackPressed method, like this:

首先动画文件:
left_to_right.xml:

First the animation files: 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:

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中执行以下操作:

And in ActivityB do the following:

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

此外,如果您启用了向上导航功能,则还必须添加动画在这种情况下也是如此:

Also if you have up navigation enabled, you'll have to add the animation in this case as well:

您可以像这样启用UP导航:

You enable UP navigation like this:

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

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

And this is how you handle the animation in this case too:

@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);
}

另外请注意,即使您的代码正常,您的手机也可能具有动画效果关掉。要打开然后执行以下操作:

Also be aware that even if your code is okay, your phone might have animation turned off. To turn then on do the following:


  1. 打开设置并转到开发人员选项

  2. 确保启用它(通过滑动右上角的切换按钮)

  3. 向下滚动并在绘图下,一个接一个地点击以下选项:Windows动画比例,过渡动画比例和Animator持续时间比例

  4. 选择动画比例1x

  1. Open Settings and go to Developer Options
  2. Make sure it is enabled (By sliding the toggle button on the top right)
  3. Scroll down and under Drawing, tap these options one by one: Windows animation scale, Transition animation scale, and Animator duration scale
  4. Select "Animation scale 1x"

这有帮助吗?

这篇关于如何实现从右到左的动画来开始活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆