本学习笔记主要来自启舰:
http://blog.csdn.net/harvic880925/article/details/5078578
在学习过程中融入了自己的理解和思路。
这篇我们将讲述有关普通viewGroup添加进入统一动画的LayoutAnimation。
一、LayoutAnimation的xml实现——layoutAnimation标签
1、概述,使用layoutAnimation只需要三步:
第一步:定义一个layoutAnimation的animation文件layout_animation.xml
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animationOrder="normal"
android:interpolator="@android:anim/linear_interpolator"
android:animation="@anim/slide_in_bottom"
android:delay="1">
</layoutAnimation>
第二步:在viewGroup类型的控件中,添加
Android:layoutAnimation=”@anim/layout_animation”
如:
<android.support.v7.widget.RecyclerView
android:id="@+id/rvLayoutAnimate"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layoutAnimation="@anim/layout_animation"/>
在RecyclerView中添加上android:layoutAnimation="@anim/layout_animation"
来指定创建布局时,其子item所使用的动画。
第三步:定义slide_in_bottom的动画详情
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200">
<alpha
android:fromAlpha="0"
android:toAlpha="1"/>
<translate
android:fromXDelta="-70%p"
android:toXDelta="0"/>
<scale
android:fromXScale="0.5"
android:toXScale="1"
android:fromYScale="0.5"
android:toYScale="1"/>
</set>
这部分实现的效果是,让控件从左边70%的位置进入屏幕,同时透明度从0变到1;动画总时长为200毫秒;从0.5当大到1。
在layoutAnimation中,只有三个字段是有效的,分别是:android:delay、android:animationOrder和android:animation;其它诸如android:duration、android:interpolator等针对animation的字段都是无效的。下面我们结合上面的layoutAnimation代码,来看一下各个字段的具体意义:
delay:指每个Item的动画开始延时,取值是android: duration所指定动画时长的倍数,取值类型可以是float类型,也可以是百分数,默认是0.5;比如我们这里指定的动画是slide_in_bottom中指定android:duration="200",即单次动画的时长是200毫秒,而我们在这里的指定android:delay="1",即一个Item的动画会在上一个item动画完成后延时单次动画时长的一倍时间开始,即延时200毫秒后开始;
animationOrder:指viewGroup中的控件动画开始顺序,取值有normal(正序)、reverse(倒序)、random(随机);
animation:指定每个item入场所要应用的动画。仅能指定res/aim文件夹下的animation定义的动画,不可使用animator动画。比如本例中用了:android:animation="@anim/slide_in_bottom"。
通过上面的设置,RecyclerView的Item加载的时候,便有了动画效果。在代码里面不需要任何与动画有关的代码操作。
二、LayoutAnimation的代码实现——LayoutAnimationController
1、概述,上面我们讲过了LayoutAnimation的xml实现方式,下面来看看LayoutAnimation的代码实现方式。
代码实现动画API:
public LayoutAnimationController(Animation animation)
public LayoutAnimationController(Animation animation, float delay)
animation对应标签中的android:animation属性,delay对应标签中的android:delay属性。
LayoutAnimationController的函数如下:
public void setAnimation(Animation animation)//设置animation动画
public void setDelay(float delay)//设置单个item开始动画延时
public void setOrder(int order)//设置viewGroup中控件开始动画顺序
实际应用方面,去掉布局中RecyclerView的android:layoutAnimation="@anim/layout_animation"设置,然后在代码中添加如下代码:
private void constructeAnimarion(){
Animation animation = AnimationUtils.loadAnimation(this,
R.anim.slide_in_bottom);
LayoutAnimationController animationController = new LayoutAnimationController(animation);
animationController.setDelay(0.5f);
animationController.setOrder(LayoutAnimationController.ORDER_NORMAL);
mRvmLayoutAnimate.setLayoutAnimation(animationController);
mRvmLayoutAnimate.startLayoutAnimation();
mAdapterAnimate.refreshAdapter();
}
这样,RecyclerView的Item加载的时候,便有了动画效果。在通过xml和java代码分别实现动画的时候发现:java代码实现的动画在执行的时候,效果要流畅许多。
总结:
LayoutAnimationt和GridLayoutAnimation仅支持Animation动画,不支持Animator动画。