我們使用Scene來(lái)展示動(dòng)畫(huà)。
首先我們獲取場(chǎng)景,代碼如下:
Scene scene1 = Scene.getSceneForLayout(frameLayout, R.layout.animations_scenes1, this);
frameLayout為盛放控件的總?cè)萜鳌?br>
animations_scenes1為盛放控件。
this為上下文。
我們寫(xiě)四個(gè)scene布局:
animations_scenes1代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<ImageView
android:id="@+id/iv_green"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@color/theme_green_primary"/>
<ImageView
android:id="@+id/iv_red"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:background="@color/theme_red_primary"/>
<ImageView
android:id="@+id/iv_blue"
android:layout_marginTop="20dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@id/iv_red"
android:background="@color/theme_blue_primary"/>
<ImageView
android:id="@+id/iv_yellow"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="@id/iv_green"
android:layout_alignParentRight="true"
android:layout_marginTop="20dp"
android:background="@color/theme_yellow_primary"/>
</RelativeLayout>
animations_scenes2 -- 4 置換ImageView的位置。要注意的是四個(gè)布局的id要一直,這樣動(dòng)畫(huà)就可以在同id的控件間傳動(dòng)。
下面是動(dòng)畫(huà)轉(zhuǎn)換的資源文件slide_and_changebounds_sequential_with_interpolators.xml:
<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:transitionOrdering="sequential">
<slide android:interpolator="@android:interpolator/decelerate_cubic"/>
<changeBounds android:interpolator="@android:interpolator/bounce"/>
</transitionSet>
最后調(diào)用方法轉(zhuǎn)換:
TransitionManager.go(scene1, TransitionInflater.from(AnimationsActivity2.this).inflateTransition(R.transition.slide_and_changebounds_sequential));