layoutAnimation作用於viewGroup,爲ViewGroup制定一個動畫,他的每個子元素都會按照這種動畫出場。
首相定義LayoutAnimation如下所示:
<?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/animation_scale" />
android:delay 表示子元素開始動畫的時間延遲。
android:animationOrder 表示動畫播放的順序呢
normal:順序顯示
reverse:表示逆向顯示
random:隨機播放
android:animation 爲每個子元素指定具體的入場動畫,如下
<?xml version="1.0" encoding="utf-8"?>
<!-- 從0.1放大到1.5 -->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.1"
android:toXScale="1.5"
android:fromYScale="0.1"
android:toYScale="1.5"
android:duration="2000"
>
</scale>
最後爲GroupView指定LayoutAnimation屬性,以listview爲例:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/xlv"
android:layoutAnimation="@anim/animation_layout"
android:background="#fff4f7f9"
android:divider="#ddd"
android:dividerHeight="1.0px"
android:listSelector="#999"
></ListView>
除了在XML文件中進行設置,我們還可以使用代碼來完成:
Animation animation = AnimationUtils.loadAnimation(this, R.anim.animation_scale);
LayoutAnimationController controller = new LayoutAnimationController(animation);
controller.setDelay(0.5f);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
xlv.setLayoutAnimation(controller);
效果如下