【问题标题】:Android shared element reenter transition no animationAndroid共享元素重入过渡无动画
【发布时间】:2015-04-12 14:42:41
【问题描述】:

我想创建一个带有共享元素的活动转换,如 here 所解释的那样。

我希望左侧的列表项转换为右侧突出显示的框。

嗯,这很好用,但不幸的是,退出转换根本不起作用。从第二个活动返回后,我得到了这个:

CardView 只是停留在原处,不会在任何地方缩放或变换。片刻之后,它消失了。

我认为退出转换将是向后播放的进入转换,但这里似乎并非如此。如何让 CardView 转换回左侧的 ListItem?

这是我当前的代码:

在第一个Activity的OnItemClickListener中

Intent intent = new Intent(context, DisplayEpisodeActivity.class);
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this,clickedRow.findViewById(R.id.background), "background");
startActivity(intent, options.toBundle());

我的主题:

<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="NewApi">
    <style name="BaseTheme" parent="android:Theme.Material.Light.DarkActionBar">
        <item name="android:colorPrimary">#D32F2F</item>
        <item name="android:textColorPrimary">#616161</item>
        <item name="android:colorPrimaryDark">#B71C1C</item>
        <item name="android:colorAccent">#757575</item>
        <item name="android:colorControlHighlight">#EF9A9A</item>
        <item name="android:windowContentTransitions">true</item>
        <item name="android:windowSharedElementEnterTransition">
            @transition/change_image_transform</item>
        <item name="android:windowSharedElementExitTransition">
            @transition/change_image_transform</item>
    </style>
</resources>

change_image_transform.xml:

<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
    <changeTransform/>
    <changeBounds/>
</transitionSet>

我已将transitionName 设置为右侧布局的android.support.v7.widget.CardView 和左侧的RelativeLayouts。

【问题讨论】:

  • 当它刚刚淡出时,表明共享元素未被对方接受。如果 transitionNames 不是在适配器中唯一分配(或视图持有者用于回收视图),您应该创建一个 SharedElementCallback 并覆盖 onMapSharedElements 以告诉共享元素转换哪个 View 是您的共享元素。
  • 谢谢@George!覆盖 onMapSharedElements 效果很好。
  • 谢谢@GeorgeMount !!您的评论应该是可接受的答案
  • @Shine 我完全同意。也许您可以添加更多详细信息并将其作为答案发布?
  • 给你@JohannBauer

标签: android transition android-5.0-lollipop android-cardview android-transitions


【解决方案1】:

Activity 和 Fragment Shared 转换都很难调试。默认情况下动画很快,有时眼睛只能看到“有问题”,但弄清楚什么/为什么是不平凡的。

在调试从 A 到 B 的共享转换时,我通常发现重写 onMapSharedElements 方法非常有用,因此您可以轻松找出 A 中的哪些共享元素与 B 中的元素相关联。这样的事情(在 B 的 @ 987654322@方法:

setEnterSharedElementCallback(new SharedElementCallback() {

            @Override
            public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
                Log.i(Constants.TAG, "EnterSharedElement.onMapSharedElements:" + sharedElements.size());
                //manual override of non-working shared elements goes here
                super.onMapSharedElements(names, sharedElements);
            }

            @Override
            public void onRejectSharedElements(List<View> rejectedSharedElements) {
                //Some element was rejected? Aha!!
                Log.i(Constants.TAG, "EnterSharedElement.onMapSharedElements:" + rejectedSharedElements.size());
                super.onRejectSharedElements(rejectedSharedElements);
            }

            @Override
            public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
                //AFTER the shared transition
                super.onSharedElementEnd(sharedElementNames, sharedElements, sharedElementSnapshots);
            }
        });
    }catch (Exception uie){
        Log.e(Constants.TAG,"UIE:"+uie.getMessage());
    }

通过这种方式,将更容易确定哪些元素没有被映射(也许它们还没有被创建)并解决共享转换问题(即:故障、伪影)

【讨论】:

  • 这很有用。除非日志都读取相同,因此共享元素似乎不会被拒绝。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
  • 1970-01-01
  • 2016-02-04
  • 2020-04-29
  • 1970-01-01
  • 2016-04-23
  • 2018-07-14
相关资源
最近更新 更多