天天看點

詳解Android動畫之Tween Animation

前面講了動畫中的Frame動畫,今天就來詳細講解一下Tween動畫的使用。

同樣,在開始執行個體示範之前,先引用官方文檔中的一段話:

Tween動畫是操作某個控件讓其展現出旋轉、漸變、移動、縮放的這麼一種轉換過程,我們成為補間動畫。我們可以以XML形式定義動畫,也可以編碼實作。

如果以XML形式定義一個動畫,我們按照動畫的定義文法完成XML,并放置于/res/anim目錄下,檔案名可以作為資源ID被引用;如果由編碼實作,我們需要使用到Animation對象。

如果用定義XML方式實作動畫,我們需要熟悉一下動畫XML文法:

[html] view plaincopy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:interpolator="@[package:]anim/interpolator_resource"  
  4.     android:shareInterpolator=["true" | "false"] >  
  5.     <alpha  
  6.         android:fromAlpha="float"  
  7.         android:toAlpha="float" />  
  8.     <scale  
  9.         android:fromXScale="float"  
  10.         android:toXScale="float"  
  11.         android:fromYScale="float"  
  12.         android:toYScale="float"  
  13.         android:pivotX="float"  
  14.         android:pivotY="float" />  
  15.     <translate  
  16.         android:fromX="float"  
  17.         android:toX="float"  
  18.         android:fromY="float"  
  19.         android:toY="float" />  
  20.     <rotate  
  21.         android:fromDegrees="float"  
  22.         android:toDegrees="float"  
  23.     <set>  
  24.         ...  
  25.     </set>  
  26. </set>  

XML檔案中必須有一個根元素,可以是<alpha>、<scale>、<translate>、<rotate>中的任意一個,也可以是<set>來管理一個由前面幾個元素組成的動畫集合。

<set>是一個動畫容器,管理多個動畫的群組,與之相對應的Java對象是AnimationSet。它有兩個屬性,android:interpolator代表一個插值器資源,可以引用系統自帶插值器資源,也可以用自定義插值器資源,預設值是勻速插值器;稍後我

會對插值器做出詳細講解。android:shareInterpolator代表<set>裡面的多個動畫是否要共享插值器,預設值為

true,即共享插值器,如果設定為false,那麼<set>的插值器就不再起作用,我們要在每個動畫中加入插值器。

<alpha>是漸變動畫,可以實作fadeIn和fadeOut的效果,與之對應的Java對象是AlphaAnimation。android:fromAlpha屬性代表起始alpha值,浮點值,範圍在0.0和1.0之間,分别代表透明和完全不透

明,android:toAlpha屬性代表結尾alpha值,浮點值,範圍也在0.0和1.0之間。

<scale>是縮放動畫,可以實作動态調控件尺寸的效果,與之對應的Java對象是ScaleAnimation。android:fromXScale屬性代表起始的X方向上相對自身的縮放比例,浮點值,比如1.0代表自身無變化,0.5代表起始時縮小一倍,2.0

代表放大一倍;android:toXScale屬性代表結尾的X方向上相對自身的縮放比例,浮點值;android:fromYScale屬性代表起始

的Y方向上相對自身的縮放比例,浮點值;android:toYScale屬性代表結尾的Y方向上相對自身的縮放比例,浮點

值;android:pivotX屬性代表縮放的中軸點X坐标,浮點值,android:pivotY屬性代表縮放的中軸點Y坐标,浮點值,對于這兩個屬

性,如果我們想表示中軸點為圖像的中心,我們可以把兩個屬性值定義成0.5或者50%。

<translate>是位移動畫,代表一個水準、垂直的位移。與之對應的Java對象是TranslateAnimation。android:fromXDelta屬性代表起始X方向的位置,android:toXDelta代表結尾X方向上的位

置,android:fromYScale屬性代表起始Y方向上的位置,android:toYDelta屬性代表結尾Y方向上的位置,以上四個屬性都支

持三種表示方式:浮點數、num%、num%p;如果以浮點數字表示,代表相對自身原始位置的像素值;如果以num%表示,代表相對于自己的百分比,比如

toXDelta定義為100%就表示在X方向上移動自己的1倍距離;如果以num%p表示,代表相對于父類元件的百分比。

<rotate>是旋轉動畫,與之對應的Java對象是RotateAnimation。android:fromDegrees屬性代表起始角度,浮點值,機關:度;android:toDegrees屬性代表結尾角度,浮點值,機關:度;android:pivotX屬性代表旋轉中

心的X坐标值,android:pivotY屬性代表旋轉中心的Y坐标值,這兩個屬性也有三種表示方式,數字方式代表相對于自身左邊緣的像素值,num%

方式代表相對于自身左邊緣或頂邊緣的百分比,num%p方式代表相對于父容器的左邊緣或頂邊緣的百分比。

另外,在動畫中,如果我們添加了android:fillAfter="true"後,這個動畫執行完之後保持最後的狀态;android:duration="integer"代表動畫持續的時間,機關為毫秒。

如果要把定義在XML中的動畫應用在一個ImageView上,代碼是這樣的:

[java] view plaincopy

  1. ImageView image = (ImageView) findViewById(R.id.image);  
  2. Animation testAnim = AnimationUtils.loadAnimation(this, R.anim.test);  
  3. image.startAnimation(testAnim);  

下面重點介紹一下插值器的概念:

首先要了解為什麼需要插值器,因為在補間動畫中,我們一般隻定義關鍵幀(首幀或尾幀),然後由系統自動生成中間幀,生成中間幀的這個過程可以成為“插值”。插值器定義了動畫變化的速率,提供不同的函數定義變化值相對于時間的變化規則,可以定義各種各樣的非線性變化函數,比如加速、減速等。下面是幾

種常見的插值器:

Interpolator對象 資源ID 功能作用
AccelerateDecelerateInterpolator @android:anim/accelerate_decelerate_interpolator 先加速再減速
AccelerateInterpolator @android:anim/accelerate_interpolator 加速
AnticipateInterpolator @android:anim/anticipate_interpolator 先回退一小步然後加速前進
AnticipateOvershootInterpolator @android:anim/anticipate_overshoot_interpolator 在上一個基礎上超出終點一小步再回到終點
BounceInterpolator @android:anim/bounce_interpolator 最後階段彈球效果
CycleInterpolator @android:anim/cycle_interpolator 周期運動
DecelerateInterpolator @android:anim/decelerate_interpolator 減速
LinearInterpolator @android:anim/linear_interpolator 勻速
OvershootInterpolator @android:anim/overshoot_interpolator 快速到達終點并超出一小步最後回到終點

然後我們可以這樣使用一個插值器:

  1. <set android:interpolator="@android:anim/accelerate_interpolator">  
  2. ...  
  1. <alpha android:interpolator="@android:anim/accelerate_interpolator"  
  2.     .../>  

如果隻簡單地引用這些插值器還不能滿足需要的話,我們要考慮一下個性化插值器。我們可以建立一個插值器資源修改插值器的屬性,比如修改AnticipateInterpolator的加速速率,調整CycleInterpolator的循環次數等。為了完成這種需求,我們需要建立XML

資源檔案,然後将其放于/res/anim下,然後再動畫元素中引用即可。我們先來看一下幾種常見的插值器可調整的屬性:

<accelerateDecelerateInterpolator> 無

<accelerateInterpolator> android:factor 浮點值,加速速率,預設為1

<anticipateInterploator> android:tension 浮點值,起始點後退的張力、拉力數,預設為2

<anticipateOvershootInterpolator> android:tension 同上 android:extraTension 浮點值,拉力的倍數,預設為1.5(2  * 1.5)

<bounceInterpolator> 無

<cycleInterplolator> android:cycles 整數值,循環的個數,預設為1

<decelerateInterpolator> android:factor 浮點值,減速的速率,預設為1

<linearInterpolator> 無

<overshootInterpolator> 浮點值,超出終點後的張力、拉力,預設為2

下面我們就拿最後一個插值器來舉例:

  1. <overshootInterpolator xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:tension="7.0"/>  

上面的代碼中,我們把張力改為7.0,然後将此檔案命名為my_overshoot_interpolator.xml,放置于/res/anim下,我們就可以引用到自定義的插值器了:

  1. <scale xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:interpolator="@anim/my_overshoot_interpolator"  

如果以上這些簡單的定義還不能滿足我們的需求,那麼我們就需要考慮一下自己定義插值器類了。

我們可以實作Interpolator接口,因為上面所有的Interpolator都實作了Interpolator接口,這個接口定義了一個方法:float getInterpolation(float input);

此方法由系統調用,input代表動畫的時間,在0和1之間,也就是開始和結束之間。

線性(勻速)插值器定義如下:

  1. public float getInterpolation(float input) {  
  2.     return input;  
  3. }  

加速減速插值器定義如下:

  1.     return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;  

有興趣的話,大家可以嘗試一下自定義一個插值器。

講了這麼久的概念,下面我們就結合執行個體來示範一下幾種Tween動畫的應用。

先來介紹一下旋轉動畫的使用,布局檔案/res/layout/rotate.xml如下:

  1. <LinearLayout  
  2.   xmlns:android="http://schemas.android.com/apk/res/android"  
  3.   android:orientation="vertical"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent"  
  6.   android:background="#FFFFFF">  
  7.   <ImageView  
  8.         android:id="@+id/piechart"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_gravity="center_horizontal"  
  12.         android:src="@drawable/piechart"/>  
  13.   <Button  
  14.         android:id="@+id/positive"  
  15.         android:layout_width="fill_parent"  
  16.         android:text="順時針"  
  17.         android:onClick="positive"/>  
  18.         android:id="@+id/negative"  
  19.         android:text="逆時針"  
  20.         android:onClick="negative"/>       
  21. </LinearLayout>  

我們定義了一個ImageView,用于顯示一個餅狀圖,示範旋轉動畫,然後定義了兩個按鈕,用以運作編碼實作的動畫。動畫定義檔案/res/anim/rotate.xml如下:

  1.     android:interpolator="@android:anim/accelerate_decelerate_interpolator">  
  2.         android:fromDegrees="0"  
  3.         android:toDegrees="+360"  
  4.         android:pivotX="50%"  
  5.         android:pivotY="50%"  
  6.         android:duration="5000"/>  

最後再來看一下RotateActivity.java代碼:

  1. package com.scott.anim;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.view.animation.Animation;  
  6. import android.view.animation.AnimationUtils;  
  7. import android.view.animation.LinearInterpolator;  
  8. import android.view.animation.RotateAnimation;  
  9. public class RotateActivity extends Activity {  
  10.     private int currAngle;  
  11.     private View piechart;  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.rotate);  
  16.         piechart = findViewById(R.id.piechart);  
  17.         Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);  
  18.         piechart.startAnimation(animation);  
  19.     }  
  20.     public void positive(View v) {  
  21.         Animation anim = new RotateAnimation(currAngle, currAngle + 180, Animation.RELATIVE_TO_SELF, 0.5f,  
  22.                 Animation.RELATIVE_TO_SELF, 0.5f);  
  23.         /** 勻速插值器 */  
  24.         LinearInterpolator lir = new LinearInterpolator();  
  25.         anim.setInterpolator(lir);  
  26.         anim.setDuration(1000);  
  27.         /** 動畫完成後不恢複原狀 */  
  28.         anim.setFillAfter(true);  
  29.         currAngle += 180;  
  30.         if (currAngle > 360) {  
  31.             currAngle = currAngle - 360;  
  32.         }  
  33.         piechart.startAnimation(anim);  
  34.     public void negative(View v) {  
  35.         Animation anim = new RotateAnimation(currAngle, currAngle - 180, Animation.RELATIVE_TO_SELF, 0.5f,  
  36.         currAngle -= 180;  
  37.         if (currAngle < -360) {  
  38.             currAngle = currAngle + 360;  

然後,看一下漸變動畫,布局檔案/res/layout/alpha.xml如下:

  1. <FrameLayout  
  2.       android:id="@+id/splash"  
  3.       android:layout_width="fill_parent"  
  4.       android:layout_height="fill_parent"  
  5.       android:layout_gravity="center"  
  6.       android:src="@drawable/splash"/>  
  7.       android:layout_height="wrap_content"  
  8.       android:layout_gravity="bottom"  
  9.       android:text="alpha"  
  10.       android:onClick="alpha"/>    
  11. </FrameLayout>  

動畫定義檔案/res/anim/alpha.xml如下:

  1. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  2.         android:fromAlpha="0.0"  
  3.         android:toAlpha="1.0"  
  4.         android:duration="3000"/>  

AlphaActivity.java代碼如下:

  1. import android.util.Log;  
  2. import android.view.animation.AlphaAnimation;  
  3. import android.view.animation.Animation.AnimationListener;  
  4. import android.widget.ImageView;  
  5. public class AlphaActivity extends Activity implements AnimationListener {  
  6.     private ImageView splash;  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         setContentView(R.layout.alpha);  
  9.         splash = (ImageView) findViewById(R.id.splash);  
  10.         Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);  
  11.         anim.setAnimationListener(this);  
  12.         splash.startAnimation(anim);  
  13.     public void alpha(View view) {  
  14.         Animation anim = new AlphaAnimation(1.0f, 0.0f);  
  15.         anim.setDuration(3000);  
  16.     public void onAnimationStart(Animation animation) {  
  17.         Log.i("alpha", "onAnimationStart called.");  
  18.     public void onAnimationEnd(Animation animation) {  
  19.         Log.i("alpha", "onAnimationEnd called");  
  20.     public void onAnimationRepeat(Animation animation) {  
  21.         Log.i("alpha", "onAnimationRepeat called");  

接着看一下位移動畫,布局檔案/res/layout/translate.xml如下:

  1.   android:layout_height="fill_parent">  
  2.     android:id="@+id/trans_image"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:layout_weight="1"  
  6.     android:src="@drawable/person"/>  
  7.     android:id="@+id/trans_button"  
  8.     android:layout_width="fill_parent"  
  9.     android:layout_gravity="bottom"  
  10.     android:text="translate"  
  11.     android:onClick="translate"/>  

動畫定義檔案/res/anim/translate.xml如下:

  1.     android:interpolator="@android:anim/bounce_interpolator">  
  2.         android:fromXDelta="0"  
  3.         android:fromYDelta="0"  
  4.         android:toXDelta="200"  
  5.         android:toYDelta="300"  
  6.         android:duration="2000"/>  

然後TranslateActivity.java代碼如下:

  1. import android.view.animation.OvershootInterpolator;  
  2. import android.view.animation.TranslateAnimation;  
  3. public class TranslateActivity extends Activity {  
  4.     private ImageView trans_iamge;  
  5.         setContentView(R.layout.tanslate);  
  6.         trans_iamge = (ImageView) findViewById(R.id.trans_image);  
  7.         Animation anim = AnimationUtils.loadAnimation(this, R.anim.translate);  
  8.         trans_iamge.startAnimation(anim);  
  9.     public void translate(View view) {  
  10.         Animation anim = new TranslateAnimation(200, 0, 300, 0);  
  11.         anim.setDuration(2000);  
  12.         OvershootInterpolator overshoot = new OvershootInterpolator();  
  13.         anim.setInterpolator(overshoot);  

最後,我們再來看以下縮放動畫,布局檔案/res/layout/scale.xml如下:

  1.     android:id="@+id/scale_image"  
  2.     android:layout_gravity="center"  
  3.     android:id="@+id/scale_button"  
  4.     android:text="scale"  
  5.     android:onClick="sclae"/>  

動畫定義檔案/res/anim/scale.xml如下:

  1.         android:fromXScale="1.0"  
  2.         android:toXScale="2.0"  
  3.         android:fromYScale="1.0"  
  4.         android:toYScale="2.0"  
  5.         android:pivotX="0.5"  

然後ScaleActivity.java代碼如下:

  1. import android.view.animation.BounceInterpolator;  
  2. import android.view.animation.ScaleAnimation;  
  3. public class ScaleActivity extends Activity {  
  4.     private ImageView scale_iamge;  
  5.         setContentView(R.layout.scale);  
  6.         scale_iamge = (ImageView) findViewById(R.id.scale_image);  
  7.         Animation anim = AnimationUtils.loadAnimation(this, R.anim.scale);  
  8.         scale_iamge.startAnimation(anim);  
  9.     public void sclae(View view) {  
  10.         Animation anim = new ScaleAnimation(2.0f, 1.0f, 2.0f, 1.0f,   
  11.                 Animation.RELATIVE_TO_SELF, 0.5f,   
  12.         BounceInterpolator bounce = new BounceInterpolator();  
  13.         anim.setInterpolator(bounce);  

幾種動畫運作效果如下圖所示:

上一篇: bs