Android 5上的共享元素活动过渡 [英] Shared element activity transition on android 5

本文介绍了Android 5上的共享元素活动过渡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从一个活动转到另一个活动时,我想设置一个共享元素过渡.

I wanted to setup a shared element transition when going from one Activity to another.

第一个活动具有一个带有项目的RecyclerView.单击某个项目后,该项目应设置为新活动的动画.

The first Activity has a RecyclerView with items. When an item is clicked that item should animate to the new activity.

所以我设置了一个 android:transitionName ="item"都位于两个最终活动视图中,就像回收者视图项目视图一样.

So i've set a android:transitionName="item" on both the final activity views, as wel as the recycler-view item views.

在进行下一个活动时,我也在使用此代码:

I'm also using this code when going to the next activity:

this.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this, itemView, "boomrang_item").toBundle());

单击一个项目时,它会正确过渡并显示新视图.真的很好 但是,当我单击后退按钮时.有时它可以正常工作,但是在大多数情况下,我的活动因以下堆栈跟踪而崩溃:

When clicking an item, it transitions properly and the new view is shown. It is really nice. However when i click the back button. Sometimes it works fine, but most of the time my activity crashes with the following stacktrace:

   java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.transformMatrixToGlobal(android.graphics.Matrix)' on a null object reference
            at android.view.GhostView.calculateMatrix(GhostView.java:95)
            at android.app.ActivityTransitionCoordinator$GhostViewListeners.onPreDraw(ActivityTransitionCoordinator.java:845)
            at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:847)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1956)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:550)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我在做什么错? 看起来像是Android 5中的错误

What am i doing wrong? It looks like a bug in android 5

推荐答案

我遇到了相同的问题,并且注意到当您返回时,如果先前的屏幕上不再显示原始共享元素,则会发生崩溃.屏幕上的最后一个元素以纵向显示,但是一旦切换为横向,它就不再可见),因此过渡位置无处放回共享元素.

I encounter the same issue, and notice the crash happens if the original shared element is no longer visible on the previous screen when you go back (probably it is the last element on screen in portrait, but once switched to landscape it's no longer visible), and thus the transition has nowhere to put back the shared element.

我的解决方法是,如果在返回屏幕之前已经旋转了屏幕,则要删除返回过渡(在第二个活动中),但是我确定必须有更好的方法来处理此问题:

My workaround is to remove the return transition (in the 2nd activity) if the screen has been rotated before going back, but I'm sure there must be a better way to handle this:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mOrientationChanged = !mOrientationChanged;
}

@Override
public void supportFinishAfterTransition() {
    if (mOrientationChanged) {
        /**
         * if orientation changed, finishing activity with shared element
         * transition may cause NPE if the original element is not visible in the returned
         * activity due to new orientation, we just finish without transition here
         */
        finish();
    } else {
        super.supportFinishAfterTransition();
    }
}

这篇关于Android 5上的共享元素活动过渡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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