利用Android开发艺术探索的例子,实践了一下
给ViewGroup的子元素添加出场效果,遵循以下几个步骤
1.在res/anim/anim_layout中定义LayoutAnimation
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="normal"
android:animation="@anim/anima_item"
/>
2.位子元素指定具体的入场动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true"
>
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"/>
<translate
android:fromXDelta="500"
android:toXDelta="0"/>
</set>
3.为ViewGroup指定android:layoutAnimation属性,=“@anim/anim_layout”。这种方式适用所有的ViewGroup
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutAnimation="@anim/anima_layout"
android:background="#fff4f7f9"
android:cacheColorHint="#000"
android:divider="#dddbdb"
android:dividerHeight="1px"
android:listSelector="@android:color/transparent"/>
除了在XML中指定LayoutAnimation外,还可以通过LayoutAnimationController来实现,具体的代码如下:
Animation animation= AnimationUtils.loadAnimation(this,R.anim.anima_item);
LayoutAnimationController animationController=new LayoutAnimationController(animation);
animationController.setDelay(0.5f);
animationController.setOrder(LayoutAnimationController.ORDER_NORMAL);
listview.setLayoutAnimation(animationController);