Android 动画(一)---布局动画
LayoutAnimation 可以用來為ViewGroup添加動畫,并按照預定的順序把一個動畫(或者動畫集合)應用到ViewGroup的第一個子View 中。
可以使用LayoutAnimationController 來指定一個應用到View組中的每一個動畫(或動畫集合)。ViewGroup中包含的每一個View都將應用到這個相同的動畫,但可以使用LayoutAnimationController來指定每一個View的順序和起始時間。
1、創建布局動畫
要創建一個新的布局動畫,首先要定義一個將應用于每個子View的動畫,然后,在代碼中或者作為外部動畫資源,創建一個新的LayoutAnimation,它引用了要應用的動畫并定義了應用它的順序和時刻安排。
1.1、定義一個將應用于每個子View的動畫 popin.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator"><scaleandroid:duration="2000"android:fromXScale="0.0"android:fromYScale="0.0"android:pivotX="50%"android:pivotY="50%"android:toXScale="1.0"android:toYScale="1.0" /></set>
1.2、定義一個LayoutAnimation ? popinlayout.xml
<?xml version="1.0" encoding="utf-8"?> <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"android:delay="0.5"android:animationOrder="reverse"android:animation="@anim/popin"></layoutAnimation>
其中:
android:animationOrder="reverse"指定ViewGroup中的每一個子View應用動畫的順序,這里指定為“逆序”;
android:animation="@anim/popin"指定要在每一個View中的應用的動畫,這里為1.1中的定義的動畫資源;
2、使用布局動畫
一旦定義了一個布局動畫,就可以使用代碼或者布局XML 資源將其應用到一個ViewGroup中。
2.1、在XML中使用,通過在布局定義中使用android:layoutAnimation="@anim/popinlayout"來完成
如下為 LinearLayout應用上述布局動畫
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity"android:layoutAnimation="@anim/popinlayout"><Buttonandroid:id="@+id/start_anim_bt"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/start_anim_bt_txt"/><TextViewandroid:id="@+id/info_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world" /><ImageViewandroid:id="@+id/img_iv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/image2"/></LinearLayout>
2.2、在代碼中設置一個布局動畫,可以調用ViewGroup的setLayoutAnimation,并給它傳遞所希望應用到的LayoutAnimation對象的引用。
通常情況下,布局動畫會在ViewGroup第一次進行布局的時候執行一次。可以通過對ViewGroup調用scheduleLayoutAnimation來強制它再次執行,然后當ViewGroup下次被布局的時候,這個動畫就會再次執行。
布局動畫也支持動畫監聽器。
<span style="white-space:pre"> </span>//得到要應用動畫的ViewGrouplinearlayout_vg=(LinearLayout)findViewById(R.id.linearlayout_vg);//得到在res/anim/popin.xml文件中定義的動畫資源Animation myAnimation= AnimationUtils.loadAnimation(MainActivity.this, R.anim.popin);LayoutAnimationController layoutAnimationController=new LayoutAnimationController(myAnimation);layoutAnimationController.setDelay(0.5f);//設置ViewGroup中每一個子View應用動畫的順序layoutAnimationController.setOrder(LayoutAnimationController.ORDER_REVERSE);//布局動畫也支持動畫監聽器linearlayout_vg.setLayoutAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {Toast.makeText(MainActivity.this,"動畫開始了。。。",Toast.LENGTH_SHORT).show();}@Overridepublic void onAnimationEnd(Animation animation) {Toast.makeText(MainActivity.this,"動畫結束了。。。",Toast.LENGTH_SHORT).show();}@Overridepublic void onAnimationRepeat(Animation animation) {Toast.makeText(MainActivity.this,"動畫重復執行了。。。",Toast.LENGTH_SHORT).show();}});//當ViewGroup被布局的時候,強制該動畫再次執行linearlayout_vg.scheduleLayoutAnimation();//在ViewGroup中應用布局動畫linearlayout_vg.setLayoutAnimation(layoutAnimationController);
總結
以上是生活随笔為你收集整理的Android 动画(一)---布局动画的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android---自定义Toast
- 下一篇: Android 动画(四)---逐帧动画