Android 记录一下ARouter跳转动画不生效的问题

2021-09-02  本文已影响0人  坑逼的严

今天UI对我们说你们Android的跳转动画为什么没有IOS的好看,你们都是左右切换的。我心想这不就是一个跳转动画的设置吗,有什么难的。我们基于Arouter跳转的,这个问题不大。

String appRoute = MappingPath.getAppRoute(schema);
                Postcard build = ARouter.getInstance().build(appRoute);
                if (bundle != null) {
                    build.with(bundle);
                }
                if (flag != -1) {
                    build.withFlags(flag);
                }

                build.withString(Constants.library.DotSchema, schema).navigation(context);

这是我们封装的代码片段,没毛病,我在跳转前设置个动画就行。于是我加了下面的代码

build.withString(Constants.library.DotSchema, schema)
.withTransition(R.anim.suplayer_anim_alpha_in,R.anim.suplayer_anim_alpha_out)
.navigation(context);

心想着完美了,但是运行,并没有奏效。
我靠,这么简单的东西,不能生效?于是我怀疑我的动画写错了,但是单独写了个demo运行,很完美,下面是动画的代码
suplayer_anim_alpha_in

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0"
    android:toAlpha="1"
    android:duration="300">
</alpha>

suplayer_anim_alpha_out

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="1"
    android:toAlpha="0"
    android:duration="300">
</alpha>

那怎么办?看源码呗,会后一路跟踪下来走到了ARouter最后跳转activity的代码

private void startActivity(int requestCode, Context currentContext, Intent intent, Postcard postcard, NavigationCallback callback) {
        if (requestCode >= 0) {  // Need start for result
            ActivityCompat.startActivityForResult((Activity) currentContext, intent, requestCode, postcard.getOptionsBundle());
        } else {
            ActivityCompat.startActivity(currentContext, intent, postcard.getOptionsBundle());
        }

        if ((-1 != postcard.getEnterAnim() && -1 != postcard.getExitAnim()) && currentContext instanceof Activity) {    // Old version.
            ((Activity) currentContext).overridePendingTransition(postcard.getEnterAnim(), postcard.getExitAnim());
        }

        if (null != callback) { // Navigation over.
            callback.onArrival(postcard);
        }
    }

其中设置跳转动画的代码

if ((-1 != postcard.getEnterAnim() && -1 != postcard.getExitAnim()) && currentContext instanceof Activity) {    // Old version.
            ((Activity) currentContext).overridePendingTransition(postcard.getEnterAnim(), postcard.getExitAnim());
        }

这里注意两点:
1、进入和退出动画都要设置,不能单独设置一个,不设置就是-1的,那么就不满足他的第一个判断,哪怕你设置动画id为0都行

(-1 != postcard.getEnterAnim() && -1 != postcard.getExitAnim())

2、一定要传activity类型的context
于是,我重新检查了封装的代码,发现用的是ApplicationContext,嗯,意识问题解决了。

上一篇下一篇

猜你喜欢

热点阅读