1. 组件动画
设置ViewGroup
的layoutAnimation
属性,指定组件动画配置。
android:layoutAnimation="@anim/anim_layout_animation"
配置属性
delay
,动画播放延迟时间animationOrder
,子控件播放动画顺序。animationOrder
的值为normal
、reverse
和random
animation
,指定补间动画
组件动画anim_layout_animation.xml
文件
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.1"
android:animationOrder="normal"
android:animation="@anim/anim_in_from_right" />
补间动画anim_in_from_right.xml
文件
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="100%"
android:toXDelta="0"
android:duration="1000" />
效果如下
2. 代码实现
LayoutAnimationController
可以实现组件动画。
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_in_from_right);
LayoutAnimationController controller = new LayoutAnimationController(animation);
controller.setDelay(0.1f);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
ViewGroup container = findViewById(R.id.container);
container.setLayoutAnimation(controller);
container.startLayoutAnimation();
3. 指定播放顺序
自定义LayoutAnimationController
,并覆盖getTransformedIndex(AnimationParameters)
方法,同时指定自定义顺序setOrder(-1)
。
private static class CustomLayoutAnimationController extends LayoutAnimationController {
CustomLayoutAnimationController(Animation animation) {
super(animation);
}
@Override
protected int getTransformedIndex(AnimationParameters params) {
if (getOrder() < 0) {
int index = params.index;
int count = (params.count + 1) / 2;
if (index < count) {
return count - 1 - params.index;
} else {
return params.index - count;
}
} else {
return super.getTransformedIndex(params);
}
}
}
效果如下
相关文章
Animation动画
帧动画
属性动画
组件动画
Transition动画