Activity 导航:自定义动画与 popEnter 和 popExit 类似的片段 [英] Activity navigation: custom animation with popEnter and popExit like fragments

查看:24
本文介绍了Activity 导航:自定义动画与 popEnter 和 popExit 类似的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码可以使用动画更改活动:

Changing activities with an animation is possible using the code below:

Bundle animation = ActivityOptions.makeCustomAnimation(App.getContext(), R.anim.enter_from_right, R.anim.exit_to_left).toBundle();
startActivity(intent, animation);

对于 Fragment,您可以在 FragmentTransaction 上执行类似的操作:

For fragments you can do something similar on FragmentTransaction:

// ...
transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
// ...

这行得通!但是我想在按下时有一个动画(从后台弹出).对于片段,您只需添加 2 个动画资源(popEnter 和 popExit):

This works! But I'd like to have an animation when pressing back (pop from backstack). For fragments you simply add 2 anim resources (popEnter & popExit):

transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);

如何为活动创建相同的背景动画"?

How can I create the same 'back-animation' for activities?

推荐答案

我发现了一种不同但简单的方法,似乎效果很好.活动的动画也可以使用 overridePendingTransition 来执行,因此当活动完成时,您只需使用该方法即可.

I've found a different though simple approach that seems to work quite well. Animations for activities can also be performed using overridePendingTransition, so when the activity finishes you'll simply use that method.

在 BaseActivity 中实现这些覆盖是最有效的,它由项目中的所有活动扩展.现在,您的所有活动都会在开始新活动时自动包含退出动画和动画:

It would be most effective to implement these overrides in a BaseActivity, which is extended by all activities in your project. Now all your activities will automatically include an exit animation and an animation when starting new activities:

public abstract class BaseActivity extends AppCompatActivity {

    @Override
    public void finish() {
        super.finish();
        onLeaveThisActivity();
    }

    protected void onLeaveThisActivity() {
        overridePendingTransition(R.anim.enter_from_left, R.anim.exit_to_right);
    }

    // It's cleaner to animate the start of new activities the same way.
    // Override startActivity(), and call *overridePendingTransition*
    // right after the super, so every single activity transaction will be animated:

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        onStartNewActivity();
    }

    @Override
    public void startActivity(Intent intent, Bundle options) {
        super.startActivity(intent, options);
        onStartNewActivity();
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        onStartNewActivity();
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
        super.startActivityForResult(intent, requestCode, options);
        onStartNewActivity();
    }

    protected void onStartNewActivity() {
        overridePendingTransition(R.anim.enter_from_right, R.anim.exit_to_left);
    }
}

为了总结一下,我将包括 4 个动画资源:

To round things up, I'll include the 4 anim resources:

enter_from_right

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="100%p"
    android:toXDelta="0%p"/>

exit_to_left

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="0%p"
    android:toXDelta="-100%p"/>

enter_from_left

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="-100%p"
    android:toXDelta="0%p"/>

exit_to_right

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="0%p"
    android:toXDelta="100%p"/>

ps.您可能希望在开始/主要活动中排除退出动画;-)

ps. You'd probably like to exclude the exit-animation in your starting / main activity ;-)

public class MainMenuActivity extends BaseActivity {
    ....
    @Override
    protected void onLeaveThisActivity() {
        // Don't use an exit animation when leaving the main activity!
    }
}

2019 年 10 月 24 日

当从一个活动导航到下一个活动并在流程中完成当前活动时,请注意应该在导航实现之前调用 finish().如果按错误的顺序执行此操作,onLeaveThisActivity 将在 onStartNewActivity 之后调用,从而导致错误的动画.

When navigating from one activity to the next, AND finishing the current activity in the process, take note that finish() should be called BEFORE the navigation implementation. If this is done in the wrong order, onLeaveThisActivity will be called AFTER onStartNewActivity, resulting in the wrong animation.

这篇关于Activity 导航:自定义动画与 popEnter 和 popExit 类似的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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