LayoutAnimation 主要是实现viewgroup 子view改变的动画。OK先看一个效果
要实现这样的效果一般有两种方式:代码 和xml 配置两种
1.xml方式
先创建一个item_anim.xml的
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0"
android:toAlpha="1"
android:interpolator="@android:anim/decelerate_interpolator"
android:duration="1000"
>
</alpha>
这里在啰嗦一下android interpolator的一些属性
AccelerateDecelerateInterpolator 在动画开始与结束的地方速率改变比较慢,在中间的时候加速
AccelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始加速
AnticipateInterpolator 开始的时候向后然后向前甩
AnticipateOvershootInterpolator 开始的时候向后然后向前甩一定值后返回最后的值
BounceInterpolator 动画结束的时候弹起
CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator 在动画开始的地方快然后慢
LinearInterpolator 以常量速率改变
OvershootInterpolator 向前甩一定值后再回到原来位置
之后在创建一个layout_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="1.2"
android:animationOrder="random"
android:animation="@anim/item_anim">
</layoutAnimation>
android:delay 表示动画播放的延时可以是百分比float 小数
android:animationOrder表示动画执行的顺序 总共有三个normal(按顺序)、reverse(反序)、random(随机)
android:animation 表示子控件要播放的动画
至此只需要将这个引用在布局中就好了。
如下
<RelativeLayout 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"
>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutAnimation="@anim/layout_anim"
></ListView>
</RelativeLayout>
2.代码中实现
private void setAnimation(){
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.item_anim);
LayoutAnimationController controller = new LayoutAnimationController(animation);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
controller.setDelay(0.3f);
listView.setLayoutAnimation(controller);
listView.startLayoutAnimation();
}
ok至此