Android Animation 高手必讀 之一 Tweened Animations 代碼實現

來源:互聯網
上載者:User

Android提供了兩種動畫的機制,可以通過SurfaceView來一幀一幀的繪製,同樣也可以通過Animation機制。

Animations分類

Animations分為兩種機制:Tweened Animations和Frame-by-FrameAnimations。

Tweened類似於flash,通過旋轉、移動、縮放等實現動畫效果,而Frame-by-FrameAnimations是類似於電影,通過改變每一幀的顯示來實現動畫效果。

其實Animation就是先寫好對象要發生的動作,比如說平移,變形等。然後你需要哪個View或者View的子類,來完成這些預定義好的動作,只要給他們添加,就可以了。這部分的內容還是蠻多的,但是相對簡單,本節只介紹Tweened Animations。

Tweened Animations

1所示,Tweened Animations有如下四種方式實現動畫,對於每一種方式,Android都提供了一個類來實現。[url=]AlphaAnimation[/url], [url=]AnimationSet[/url], [url=]RotateAnimation[/url], [url=]ScaleAnimation[/url], [url=]TranslateAnimation[/url] 。

2011-9-30 12:35:17 上傳

下載附件
(6.66 KB)

圖1 Tweened Animations四種方式實現動畫

對於每一種,筆者都會給出例子來詳細解釋。出於由簡到難的考慮,首先介紹Alpha。

Alpha

alpha這個詞詳細如果有過opengl 開發經驗的朋友一定不會陌生,沒錯,他就是設定透明度的。讓我們先看一組表徵圖,這樣你就會加深對alpha的認識。2所示,實現了透明度變化的表徵圖和按鈕的效果。

2011-9-30 12:35:13 上傳

下載附件
(17.33 KB)

圖2 色彩坡形demo

如果要實現view的透明度變化,就要去實現alpha 的Animation。具體方法分為如下4部分:

q      執行個體化AnimationSet對象

q      執行個體化Animation對象

q      設定Animation對象屬性

q      為View添加AnimationSet

這裡要強調的就是Animation的構造方法如下:

AlphaAnimation aa = new AlphaAnimation(1, 0);//2.建立需要的animation

相對來說,alpha的構造方法最為簡單。其第一個參數是出示時的透明度,0為完全透明,1為不透明。第二個參數為動畫結束時的透明度。

aa.setDuration(2000);//設定動畫的時間

通過上面這句可以設定動畫播放的時間。

具體代碼我會在最後給出。

Scale

scale為縮放,在瞭解Scale之前我們先看看其運行效果。3所示。

2011-9-30 12:35:14 上傳

下載附件
(30.44 KB)

圖3 scale demo

以上是Scale最簡單的構造方法:

ScaleAnimation aa = new ScaleAnimation(1, 2, 1, 2);//2.建立需要的animation,這種構造方法是以自身為pivot

q      第1個參數為初始時x的大小,其中1為自身長度,同一2為自身長度的2倍,下同

q      第2個參數為結束動畫時x的大小

q      第3個參數為初始時y的大小

q      第4個參數為結束動畫時y的大小

如果你以為你已經學會了Scale,那我只能說你是淺嘗輒止。下面介紹其另一個構造方法,該方法更加靈活。

ScaleAnimation aa = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);

q      其四個參數同上,不解釋

q      第五個參數是設定樞軸,可以是以自身,亦可以使用父view的,甚至可以誰用絕對數值。這裡之後會解釋。如果不明白可先忽略。

q      第六個參數是設定x樞軸的座標。

q      接下來的兩個參數同上,是設定y座標的。

也許這裡讀者肯定不會明白樞軸的意思,那麼我就用上面的方法來構造Animation並運行看看效果。請看圖4.

2011-9-30 12:35:15 上傳

下載附件
(47.83 KB)

圖4 scale Demo2

,View控制項在自身大小變化的同時,位置也在改變。後面的幾個參數是設定其初始位置。如果以父view為參考,值為0.5f的話,那麼就是當前父View的中間。

讀者可能會問到,怎麼不是螢幕的中心位置,我認為他是根據布局檔案來決定位置的,當前的布局檔案為LinearLayout,其設定為向下一次新增內容,如果設定y座標為0.5f,則Android會認為你是要在當前布局接下來新增內容的位置的y軸中間。

Transfer

如果明白了樞軸的概念,那麼Transfer平移就很簡單了,5所示,為平移的例子。

2011-9-30 12:35:16 上傳

下載附件
(32.76 KB)

圖5 平移 demo

以上平移的執行個體化方法如下:

TranslateAnimation aa = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1);

讀者結合api,相信理解這個方法應該還是比較容易的,這裡就不在贅述,如果有問題,可以給留言。

Rotate

旋轉,這個也是比較容易,看示範,6所示。

2011-9-30 12:35:16 上傳

下載附件
(35.43 KB)

圖6 旋轉demo

其構造方法如下:

RotateAnimation aa = new RotateAnimation(0, 270, Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 0.2f);

綜合

Animation不但可以單獨使用,還可以設定多個Animation然後添加到AnimationSet中。在對View調用,在例子中我給出了一個綜合的例子,讀者自行查看,檢測自己是否已經明白這塊知識。

代碼

本節參考AnimationDemo1例子。

限於本人水平有限,如有不足之處,請多多指正。

Java代碼

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  3.                 android:layout_width="fill_parent"
  4.                 android:layout_height="wrap_content"
  5.         >
  6.         <LinearLayout
  7.             android:orientation="vertical"
  8.             android:layout_width="fill_parent"
  9.             android:layout_height="fill_parent"
  10.             >
  11.                 <Button
  12.                         android:text="alpha"
  13.                         android:id="@+id/Button01"
  14.                         android:layout_width="wrap_content"
  15.                         android:layout_height="wrap_content">
  16.                        
  17.                 </Button>
  18.                 <ImageView
  19.                         android:id="@+id/iv1"
  20.                         android:layout_width="wrap_content"
  21.                         android:layout_height="wrap_content"
  22.                         android:src = "@drawable/icon"
  23.                 >
  24.                 </ImageView>
  25.                
  26.                 <Button
  27.                         android:text="scale"
  28.                         android:id="@+id/Button02"
  29.                         android:layout_width="wrap_content"
  30.                         android:layout_height="wrap_content">
  31.                        
  32.                 </Button>
  33.                 <ImageView
  34.                         android:id="@+id/iv2"
  35.                         android:layout_width="wrap_content"
  36.                         android:layout_height="wrap_content"
  37.                         android:src = "@drawable/icon"
  38.                 >
  39.                 </ImageView>
  40.                
  41.                 <Button
  42.                         android:text="rotate"
  43.                         android:id="@+id/Button03"
  44.                         android:layout_width="wrap_content"
  45.                         android:layout_height="wrap_content">
  46.                        
  47.                 </Button>
  48.                 <ImageView
  49.                         android:id="@+id/iv3"
  50.                         android:layout_width="wrap_content"
  51.                         android:layout_height="wrap_content"
  52.                         android:src = "@drawable/icon"
  53.                 >
  54.                 </ImageView>
  55.                
  56.                 <Button
  57.                         android:text="transf"
  58.                         android:id="@+id/Button04"
  59.                         android:layout_width="wrap_content"
  60.                         android:layout_height="wrap_content">
  61.                        
  62.                 </Button>
  63.                 <ImageView
  64.                         android:id="@+id/iv4"
  65.                         android:layout_width="wrap_content"
  66.                         android:layout_height="wrap_content"
  67.                         android:src = "@drawable/icon"
  68.                 >
  69.                 </ImageView>
  70.                
  71.                 <Button
  72.                         android:text="complex"
  73.                         android:id="@+id/Button05"
  74.                         android:layout_width="wrap_content"
  75.                         android:layout_height="wrap_content">
  76.                        
  77.                 </Button>
  78.                 <ImageView
  79.                         android:id="@+id/iv5"
  80.                         android:layout_width="wrap_content"
  81.                         android:layout_height="wrap_content"
  82.                         android:src = "@drawable/icon"
  83.                 >
  84.                 </ImageView>
  85.         </LinearLayout>                                                                       
  86. </ScrollView>

複製代碼

Java代碼

  1. package cn.edu.heut.zcl;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.view.animation.AlphaAnimation;
  7. import android.view.animation.Animation;
  8. import android.view.animation.AnimationSet;
  9. import android.view.animation.RotateAnimation;
  10. import android.view.animation.ScaleAnimation;
  11. import android.view.animation.TranslateAnimation;
  12. import android.widget.Button;
  13. import android.widget.ImageButton;
  14. import android.widget.ImageView;
  15. public class DemoActivity extends Activity implements OnClickListener{
  16.     /** Called when the activity is first created. */
  17.         Button alphaBut ;
  18.         Button scaleBut ;
  19.         Button rotateBut ;
  20.         Button transfBut ;
  21.         Button complexBut ;
  22.        
  23.         ImageView iv1;
  24.         ImageView iv2;
  25.         ImageView iv3;
  26.         ImageView iv4;
  27.         ImageView iv5;
  28.        
  29.         AnimationSet as ;
  30.     @Override
  31.     public void onCreate(Bundle savedInstanceState) {
  32.         super.onCreate(savedInstanceState);
  33.         setContentView(R.layout.main);
  34.         alphaBut = (Button)findViewById(R.id.Button01);
  35.         scaleBut = (Button)findViewById(R.id.Button02);
  36.         rotateBut = (Button)findViewById(R.id.Button03);
  37.         transfBut = (Button)findViewById(R.id.Button04);
  38.         complexBut = (Button)findViewById(R.id.Button05);
  39.         
  40.         alphaBut.setOnClickListener(this);
  41.         scaleBut.setOnClickListener(this);
  42.         rotateBut.setOnClickListener(this);
  43.         transfBut.setOnClickListener(this);
  44.         complexBut.setOnClickListener(this);
  45.         
  46.         iv1 = (ImageView)findViewById(R.id.iv1);
  47.         iv2 = (ImageView)findViewById(R.id.iv2);
  48.         iv3 = (ImageView)findViewById(R.id.iv3);
  49.         iv4 = (ImageView)findViewById(R.id.iv4);
  50.         iv5 = (ImageView)findViewById(R.id.iv5);
  51.       
  52.     }
  53.         @Override
  54.         public void onClick(View v) {
  55.                 Button b = (Button)v;
  56.                 switch(b.getId()){
  57.                 case R.id.Button01://alpha
  58.                         alphaAnimation();
  59.                         break;
  60.                 case R.id.Button02://scale
  61.                         scaleAnimation();
  62.                         break;
  63.                 case R.id.Button03://rotate
  64.                         rotateAnimation();
  65.                         break;
  66.                 case R.id.Button04 ://transf
  67.                         transfAnimation();
  68.                         break;
  69.                 case R.id.Button05 ://complex
  70.                         complexAnimation();
  71.                         break;
  72.                 }
  73.                
  74.         }
  75.         /**
  76.          * 綜合例子
  77.          */
  78.         private void complexAnimation() {
  79.                 as = new AnimationSet(true);//1.執行個體化AnimationSet
  80.                
  81.                 TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0);
  82.                 ta.setDuration(5000);//設定動畫的時間
  83.                
  84.                 AlphaAnimation aa = new AlphaAnimation(1, 0.3f);//2.建立需要的animation
  85.                 aa.setDuration(3000);
  86.                
  87.                 as.addAnimation(ta);//3.將animation加入AnimationSet
  88.                 as.addAnimation(aa);//3.將animation加入AnimationSet
  89.                
  90.                
  91.                 as.setFillAfter(true);//最終停止
  92.                
  93.                 complexBut.startAnimation(as);//4.開始動畫
  94.                 iv5.startAnimation(as);//開始動畫
  95.         }
  96.         /**
  97.          * 平移
  98.          */
  99.         private void transfAnimation() {
  100.                 as = new AnimationSet(true);//1.執行個體化AnimationSet
  101.                 TranslateAnimation aa = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 1);
  102.                 aa.setDuration(5000);//設定動畫的時間
  103.                 as.addAnimation(aa);//3.將animation加入AnimationSet
  104.                 transfBut.startAnimation(as);//4.開始動畫
  105.                 iv4.startAnimation(as);//開始動畫
  106.         }
  107.         /**
  108.          * 旋轉
  109.          */
  110.         private void rotateAnimation() {
  111.                 as = new AnimationSet(true);//1.執行個體化AnimationSet
  112.                 RotateAnimation aa = new RotateAnimation(0, 270, Animation.RELATIVE_TO_PARENT, 0.2f, Animation.RELATIVE_TO_PARENT, 0.2f);
  113.                 aa.setDuration(5000);//設定動畫的時間
  114.                 as.addAnimation(aa);//3.將animation加入AnimationSet
  115.                 rotateBut.startAnimation(as);//4.開始動畫
  116.                 iv3.startAnimation(as);//開始動畫
  117.         }
  118.         /**
  119.          * 變化大小
  120.          */
  121.         private void scaleAnimation() {
  122.                 as = new AnimationSet(true);//1.執行個體化AnimationSet
  123. //                ScaleAnimation aa = new ScaleAnimation(1, 2, 1, 2);//2.建立需要的animation,這種構造方法是以自身為pivot
  124.                 ScaleAnimation aa = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
  125.                 aa.setDuration(7000);//設定動畫的時間
  126.                 as.addAnimation(aa);//3.將animation加入AnimationSet
  127.                 scaleBut.startAnimation(as);//4.開始動畫
  128.                 iv2.startAnimation(as);//開始動畫
  129.         }
  130.         /**
  131.          * 透明度漸層
  132.          */
  133.         private void alphaAnimation(){
  134.                 as = new AnimationSet(true);//1.執行個體化AnimationSet
  135.                 AlphaAnimation aa = new AlphaAnimation(1, 0);//2.建立需要的animation
  136.                 aa.setDuration(2000);//設定動畫的時間
  137.                 as.addAnimation(aa);//3.將animation加入AnimationSet
  138.                 alphaBut.startAnimation(as);//4.開始動畫
  139.                 iv1.startAnimation(as);//開始動畫
  140.         }
  141.    
  142. }

複製代碼

相關文章

聯繫我們

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