种类
ActivityOptionsCompat.makeCustomAnimation(Context context, int enterResId, int exitResId)
ActivityOptionsCompat.makeScaleUpAnimation(View source,int startX, int startY, int startWidth, int startHeight)
ActivityOptionsCompat.makeThumbnailScaleUpAnimation(View source,Bitmap thumbnail, int startX, int startY)
//单元素共享动画
ActivityOptionsCompat.makeSceneTransitionAnimation(Activity activity, View sharedElement, String sharedElementName)
//多元素共享动画
ActivityOptionsCompat.makeSceneTransitionAnimation(Activity activity,Pair<View, String>… sharedElements)
使用
1.在theme中配置(非必须)
<style name="AppTheme" parent="Theme.AppCompat">
<item name="android:windowContentTransitions">true</item>
</style>
2.配置用来过渡的view
//需要在当前Activity和目标Activity中同时定义
android:transitionName="@string/to_device_info"
3.在界面中调用
//单元素过渡
val intent = Intent(this@MainActivity, DeviceInfoActivity::class.java)
val toBundle = ActivityOptionsCompat.makeSceneTransitionAnimation(
this@MainActivity,
tv_test,
ResourceUtil.getString(R.string.to_device_info)
).toBundle()
startActivity(intent, toBundle)
//多元素过渡
val pair1 = android.support.v4.util.Pair<View, String>(tv_test,ResourceUtil.getString(R.string.to_device_info))
val pair2 = android.support.v4.util.Pair<View, String>(tv_test,ResourceUtil.getString(R.string.to_device_info))
val intent = Intent(this@MainActivity, DeviceInfoActivity::class.java)
val bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(this@MainActivity, pair1,pair2).toBundle()
startActivity(intent, bundle)
4.目标Activity返回
finishAfterTransition() //将finish替换
注意
过渡动画只对activity生效,fragment不会生效