LayoutAnimation与GridLayoutAnimation
LayoutAnimation
对viewGroup及其子类添加进入统一动画的LayoutAnimation和针对grideView添加进入动画的gridLayoutAnimation
使用示例
layoutAnimation属性只在ViewGroup及其子类创建时有效
<!-- anim/ayout_animation.xml -->
<!-- xml形式实现 -->
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="1"
android:animationOrder="normal"
android:animation="@anim/slide_in_left"/>
<!-- java形式实现 -->
Animation animation= AnimationUtils.loadAnimation(this,R.anim.slide_in_left);
LayoutAnimationController controller = new LayoutAnimationController(animation);
controller.setOrder(LayoutAnimationController.ORDER_REVERSE);
controller.setDelay(0.3f);
mListView.setLayoutAnimation(controller);
mListView.startLayoutAnimation();
<!-- anim/slide_in_left.xml -->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000">
<translate android:fromXDelta="-50%p" android:toXDelta="0"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"/>
</set>
<!-- 使用 -->
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutAnimation="@anim/layout_animation"/>
属性解析
属性 | 解析 |
---|---|
delay | 每个item延时时间的百分比,默认0.5,即在上一个item动画开始后的0.5倍时间后开始动画 |
animationOrder | 动画顺序,normal:正序,reverse:倒序,random:随机 |
animation | 指定每个item的入场动画,只能使用animation动画 |
GridLayoutAnimation
使用示例
<!-- xml实现 -->
<?xml version="1.0" encoding="utf-8"?>
<gridLayoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:rowDelay="75%"
android:columnDelay="60%"
android:directionPriority="none"
android:direction="bottom_to_top|right_to_left"
android:animation="@android:anim/slide_in_left"/>
<!-- java实现 -->
Animation animation = AnimationUtils.loadAnimation(MyActivity.this,R.anim.slide_in_left);
GridLayoutAnimationController controller = new GridLayoutAnimationController(animation);
controller.setColumnDelay(0.75f);
controller.setRowDelay(0.5f); controller.setDirection(GridLayoutAnimationController.DIRECTION_BOTTOM_TO_TOP|GridLayoutAnimationController.DIRECTION_LEFT_TO_RIGHT);
controller.setDirectionPriority(GridLayoutAnimationController.PRIORITY_NONE);
gridView.setLayoutAnimation(controller);
gridView.tartLayoutAnimation();
属性解析
属性 | 解析 |
---|---|
rowDelay | 每一行动画开始的延迟 |
columnDelay | 每一列动画开始的延迟 |
directionPriority | 方向优先级,row:行优先,collumn:列优先,none:同时进行。如,行优先,则一行一行的执行动画,第一行执行完后再执行第二行,其他同理 |
direction | 动画方向,默认left_to_right。left_to_right,right_to_left,top_to_bottom,bottom_to_top,四个值可以使用"|"连接 |
animation | 指定每个item的入场动画,只能使用animation动画 |
自定义LayoutAnimation
自定义类继承LayoutAnimationController或GridLayoutAnimationController
public class MyGridLayoutAnimationController extends GridLayoutAnimationController {
public MyGridLayoutAnimationController(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridLayoutAnimationController(Animation animation) {
super(animation);
}
public MyGridLayoutAnimationController(Animation animation, float columnDelay, float rowDelay) {
super(animation, columnDelay, rowDelay);
}
}