android 4.0.x上AnimatorSet.setDuration上的坑

來源:互聯網
上載者:User

標籤:

<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上的坑

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.