Android 共享元素重新进入转换没有动画
Android shared element reenter transition no animation
我想创建一个带有共享元素的 activity 转换,如 here 所述。
我想让左边的列表项变成右边的高亮框。
嗯,效果很好,但遗憾的是退出过渡根本不起作用。在我从第二个 activity 返回后,我得到了这个:
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
和左侧的 RelativeLayout
。
Activity 和片段共享转换都很难调试。默认情况下动画速度很快,有时眼睛只能看到 "there is something wrong" 但弄清楚 what/why 可能很重要。
在调试从 A 到 B 的共享转换时,我通常发现 Override onMapSharedElements
方法非常有用,因此您可以轻松找出 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());
}
通过这种方式,可以更容易地找出哪些元素未被映射(也许它们只是尚未创建)并解决共享转换问题(即:故障、伪像)
我想创建一个带有共享元素的 activity 转换,如 here 所述。
我想让左边的列表项变成右边的高亮框。
嗯,效果很好,但遗憾的是退出过渡根本不起作用。在我从第二个 activity 返回后,我得到了这个:
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
和左侧的 RelativeLayout
。
Activity 和片段共享转换都很难调试。默认情况下动画速度很快,有时眼睛只能看到 "there is something wrong" 但弄清楚 what/why 可能很重要。
在调试从 A 到 B 的共享转换时,我通常发现 Override onMapSharedElements
方法非常有用,因此您可以轻松找出 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());
}
通过这种方式,可以更容易地找出哪些元素未被映射(也许它们只是尚未创建)并解决共享转换问题(即:故障、伪像)