Android 基本Animation

來源:互聯網
上載者:User

轉自:http://hi.baidu.com/wendaoeryu/item/203d40efbf86583f5a7cfbcf

一.Animation簡介

1.簡介  Animation為Android提供了一系列的動畫效果:旋轉、縮放、移動、淡入淡出,這些動畫效果可以應用於絕大多數控制項。

2.分類

Animation可分為兩類:

    TweenedAnimations:漸層動畫,具體就是旋轉、縮放、移動、淡入淡出效果。

    Frame-by-FrameAnimations:將一系列Drawable序列一次播放,類似於電影模式,常用來進行定時更新背景等操作。

3.實現方法

 Animation有兩種實現方法:

    在xml檔案中實現,優點是複用性好,可維護性好,多個控制項可以使用同一個xml檔案。缺點是由於xml檔案不進行編譯,所以排錯比較難。

    在代碼中實現,優點是排錯很方便,缺點是重複代碼多,可複用性低。

二.TweenedAnimations

1.效果

      Alpha:淡入淡出

      Scale:縮放

      Rotate:旋轉

      Translate:移動

2.實現

在代碼中實現

(1)建立AnimationSet對象(可以柔和多個動畫效果)

      AnimationSet是Animation的子類,一個AnimationSet包含了一系列的Animation,對AnimationSet屬性的設定將會應用於其中的每一個Animation。

      AnimationSet animationSet=new AnimationSet(true);

(2)根據需要建立相應的Animation對象(根據4種效果有4個子類)

淡入淡出: AlphaAnimation alphaAnimation=new AlphaAnimation(1,0);    //兩個參數分別表示初始透明度和目標透明度,

                                                                                                                        //1表示不透明,0表示完全透明。

旋轉:RotateAnimation rotateAnimation=new RotateAnimation(0,360,Animation.RELATIVE_TO_PARENT,1f,Animation.RELATIVE_TO_PARENT,1f);

參數含義:

參數1:初始角度,0表示圖片當前位置。

參數2:目標角度。

參數3、4、5、6定義了旋轉的圓心

參數3:X座標的類型,有三種預設值:RELATIVE_TO_PARENT 相對於父控制項

                                                            RELATIVE_TO_SELF 相對於符自己

                                                            RELATIVE_TO_ABSOLUTE 絕對座標

參數4:X軸位移比例,變化範圍是0f—1f

參數5:Y座標的類型,同樣也有三種預設值

參數6:Y軸位移比例,變化範圍是0f—1f

縮放:ScaleAnimation scaleAnimation=new ScaleAnimation(1,0.1f,1,0.1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

參數含義:

參數1,2:X軸縮放比例—從1縮放到0.1

參數3,4:Y軸縮放比例—從1縮放到0.1

參數5,6:X軸軸心點(類型和比例)

參數7,8:Y軸軸心點(類型和比例)

移動:TranslateAnimation translateAnimation=new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0.5f);

參數含義:

參數1,2:X軸的開始位置

參數3,4:X軸的結束位置

參數5,6:Y軸的開始位置

參數7,8:Y軸的結束位置

(3)為Animation配置資料

      這裡說明一下TweenedAnimations的幾個通用屬性

            .setDuration(long durationMills);  //動畫執行的時間,單位是毫秒。

             .setFillEnabled——當設定為true時,fillAfter和fillBefroe將會都為true,此時會忽略fillBefore和fillAfter兩種屬性

            .setFillAfter(boolean fillAfter);  //設為true,則動畫完成後停留在結束狀態

            .setFillBefore(boolean fillBefore);  //設為true,則動畫完成後停留在初始狀態

            .setStartOffset(long startOffset);  //動畫執行前的等待時間

            .setRepeatCount(int repeatCount);  //動畫執行重複的次數

(4)將Animation對象加入AnimationSet中

      animationSet.addAnimation(alphaAnimation);

(5)使用控制項對象開始執行AnimationSet

      ***.StartAnimation(animationSet);

在xml中實現

(1)在res目錄下建立anim檔案夾

(2)在anim中建立xml檔案,並首先加入set標籤:

<set xmlns:android=“http://schemas.android.com/apk/res/android”

                  android:interpolator="@android:anim/accelerate_interpolator">

</set>

(3)在set標籤中可加入rotate、alpha、scale或translate等標籤。

例:

   <rotate

              android:fromDegrees="0"       //起始角度

              android:toDegrees="360"      //目的角度,這裡的角度可以寫負數,這時會逆時針旋轉

              android:pivotX="%50"            //X軸的旋轉圓心,50,則對應RELATIVE_TO_ABSOLUTE ,50%,則對

                                                            //應 RELATIVE_TO_SELF ,50%p,則對應RELATIVE_TO_PARENT 。

              android:pivotY="%50"           //Y軸的旋轉圓心

    >

        這些標籤的具體使用可以查閱Android協助文檔。

(4)在代碼中使用AnimationUtils裝載xml,並產生Animation對象

      Animation animation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.***);    //裝載指定的xml檔案

3.關於Interpolator

(1)簡介

      Interpolator定義了動畫的速率,幾種預設的Interpolator如下:

      AccelerateInterpolator:動畫開始時比較慢,然後逐漸加速。

      DecelerateInterpolator:動畫開始時比較快,然後逐漸減速。

      AccelerateDecelerateInterpolator:動畫開始時和結束時比較慢,中間過程加速。

      LinearInterpolator:動畫勻速進行。

      CycleInterpolator:動畫迴圈播放指定次數,速率沿著正弦曲線改變。

(2)實現

定義在代碼中:

      AnimationSet  animationSet =new AnimationSet (true);      //這裡設定為true,則為animationSet 設定的Interpolator將

                                                                                                 //應用於animationSet 中的所有animation對象。 

      animationSet .setInterpolator(new AccelerateInterpolator);//為animationSet 設定Interpolator。

或者:

      AnimationSet  animationSet =new AnimationSet (false);      

      animation1 .setInterpolator(new AccelerateInterpolator);

      animation2.setInterpolator(new AccelerateInterpolator);

      ......          //為每一個animation對象設定Interpolator。

定義在xml中:

在set標籤中添加屬性,

       android:shareInterpolator="true"      //設定為true,則這時設定的Interpolator適用於標籤中的所有動畫。

       android:interpolator="@android:anim/accelerate_interpolator”   //為set設定Interpolator。

或者:

       android:shareInterpolator="false"     //設定為false,則需為每一個動畫添加interpolator屬性。

三.Frame-By-FrameAnimations

1.實現
(1)在drawable檔案夾下建立xml檔案:

      <?xml version="1.0" encoding="utf-8"?>

      <animation-list xmlms:android="http://schemas.android.com/apk/res/android"

                                       android:onshot="false">

      <item android:drawable=""@drawable/pic1 android:duration="500" />

      <item android:drawable=""@drawable/pic2android:duration="500" />

      <item android:drawable=""@drawable/pic3android:duration="500" />

    </animation-list>

     

 這個檔案就定義了需要一次播放的圖片資源,每一個<item>就是一個資源,drawable屬性就是資源圖片,duration指的是播放的時間長度。

(2) 代碼中添加:

      imageView.setBackgroundResource(R.drawable.***);  //將建立的的xml裝載進imageView

      AnimationDrawable animationDrawable=(AnimationDrawable)imageView.getBackground();

      animationDrawable.start();    //啟動動畫

四.LayoutAnimaionController

1.簡介

      LayoutAnimaionController為Layout或者viewGroup裡的控制項設定動畫效果,特點是它會使其中的每個控制項都有相同的動畫效果,這些控制項的動畫效果可以在不同的時間顯示出來。

2.實現(結合ListView使用)

      LayoutAnimaionController同樣即可以在代碼中設定,也可以在xml中設定。

在代碼中:

(1)建立Animation對象,使用建構函式或者裝載xml都可以。

(2)建立LayoutAnimaionController對象

      LayoutAnimaionController lac=new LayoutAnimaionController(animation);

(3)設定屬性

      lac.setOrder(LayoutAnimaionController .ORDER_NORMAL);//設定順序

      有三種預設順序:

            LayoutAnimaionController .ORDER_NORMAL       // 順序

            LayoutAnimaionController .ORDER_REVERSE     //反序

            LayoutAnimaionController .ORDER_RANDOM      //隨機

      lac.setDelay(***f);//設定執行動畫的延遲時間,即時間間隔,單位是秒

(4)為ListView設定

      listView.setLayoutAnimation(lac);

在xml檔案中:

(1)在res/anim檔案夾中建立xml檔案

      <layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android“

                                            android:delay="0.5"                                                      //執行動畫的延遲時間,即時間間隔,單位是秒

                                            android:animationOrder="random"/"normal"/"reverse"  //執行順序

                                            android:animation="@anim/***"                                      //這是裝載具體動畫的xml檔案

(2)在具體的布局檔案中配置屬性

      android:layoutAnimation="@anim/***"   

五.AnimationListener

1.簡介

      AnimationListener是一個監聽器,它在動畫執行的各個階段會得到通知,並且調用相應的方法

2.方法

(1)onAnimationStart(Animation animation)    在動畫開始的時候調用

(2)onAnimationEnd(Animation animation)     在動畫結束的時候調用

(3)onAnimationRepeate(Animation animation)    在動畫重複的時候調用

3.實現

(1)構造監聽器

      private class MyAnimationListener implements AnimationListener{

                       ......在其中覆寫三種方法

        }

(2)使用

      animation.setAnimationListener(new MyAnimationListener());

相關文章

聯繫我們

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