1

我想用这里解释的共享元素创建一个活动转换。

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

好吧,这很好用,但不幸的是,退出转换根本不起作用。从第二个活动返回后,我得到了这个: 在此处输入图像描述

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>

我已将右侧的布局设置为 ,左侧的设置为transitionNames 。android.support.v7.widget.CardViewRelativeLayout

4

1 回答 1

6

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

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

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

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

于 2015-09-16T13:23:11.880 回答