種類
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.在界面中調(diào)用
//單元素過渡
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不會生效