码迷,mamicode.com
首页 > 移动开发 > 详细

android 4.0.x上AnimatorSet.setDuration上的坑

时间:2015-01-11 21:44:37      阅读:436      评论:0      收藏:0      [点我收藏+]

标签:

<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版本兼容问题,踩过坑了就不要再犯。


android 4.0.x上AnimatorSet.setDuration上的坑

标签:

原文地址:http://blog.csdn.net/mobilexu/article/details/42582881

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!