Android 组件动画

1. 组件动画

设置ViewGrouplayoutAnimation属性,指定组件动画配置。

android:layoutAnimation="@anim/anim_layout_animation"

配置属性

  • delay,动画播放延迟时间
  • animationOrder,子控件播放动画顺序。animationOrder的值为normalreverserandom
  • 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动画

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值