標籤:android android 5.0 過渡動畫
前言
Activity Transition:
提供了三種Transition類型:
進入:一個進入的過渡(動畫)決定activity中的所有的視圖怎麼進入螢幕。
退出:一個退出的過渡(動畫)決定一個activity中的所有視圖怎麼退出螢幕。
共用元素:一個共用元素過渡(動畫)決定兩個activities之間的過渡,怎麼共用(它們)的視圖。
<span style="font-size:18px;"><span style="font-family: Arial, Helvetica, sans-serif;"></span><span style="font-family: Arial; line-height: 26px;"><span style="color:#ff6666;">支援這些進入和退出的過渡動畫</span><span style="color:#333333;">:</span></span><span style="color: rgb(51, 51, 51); font-family: Arial, Helvetica, sans-serif;"></span></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;"><span style="font-family: Arial; line-height: 26px;">explode(分解) –進或出地行動裝置檢視,從螢幕中間</span><span style="font-family: Arial; line-height: 26px;">slide(滑動) -進或出地行動裝置檢視,從螢幕邊緣</span><span style="font-family: Arial; line-height: 26px;">fade(淡出) –通過改變螢幕上視圖的不透明度達到添加或者移除視圖(的效果)</span></span></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;color:#ff6666;">在以上動畫基礎上還可以添加還支援共用元素過渡:(以上效果的共用元素效果基於分解動畫基礎上進行</span></span><span style="color: rgb(255, 102, 102); font-family: Arial, Helvetica, sans-serif;font-size:18px;">)</span>
它的作用就是共用兩個acitivity種共同的元素,在Android 5.0下支援如下效果:
changeBounds - 改變目標視圖的布局邊界
changeClipBounds - 裁剪目標視圖邊界
changeTransform - 改變目標視圖的縮放比例和旋轉角度
changeImageTransform - 改變靶心圖表片的大小和縮放比例
使用步驟:
1.設定動畫(兩種方式):
1.1xml設定
當你定義繼承了material主題樣式時,使用android:windowContentTransitions屬性啟用視窗的內容轉換(效果)。你還可以指定進入、退出、和共用元素的轉換:
<style name="myTheme" parent="android:Theme.Material"> <!-- 允許使用transitions --> <item name="android:windowContentTransitions">true</item> <!-- 指定進入和退出transitions --> <item name="android:windowEnterTransition">@transition/explode</item> <item name="android:windowExitTransition">@transition/explode</item> <!-- 指定shared element transitions --> <item name="android:windowSharedElementEnterTransition"> @transition/change_image_transform</item> <item name="android:windowSharedElementExitTransition"> @transition/change_image_transform</item></style>
定義transition動畫:
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android"> <explode/> <changeBounds/> <changeTransform/> <changeClipBounds/> <changeImageTransform/></transitionSet>
1.2代碼設定
// 允許使用transitionsgetWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);// 設定一個exit transitiongetWindow().setExitTransition(new Explode());//new Slide() new Fade()
Window.setEnterTransition():設定進入動畫
Window.setExitTransition():設定退出效果
Window.setSharedElementEnterTransition():設定共用元素的進入動畫
Window.setSharedElementExitTransition():設定共用元素的退齣動畫
2.Activity跳轉方法:
進入退齣動畫跳轉:
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
共用元素跳轉:
為了使有一個共用元素的兩個activities間使用過渡動畫:
1.在你的主題中啟用視窗內容過渡
2.在你的主題樣式中指定共用元素的過渡
3.定義你的過渡動畫為XML資源
4.使用android:transitionName屬性給兩個布局中的共用元素指定一個相同的名字(名字一定不要寫錯)
5.使用ActivityOptions.makeSceneTransitionAnimation() 方法
代碼:
// 共用跳轉
intent = new Intent(Transitions.this, Transitions4.class); startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this,view,"shareName").toBundle());
跳轉目標xml:
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white"> <LinearLayout android:orientation="horizontal" android:id="@+id/ll" android:background="#2faaf3" android:elevation="2dip" android:layout_width="fill_parent" android:layout_height="200dp"/> <TextView android:id="@+id/btn_test" android:elevation="8dip" android:padding="10dip" android:layout_x="300dip" android:layout_y="175dip" android:transitionName="shareName" android:layout_width="50dp" android:layout_height="50dp" android:background="@drawable/myrect" android:layout_below="@+id/ll" android:layout_alignParentEnd="true" android:layout_marginRight="56dp"/></AbsoluteLayout>
如果有多個共用元素:
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this, Pair.create(view1, "agreedName1"), Pair.create(view2, "agreedName2"));
去試試吧~!
ym—— Android 5.0學習之Activity過渡動畫