注:要Android 5.0 及以上才支持
1倔撞、設(shè)置支持動(dòng)畫
在styles里里面設(shè)置
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!-- <item name="android:windowNoTitle">true</item>-->
<item name="windowNoTitle">true</item>
<!-- <item name="android:windowActionBar">false</item>-->
//設(shè)置支持動(dòng)畫
<item name="android:windowContentTransitions">true</item>
<!--動(dòng)畫重疊变姨,默認(rèn)true,false就是要等另一個(gè)動(dòng)畫走完了才走該動(dòng)畫-->
<!-- <item name="android:windowAllowEnterTransitionOverlap">false</item>-->
<!-- <item name="android:windowAllowReturnTransitionOverlap">false</item>-->
</style>
</resources>
2寺渗、定義動(dòng)畫
// 側(cè)滑動(dòng)畫
Slide transition = new Slide();
transition.setSlideEdge(Gravity.LEFT);
// 爆炸效果的動(dòng)畫
Explode transition2 = new Explode();
// 漸變動(dòng)畫
Fade transition3 = new Fade();
transition.setDuration(getResources().getInteger(R.integer.anim_duration_long));
transition2.setDuration(getResources().getInteger(R.integer.anim_duration_long));
transition3.setDuration(getResources().getInteger(R.integer.anim_duration_long));
3大猛、設(shè)置動(dòng)畫
頁(yè)面繼承AppCompatActivity历涝,否則要加判斷 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
getWindow().setExitTransition(transition); //跳轉(zhuǎn)時(shí)的退出時(shí)動(dòng)畫
getWindow().setReenterTransition(transition2); //重新進(jìn)入時(shí)動(dòng)畫
getWindow().setEnterTransition(transition); //進(jìn)入時(shí)的動(dòng)畫
getWindow().setReturnTransition(transition2); //返回時(shí)的退出動(dòng)畫
4诅需、頁(yè)面跳轉(zhuǎn)
Pair是共享動(dòng)畫的共享元素,沒(méi)有共享動(dòng)畫元素 可不傳荧库,下面再說(shuō)共享動(dòng)畫
//Pair是共享動(dòng)畫的共享元素堰塌,沒(méi)有共享動(dòng)畫元素 可不傳
Pair<View, String>[] pairs = TransitionHelper.createSafeTransitionParticipants(this, false, new Pair<View, String>(tv1, tv1.getTransitionName()));
ActivityOptionsCompat aoc = ActivityOptionsCompat.makeSceneTransitionAnimation(this, pairs);
startActivity(new Intent(this, TwoActivity.class), aoc.toBundle());
5、頁(yè)面關(guān)閉
supportFinishAfterTransition();
如果頁(yè)面沒(méi)有繼承AppCompatActivity分衫,就這樣寫
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
finishAfterTransition();
} else
super.finish();
上面用到的工具類场刑,主要是處理狀態(tài)欄和導(dǎo)航欄是否也要跟著動(dòng)畫
/**
* Wing_Li
* 2016/9/14.
* 共享過(guò)渡動(dòng)畫加入狀態(tài)欄和導(dǎo)航欄,這樣頁(yè)面轉(zhuǎn)場(chǎng)動(dòng)畫時(shí) 狀態(tài)欄和導(dǎo)航欄 不會(huì)跟著動(dòng)
*/
public class TransitionHelper {
/**
* 創(chuàng)建所需的活動(dòng)過(guò)渡期間避免與系統(tǒng)UI小過(guò)渡的參與者蚪战。
*
* @param activity The activity used as start for the transition.
* @param includeStatusBar 如果是錯(cuò)誤的牵现,狀態(tài)欄將不會(huì)被添加為過(guò)渡參與者
* @return All transition participants.
*/
public static Pair<View, String>[] createSafeTransitionParticipants(@NonNull AppCompatActivity activity, boolean
includeStatusBar, @Nullable Pair... otherParticipants) {
// Avoid system UI glitches as described here:
// https://plus.google.com/+AlexLockwood/posts/RPtwZ5nNebb
View decor = activity.getWindow().getDecorView();
View statusBar = null;
if (includeStatusBar) {
statusBar = decor.findViewById(android.R.id.statusBarBackground);
}
View navBar = decor.findViewById(android.R.id.navigationBarBackground);
// 創(chuàng)建一對(duì)過(guò)渡參與者。
List<Pair> participants = new ArrayList<>(3);
addNonNullViewToTransitionParticipants(statusBar, participants);
addNonNullViewToTransitionParticipants(navBar, participants);
// only add transition participants if there's at least one none-null element
// 只有添加過(guò)渡參與者邀桑,如果至少有一個(gè)非空元素
if (otherParticipants != null && !(otherParticipants.length == 1 && otherParticipants[0] == null)) {
participants.addAll(Arrays.asList(otherParticipants));
}
return participants.toArray(new Pair[participants.size()]);
}
private static void addNonNullViewToTransitionParticipants(View view, List<Pair> participants) {
if (view == null) {
return;
}
participants.add(new Pair<>(view, view.getTransitionName()));
}
}
至此瞎疼,已經(jīng)實(shí)現(xiàn)了基本的轉(zhuǎn)場(chǎng)過(guò)渡動(dòng)畫了,
添加共享動(dòng)畫元素 :
兩個(gè)控件的屬性android:transitionName 的值 一樣即可:
1.png