android 4.0.x上AnimatorSet.setDuration上的坑

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


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。