android 4.0.x上AnimatorSet.setDuration上的坑

<span style="font-size:18px;">最近在用https://github.com/skyfishjy/android-ripple-background开源控件的时候,在Android4.0.x上遇到一个问题,本来优雅的动画变得很快。</span>
<span style="font-size:18px;">刚开始以为是硬件加速的问题,但是查资料后,发现google在4.0的时候就默认的把硬件加速打开了,所以之上的版本都应该一样的。后来和同事一起研究的时候发现,好像是animatorSet.setDuration()方法没有效果,又查了源码发现下面的区别:</span>

     * each child animation will use its own duration. If the duration is set on the AnimatorSet,
     * then each child animation inherits this duration.
     *
     * @param duration The length of the animation, in milliseconds, of each of the child
     * animations of this AnimatorSet.
     */
    @Override
    public AnimatorSet setDuration(long duration) {
        if (duration < 0) {
            throw new IllegalArgumentException("duration must be a value of zero or greater");
        }
        // Just record the value for now - it will be used later when the AnimatorSet starts
        mDuration = duration;
        return this;
    }

API 19

    /**
     * Sets the length of each of the current child animations of this AnimatorSet. By default,
     * each child animation will use its own duration. If the duration is set on the AnimatorSet,
     * then each child animation inherits this duration.
     *
     * @param duration The length of the animation, in milliseconds, of each of the child
     * animations of this AnimatorSet.
     */
    @Override
    public AnimatorSet setDuration(long duration) {
        if (duration < 0) {
            throw new IllegalArgumentException("duration must be a value of zero or greater");
        }
        for (Node node : mNodes) {
            // TODO: don't set the duration of the timing-only nodes created by AnimatorSet to
            // insert "play-after" delays
            node.animation.setDuration(duration);
        }
        mDuration = duration;
        return this;
    }


原来是在API15上AnimatorSet.setDuration()方法并没有给它的子objectAnimator设置,解决方案当然是给每个add到animatorSet中的ObjectAnimator都设置自己的duration。

这是最典型的Android版本兼容问题,踩过坑了就不要再犯。

看看上面的黑底就是去不了。。。真是给CSDN的垃圾编译器跪下了!!!!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值