Android中的屬性動畫(Property Animation)——Android開發藝術探索筆記

來源:互聯網
上載者:User

Android中的屬性動畫(Property Animation)——Android開發藝術探索筆記

Animator最早出現在Android 3.0 中,和之前的Animation架構相比,Animator更加的靈活並且具有更多的功能,官方推薦使用Animator代替Animation。在3.0之前可以使用nineoldandroids來實現相同的效果。

使用Animator前需要先瞭解幾個概念:

Duration:動畫播放時間 Time interpolation:屬性值隨著時間的改變情況,比如線性增長或者先快後慢 Repeat count:動畫重複播放次數 Animator sets:動畫集,可以使多個動畫同時播放或者順序播放 Frame refresh delay:動畫每一幀的重新整理時間,一般預設10ms重新整理一次Property Animation

Property Animation非常強大,他可以讓你幾乎在任何東西上播放動畫。Property Animation的結構如:

ValueAnimator用來跟蹤動畫啟動並執行時間和屬性的值。其中TimeInterpolator指定了動畫的 interpolation,如AccelerateDecelerateInterpolator。TypeEvaluator指定屬性的值如何計算比如IntEvaluator.

樣本

點擊一個Button,Button的寬和高分別增加到500dp和400dp。

Property Animation與View Animation的區別View Animation只能對View添加動畫 View Animation只能改變如scale、rotation等值,不能改變background color等屬性 View Animation改變的只是View畫的位置,並不是真正的View,比如一個button從左至右移動,觸發onClick方法的位置還是初試的位置。 View Animation實現起來比Property Animation簡單Evaluator

以IntEvaluator為例,源碼如下:

   public Integer evaluate(float fraction, Integer startValue, Integer endValue) {        int startInt = startValue;        return (int)(startInt + fraction * (endValue - startInt));    }

很簡單,就是根據初始值,結束值和目前時間與總時間長度的比例這三個值計算出當前某屬性應該的值。

Animator

Animator提供了建立動畫的基本結構,通常我們不直接使用它,而是使用它的子類。

ValueAnimator

使用ofInt(), ofFloat(), 或者 ofObject()方法來獲得ValueAnimator執行個體

ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f);animation.setDuration(1000);animation.start();

當然也可以自訂類型

ValueAnimator animation = ValueAnimator.ofObject(new MyTypeEvaluator(), startPropertyValue, endPropertyValue);animation.setDuration(1000);animation.start();
樣本動畫的源碼
  button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                final IntEvaluator mEvaluator = new IntEvaluator();                ValueAnimator valueAnimator = ValueAnimator.ofInt(1, 100);                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {                    @Override                    public void onAnimationUpdate(ValueAnimator valueAnimator) {                        float fraction = valueAnimator.getAnimatedFraction();                        button.getLayoutParams().width = mEvaluator.evaluate(fraction, button.getWidth(), 500);                        button.getLayoutParams().height = mEvaluator.evaluate(fraction, button.getHeight(), 400);                        button.requestLayout();                    }                });                valueAnimator.setDuration(1000).start();            }        });

註:別忘了 button.requestLayout()和valueAnimator.setDuration(1000).start()。

ObjectAnimator

ObjectAnimator是ValueAnimator 的子類。可以直接對目標屬性計算。
對foo這個對象的alpha屬性做從0到1的變化,代碼如下:

ObjectAnimator anim = ObjectAnimator.ofFloat(foo, "alpha", 0f, 1f);anim.setDuration(1000);anim.start();

為了使ObjectAnimator正常運行,還需要如下步驟:

要修改的屬性必須有set方法,如setFoo() 如果你在values…參數中只指定了一個參數,預設為這是最後一個參數。參數必須有get方法,如getFoo() 有些屬性需要手動重新整理,所以要在onAnimationUpdate() 中調用invalidate()。

如果沒有要修改的屬性必須有set方法,有如下三個解決辦法:

如果有許可權,添加set方法
但是很多情況下我們是沒有的… 使用這個類的wrapper class(封裝類)
ObjectAnimator.ofFloat(wrapper, “alpha”, 0f, 1f),在這個封裝類的setAlph中對原來View的alpha屬性值變更 使用ValueAnimatorAnimatorSet

一個Set中包含多個動畫,使用起來也很方便,直接上代碼。

AnimatorSet bouncer = new AnimatorSet();bouncer.play(bounceAnim).before(squashAnim1);bouncer.play(squashAnim1).with(squashAnim2);bouncer.play(squashAnim1).with(stretchAnim1);bouncer.play(squashAnim1).with(stretchAnim2);bouncer.play(bounceBackAnim).after(stretchAnim2);ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);fadeAnim.setDuration(250);AnimatorSet animatorSet = new AnimatorSet();animatorSet.play(bouncer).before(fadeAnim);animatorSet.start();
在XML中聲明動畫

建立res/animator/目錄
每種Animator對應的標籤

ValueAnimator - ObjectAnimator - AnimatorSet -
<code class="language-html hljs "><set android:ordering="sequentially">    <set>        <cke:objectanimator android:propertyname="x" android:duration="500" android:valueto="400" android:valuetype="intType">        <cke:objectanimator android:propertyname="y" android:duration="500" android:valueto="300" android:valuetype="intType">    </cke:objectanimator></cke:objectanimator></set>    <cke:objectanimator android:propertyname="alpha" android:duration="500" android:valueto="1f"></cke:objectanimator></set></code>

在Activity中調用

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,    R.anim.property_animator);set.setTarget(myObject);set.start();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.