Animation動畫總結1

動畫類型

Androidanimation由四種類型組成

1.png

2011-4-23 22:23 上傳
下載附件(12.8 KB)


Android動畫模式

Animation主要有兩種動畫模式:
一種是tweened animation(漸變動畫)

XML
JavaCode
alpha
AlphaAnimation
scale
ScaleAnimation


一種是frame by frame(畫面轉換動畫)
XML
JavaCode
translate
TranslateAnimation
rotate
RotateAnimation



如何在XML文件中定義動畫

① 打開Eclipse,新建Android工程
② res目錄中新建anim文件夾
③ anim目錄中新建一個myanim.xml(注意文件名小寫)
④ 加入XML的動畫代碼

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3.   <alpha/>
  4.   <scale/>
  5.   <translate/>
  6.   <rotate/>
  7. </set>
複製代碼


Android動畫解析--XML

<alpha>

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <alpha
  4. android:fromAlpha="0.1"
  5. android:toAlpha="1.0"
  6. android:duration="3000"
  7. />
  8. <!-- 透明度控制動畫效果 alpha
  9.         浮點型值:
  10.             fromAlpha 屬性爲動畫起始時透明度
  11.             toAlpha   屬性爲動畫結束時透明度
  12.             說明:
  13.                 0.0表示完全透明
  14.                 1.0表示完全不透明
  15.             以上值取0.0-1.0之間的float數據類型的數字
  16.         
  17.         長整型值:
  18.             duration  屬性爲動畫持續時間
  19.             說明:     
  20.                 時間以毫秒爲單位
  21. -->
  22. </set>
複製代碼

<scale>

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3.    <scale  
  4.           android:interpolator=
  5.                      "@android:anim/accelerate_decelerate_interpolator"
  6.           android:fromXScale="0.0"
  7.           android:toXScale="1.4"
  8.           android:fromYScale="0.0"
  9.           android:toYScale="1.4"
  10.           android:pivotX="50%"
  11.           android:pivotY="50%"
  12.           android:fillAfter="false"
  13.           android:duration="700" />
  14. </set>
  15. <!-- 尺寸伸縮動畫效果 scale
  16.        屬性:interpolator 指定一個動畫的插入器
  17.         在我試驗過程中,使用android.res.anim中的資源時候發現
  18.         有三種動畫插入器:
  19.             accelerate_decelerate_interpolator  加速-減速 動畫插入器
  20.             accelerate_interpolator        加速-動畫插入器
  21.             decelerate_interpolator        減速- 動畫插入器
  22.         其他的屬於特定的動畫效果
  23.       浮點型值:
  24.          
  25.             fromXScale 屬性爲動畫起始時 X座標上的伸縮尺寸   
  26.             toXScale   屬性爲動畫結束時 X座標上的伸縮尺寸     
  27.         
  28.             fromYScale 屬性爲動畫起始時Y座標上的伸縮尺寸   
  29.             toYScale   屬性爲動畫結束時Y座標上的伸縮尺寸   
  30.         
  31.             說明:
  32.                  以上四種屬性值   
  33.    
  34.                     0.0表示收縮到沒有
  35.                     1.0表示正常無伸縮     
  36.                     值小於1.0表示收縮  
  37.                     值大於1.0表示放大
  38.         
  39.             pivotX     屬性爲動畫相對於物件的X座標的開始位置
  40.             pivotY     屬性爲動畫相對於物件的Y座標的開始位置
  41.         
  42.             說明:
  43.                     以上兩個屬性值 從0%-100%中取值
  44.                     50%爲物件的X或Y方向座標上的中點位置
  45.         
  46.         長整型值:
  47.             duration  屬性爲動畫持續時間
  48.             說明:   時間以毫秒爲單位

  49.         布爾型值:
  50.             fillAfter 屬性 當設置爲true ,該動畫轉化在動畫結束後被應用
  51. -->
複製代碼


<translate>

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <translate
  4. android:fromXDelta="30"
  5. android:toXDelta="-80"
  6. android:fromYDelta="30"
  7. android:toYDelta="300"
  8. android:duration="2000"
  9. />
  10. <!-- translate 位置轉移動畫效果
  11.         整型值:
  12.             fromXDelta 屬性爲動畫起始時 X座標上的位置   
  13.             toXDelta   屬性爲動畫結束時 X座標上的位置
  14.             fromYDelta 屬性爲動畫起始時 Y座標上的位置
  15.             toYDelta   屬性爲動畫結束時 Y座標上的位置
  16.             注意:
  17.                      沒有指定fromXType toXType fromYType toYType 時候,
  18.                      默認是以自己爲相對參照物            
  19.         長整型值:
  20.             duration  屬性爲動畫持續時間
  21.             說明:   時間以毫秒爲單位
  22. -->
  23. </set>
複製代碼

<rotate>

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">
  3. <rotate
  4.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"
  5.         android:fromDegrees="0"
  6.         android:toDegrees="+350"         
  7.         android:pivotX="50%"
  8.         android:pivotY="50%"     
  9.         android:duration="3000" />  
  10. <!-- rotate 旋轉動畫效果
  11.        屬性:interpolator 指定一個動畫的插入器
  12.              在我試驗過程中,使用android.res.anim中的資源時候發現
  13.              有三種動畫插入器:
  14.                 accelerate_decelerate_interpolator   加速-減速 動畫插入器
  15.                 accelerate_interpolator               加速-動畫插入器
  16.                 decelerate_interpolator               減速- 動畫插入器
  17.              其他的屬於特定的動畫效果
  18.                            
  19.        浮點數型值:
  20.             fromDegrees 屬性爲動畫起始時物件的角度   
  21.             toDegrees   屬性爲動畫結束時物件旋轉的角度 可以大於360度   

  22.         
  23.             說明:
  24.                      當角度爲負數——表示逆時針旋轉
  25.                      當角度爲正數——表示順時針旋轉              
  26.                      (負數from——to正數:順時針旋轉)   
  27.                      (負數from——to負數:逆時針旋轉)
  28.                      (正數from——to正數:順時針旋轉)
  29.                      (正數from——to負數:逆時針旋轉)      

  30.             pivotX     屬性爲動畫相對於物件的X座標的開始位置
  31.             pivotY     屬性爲動畫相對於物件的Y座標的開始位置
  32.                
  33.             說明:        以上兩個屬性值 從0%-100%中取值
  34.                          50%爲物件的X或Y方向座標上的中點位置

  35.         長整型值:
  36.             duration  屬性爲動畫持續時間
  37.             說明:       時間以毫秒爲單位
  38. -->
  39. </set>
複製代碼

如何使用XML中的動畫效果

  1. public static Animation loadAnimation (Context context, int id)
  2. //第一個參數Context爲程序的上下文   
  3. //第二個參數id爲動畫XML文件的引用
  4. //例子:
  5. myAnimation= AnimationUtils.loadAnimation(this,R.anim.my_action);
  6. //使用AnimationUtils類的靜態方法loadAnimation()來加載XML中的動畫XML文件
複製代碼

如何在Java代碼中定義動畫

  1. //在代碼中定義 動畫實例對象
  2. private Animation myAnimation_Alpha;
  3. private Animation myAnimation_Scale;
  4. private Animation myAnimation_Translate;
  5. private Animation myAnimation_Rotate;
  6.    
  7.     //根據各自的構造方法來初始化一個實例對象
  8. myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);

  9. myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
  10.              Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

  11. myAnimation_Translate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);

  12. myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
  13.                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
複製代碼


Android動畫解析--JavaCode


AlphaAnimation

① AlphaAnimation類對象定義

  1. private AlphaAnimation myAnimation_Alpha;
複製代碼

② AlphaAnimation類對象構造

  1. AlphaAnimation(float fromAlpha, float toAlpha)
  2. //第一個參數fromAlpha爲 動畫開始時候透明度
  3. //第二個參數toAlpha爲 動畫結束時候透明度
  4. myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);
  5. //說明:
  6. //                0.0表示完全透明
  7. //                1.0表示完全不透明
複製代碼

③ 設置動畫持續時間

  1. myAnimation_Alpha.setDuration(5000);
  2. //設置時間持續時間爲 5000毫秒
複製代碼


ScaleAnimation

① ScaleAnimation類對象定義

  1. private AlphaAnimation myAnimation_Alpha;
複製代碼

② ScaleAnimation類對象構造

  1. ScaleAnimation(float fromX, float toX, float fromY, float toY,
  2.            int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
  3. //第一個參數fromX爲動畫起始時 X座標上的伸縮尺寸   
  4. //第二個參數toX爲動畫結束時 X座標上的伸縮尺寸     
  5. //第三個參數fromY爲動畫起始時Y座標上的伸縮尺寸   
  6. //第四個參數toY爲動畫結束時Y座標上的伸縮尺寸  
  7. /*說明:
  8.                     以上四種屬性值   
  9.                     0.0表示收縮到沒有
  10.                     1.0表示正常無伸縮     
  11.                     值小於1.0表示收縮  
  12.                     值大於1.0表示放大
  13. */
  14. //第五個參數pivotXType爲動畫在X軸相對於物件位置類型  
  15. //第六個參數pivotXValue爲動畫相對於物件的X座標的開始位置
  16. //第七個參數pivotXType爲動畫在Y軸相對於物件位置類型   
  17. //第八個參數pivotYValue爲動畫相對於物件的Y座標的開始位置
  18. myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
  19.              Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
複製代碼

③ 設置動畫持續時間

  1. myAnimation_Scale.setDuration(700);
  2. //設置時間持續時間爲 700毫秒
複製代碼

TranslateAnimation


① TranslateAnimation類對象定義

  1. private AlphaAnimation myAnimation_Alpha;
複製代碼

② TranslateAnimation類對象構造

  1. TranslateAnimation(float fromXDelta, float toXDelta,
  2.                        float fromYDelta, float toYDelta)
  3. //第一個參數fromXDelta爲動畫起始時 X座標上的移動位置   
  4. //第二個參數toXDelta爲動畫結束時 X座標上的移動位置      
  5. //第三個參數fromYDelta爲動畫起始時Y座標上的移動位置     
  6. //第四個參數toYDelta爲動畫結束時Y座標上的移動位置
複製代碼

③ 設置動畫持續時間

  1. myAnimation_Translate.setDuration(2000);
  2. //設置時間持續時間爲 2000毫秒
複製代碼

RotateAnimation
① RotateAnimation類對象定義

  1. private AlphaAnimation myAnimation_Alpha;
複製代碼

② RotateAnimation類對象構造

  1. RotateAnimation(float fromDegrees, float toDegrees,
  2.             int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
  3. //第一個參數fromDegrees爲動畫起始時的旋轉角度   
  4. //第二個參數toDegrees爲動畫旋轉到的角度   
  5. //第三個參數pivotXType爲動畫在X軸相對於物件位置類型  
  6. //第四個參數pivotXValue爲動畫相對於物件的X座標的開始位置
  7. //第五個參數pivotXType爲動畫在Y軸相對於物件位置類型   
  8. //第六個參數pivotYValue爲動畫相對於物件的Y座標的開始位置
  9. myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
  10.                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
複製代碼

③ 設置動畫持續時間

  1. myAnimation_Rotate.setDuration(3000);
  2. //設置時間持續時間爲 3000毫秒
複製代碼
 
 
實例

  • 新建工程 myFrameAnimation

  • 在main.xml佈局中添加view子類,調整一下,效果如下:

1.png
2011-4-23 22:35 上傳
下載附件(19.14 KB)
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:prientation="vertical" android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent">
  5.     <LinearLayout android:prientation="horizontal"
  6.         android:layout_width="fill_parent" android:layout_height="wrap_content"
  7.         android:background="@drawable/bt_group_back" android:layout_marginTop="10px">
  8.         <Button android:text="播放動畫" android:layout_width="100px"
  9.             android:id="@+id/Button_start" android:layout_height="fill_parent"></Button>
  10.         <Button android:layout_width="100px" android:text="停止動畫"
  11.             android:id="@+id/Button_stop" android:layout_height="fill_parent"></Button>
  12.         <CheckBox android:text="動畫重複" android:layout_width="100px"
  13.             android:id="@+id/CheckBox_ifCycle_orNot" style="?android:attr/starStyle"
  14.             android:layout_height="fill_parent"></CheckBox>
  15.     </LinearLayout>
  16.     <ImageView android:id="@+id/rocket_image"
  17.         android:layout_width="80px" android:layout_height="80px"
  18.         android:background="@drawable/android_large"
  19.         android:layout_marginLeft="100dp" android:layout_marginTop="100dp"></ImageView>
  20. </LinearLayout>
複製代碼
  • 找幾個動態圖片,把它分成單個圖。(主要是爲了講解/是用FrameAnimation效果,AnimationDrawable)


  • 修改mainActivity.java的代碼
    1. package zyf.my.frame.animation;

    2. import android.app.Activity;
    3. import android.graphics.drawable.AnimationDrawable;
    4. import android.os.Bundle;
    5. import android.view.MotionEvent;
    6. import android.view.View;
    7. import android.widget.Button;
    8. import android.widget.CheckBox;
    9. import android.widget.ImageView;
    10. import android.widget.Toast;
    11. public class myFrameAnimatino extends Activity implements Button.OnClickListener {
    12.     /** Called when the activity is first created. */
    13.     AnimationDrawable frameAnimation;
    14.     /*
    15.      * 聲明AnimationDrawable 可繪製動畫 對象frameAnimation
    16.      */
    17.     ImageView myImage;
    18.     /*
    19.      * 圖片View ImageView
    20.      */
    21.     Button start,stop;
    22.     CheckBox Cycle;
    23.     boolean isChecked_cycle=true;
    24.     /*
    25.      * (non-Javadoc)
    26.      * @see android.app.Activity#onCreate(android.os.Bundle)
    27.      */
    28.     @Override
    29.     public void onCreate(Bundle savedInstanceState) {
    30.         super.onCreate(savedInstanceState);
    31.         setContentView(R.layout.main);
    32.         
    33.         start=(Button) findViewById(R.id.Button_start);
    34.         stop=(Button) findViewById(R.id.Button_stop);
    35.         Cycle=(CheckBox) findViewById(R.id.CheckBox_ifCycle_orNot);
    36.         /*
    37.          * findViewById()從XML中獲取 Button  CheckBox
    38.          */
    39.         
    40.         myImage = (ImageView) findViewById(R.id.rocket_image);
    41.         /*
    42.          * findViewById()從XML中獲取ImageView 對象myImage
    43.          */
    44.         myImage.setBackgroundResource(R.anim.myframeanimation);
    45.         /*
    46.          * ImageView.setBackgroundResource()設置 圖片View的背景圖片
    47.          * 這裏是把幀動畫 myframeanimation加到 圖片View的背景中
    48.          */
    49.         frameAnimation = (AnimationDrawable) myImage.getBackground();
    50.         /*
    51.          * myImage.getBackground()獲得背景的Drawable的對象,轉換成AnimationDrawable
    52.          */
    53.         
    54.         start.setOnClickListener(this);
    55.         stop.setOnClickListener(this);
    56.     }
    57.     /*
    58.      * (non-Javadoc)
    59.      * @see android.app.Activity#onTouchEvent(android.view.MotionEvent)
    60.      */
    61.     @Override
    62.     public boolean onTouchEvent(MotionEvent event) {
    63.         frameAnimation.setOneShot(isChecked_cycle);
    64.         /*
    65.          * 添加觸摸事件處理方法
    66.          */
    67.         // TODO Auto-generated method stub
    68.         if(event.getAction()==MotionEvent.ACTION_DOWN){
    69.             /*
    70.              * MotionEvent.getAction()獲取事件動作
    71.              * MotionEvent.ACTION_DOWN 向下的手勢動作
    72.              */

    73.         /*event.getAction() 返回正被執行的動作種類:
    74.          * 是     ACTION_DOWN, ACTION_MOVE, ACTION_UP, 或 ACTION_CANCEL中的一個.
    75.          */
    76.             frameAnimation.start();
    77.             /*
    78.              * 啓動幀動畫效果
    79.              */
    80.             return true;

    81.         }
    82.         return super.onTouchEvent(event);
    83.     }
    84.     /*
    85.      * (non-Javadoc)
    86.      * @see android.view.View.OnClickListener#onClick(android.view.View)
    87.      */
    88.     @Override
    89.     public void onClick(View button) {
    90.         // TODO Auto-generated method stub
    91.         switch (button.getId()) {
    92.         case R.id.Button_start:{
    93.             if(Cycle.isChecked()){
    94.                 Toast.makeText(this, "動畫重複", Toast.LENGTH_LONG).show();
    95.                 isChecked_cycle=false;
    96.             }else{
    97.                 Toast.makeText(this, "不重複", Toast.LENGTH_LONG).show();
    98.                 isChecked_cycle=true;
    99.             }
    100.             /*
    101.              * 複選按鈕選中,  動畫重複播放,  AnimationDrawable.setOneShot(false)
    102.              * 複選按鈕未選中,動畫不重複播放,AnimationDrawable.setOneShot(true)
    103.              */
    104.             
    105.             frameAnimation.setOneShot(isChecked_cycle);
    106.             /*
    107.              * 設置重複與否
    108.              */
    109.             frameAnimation.start();
    110.             /*
    111.              *啓動幀動畫效果
    112.              */
    113.             
    114.         }
    115.             break;
    116.         case R.id.Button_stop:{
    117.             if(frameAnimation.isRunning()){
    118.                 /*
    119.                  * AnimationDrawable.isRunning(),判斷幀動畫是否在運行,true---運行中
    120.                  * 如果動畫正在運行,可以停止
    121.                  */
    122.                 frameAnimation.stop();
    123.                 /*
    124.                  *停止幀動畫效果
    125.                  */
    126.             }
    127.             
    128.         }
    129.             break;
    130.         default:
    131.             break;
    132.         }
    133.     }
    134. }
    複製代碼

    2.png
    2011-4-23 22:35 上傳
    下載附件(32.89 KB)
    3.png
    2011-4-23 22:35 上傳
    下載附件(29.18 KB)


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章