我想做的事:
假设我在屏幕的右上角有一个 imageView。
A. 当我点击它时,它会移动/平移到屏幕中心
B. 然后它应该使用 SharedElement Transition 动画成新的活动
2 个问题
我做得对吗
如何结合A和B
在乔治的回答之后
v21/主题.xml
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementExitTransition">@transition/exit_slide_transition</item>
<item name="android:windowSharedElementEnterTransition">@transition/enter_transition</item>
</style>
</resources>
exit_slide_transition.xml
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<changeBounds
android:duration="1000"
android:interpolator="@android:anim/linear_interpolator"/>
</transitionSet>
linear.xml (由 AnimationUtils.loadAnimation 使用)
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<translate
android:fromYDelta="0"
android:toYDelta="45%p"
android:fromXDelta="0"
android:toXDelta="45%p"
android:duration="1000"/>
</set>
enter_transition.xml
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<changeImageTransform/>
</transitionSet>
启动 Activity + moveImage():
//StartActivity:
ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(
activity, transitionView, EXTRA_IMAGE);
Intent intent = new Intent(activity, DetailActivity.class);
intent.putExtra(EXTRA_IMAGE, url);
ActivityCompat.startActivity(activity, intent, options.toBundle());
//moveImage():
RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams((int)getResources().getDimension(R.dimen.my_image_dp),(int)getResources().getDimension(R.dimen.my_image_dp));
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
imageView.setLayoutParams(layoutParams);