1.在style.xml中设置
<item name="android:windowContentTransitions">trueitem>
<item name="android:windowEnterTransition">@transition/explodeitem>
<item name="android:windowExitTransition">@transition/explodeitem>
<item name="android:windowReenterTransition">@transition/explodeitem>
<item name="android:windowReturnTransition">@transition/explodeitem>
2.在res目录下,新建一个transition文件夹,创建xxx.xml文件(就是动画xml)
explode.xml
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<explode
android:duration="500"
android:interpolator="@android:interpolator/accelerate_decelerate" />
transitionSet>
silde.xml
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<slide
android:duration="500"
android:interpolator="@android:interpolator/accelerate_decelerate" />
transitionSet>
fade.xml
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<fade
android:duration="500"
android:interpolator="@android:interpolator/accelerate_decelerate" />
transitionSet>
3.跳转Activity页面代码(比较容易忘):
Intent intent = new Intent(MainActivity.this, SlideActivity.class);
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
1.在跳转Activity页面时,例如A页面跳转到B页面:
A页面关键代码:
Intent intent = new Intent(MainActivity.this, SlideActivity.class);
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
B页面关键代码:代码需放在setContentView()之前,
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
getWindow().setEnterTransition(new Explode());
getWindow().setExitTransition(new Explode());
//getWindow().setReenterTransition(new Explode());
//getWindow().setReturnTransition(new Explode());
setContentView()
两种实现方式,觉得还是xml好用,统一了页面进入和退出动画的风格,修改也比较方便
1.在style.xml中
<item name="android:windowContentTransitions">trueitem>
<item name="android:windowSharedElementEnterTransition">@transition/share1item>
<item name="android:windowSharedElementExitTransition">@transition/share2item>
<item name="android:windowSharedElementReenterTransition">@transition/share3item>
<item name="android:windowSharedElementReturnTransition">@transition/share4item>
2.xml动画(res/transition/xxx.xml)
share1.xml
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<changeBounds
android:duration="500"
android:interpolator="@android:interpolator/accelerate_decelerate" />
transitionSet>
share2.xml
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<changeClipBounds
android:duration="500"
android:interpolator="@android:interpolator/accelerate_decelerate" />
transitionSet>
share3.xml
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<changeTransform
android:duration="500"
android:interpolator="@android:interpolator/accelerate_decelerate" />
transitionSet>
share4.xml
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<changeImageTransform
android:duration="500"
android:interpolator="@android:interpolator/accelerate_decelerate" />
transitionSet>
set动画中可以多种动画组合
3.跳转页面代码:
一个共享元素
Intent intent3 = new Intent(MainActivity.this, Share1Activity.class);
startActivity(intent3, ActivityOptions.makeSceneTransitionAnimation(this, button4, "share").toBundle());
多个共享元素
startActivity(intent3, ActivityOptions.makeSceneTransitionAnimation(this, new PairString>(button4, "share"), new PairString>(button4, "share")).toBundle());
new Pair()是可变参数
共享元素的key:view控件 ,value:与android:transitionName="share" 相同(也可以通过代码设置setTransitionName())
1.页面跳转代码与xml实现相同
区别需要多试才知道
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
getWindow().setSharedElementExitTransition(new ChangeBounds());
//getWindow().setSharedElementEnterTransition(new ChangeBounds());
getWindow().setSharedElementReenterTransition(new ChangeBounds());
//getWindow().setSharedElementReturnTransition(new ChangeBounds());
setContentView()
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
getWindow().setSharedElementsUseOverlay(true);
// getWindow().setSharedElementsUseOverlay(false);
参考:Android群英传,深入理解共享元素变换(Shared Element Transition)-上