Android提供了2種動(dòng)畫:
1> Tween動(dòng)畫,通過對 View 的內(nèi)容進(jìn)行一系列的圖形變換 (包括平移、縮放、旋轉(zhuǎn)、改變透明度)來實(shí)現(xiàn)動(dòng)畫效果。動(dòng)畫效果的定義可以采用XML來做也可以采用編碼來做。Tween動(dòng)畫有4種類型:AlphaAnimation、ScaleAnimation、TranslateAnimation、RotateAnimation。
2> Frame動(dòng)畫,即順序播放事先做好的圖像,跟電影類似。開發(fā)步驟:
(1)把準(zhǔn)備好的圖片放進(jìn)項(xiàng)目res/ drawable下。
(2)在項(xiàng)目的res目錄下創(chuàng)建文件夾anim,然后在anim文件夾下面定義動(dòng)畫XML文件,文件名稱可以自定義。當(dāng)然也可以采用編碼方式定義動(dòng)畫效果(使用AnimationDrawable類)。
(3)為View控件綁定動(dòng)畫效果。調(diào)用代表動(dòng)畫的AnimationDrawable的start()方法開始動(dòng)畫。

    本例要實(shí)現(xiàn)對ImageView對象進(jìn)行漸變尺寸縮放動(dòng)畫效果
    1> 在項(xiàng)目的res目錄下創(chuàng)建文件夾anim,然后在anim文件夾下面定義動(dòng)畫XML文件,文件名稱可以自定義,如:scale.xml,內(nèi)容如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2  <set xmlns:android="http://schemas.android.com/apk/res/android"> 
 3     <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
 4         android:fromXScale="0.0"  
 5         android:toXScale="5" 
 6         android:fromYScale="0.0" 
 7         android:toYScale="5" 
 8         android:pivotX="50%" 
 9         android:pivotY="50%" 
10         android:fillAfter="false" 
11         android:duration="5000"
12         /> 
13 </set>
    動(dòng)畫的進(jìn)度使用interpolator控制,android提供了幾個(gè)Interpolator 子類,實(shí)現(xiàn)了不同的速度曲線,如LinearInterpolator實(shí)現(xiàn)了勻速效果、Accelerateinterpolator實(shí)現(xiàn)了加速效果、DecelerateInterpolator實(shí)現(xiàn)了減速效果等。還可以定義自己的Interpolator子類,實(shí)現(xiàn)拋物線、自由落體等物理效果。

fromXScale(浮點(diǎn)型) 屬性為動(dòng)畫起始時(shí)X坐標(biāo)上的縮放尺寸 
fromYScale(浮點(diǎn)型) 屬性為動(dòng)畫起始時(shí)Y坐標(biāo)上的縮放尺寸
toXScale(浮點(diǎn)型)   屬性為動(dòng)畫結(jié)束時(shí)X坐標(biāo)上的縮放尺寸
toYScale(浮點(diǎn)型)   屬性為動(dòng)畫結(jié)束時(shí)Y坐標(biāo)上的縮放尺寸
說明: 以上四種屬性值 
0.0表示收縮到?jīng)]有 
1.0表示正常無縮放
值小于1.0表示收縮 
值大于1.0表示放大
pivotX(浮點(diǎn)型)     屬性為動(dòng)畫相對于物件的X坐標(biāo)的開始位置 
pivotY(浮點(diǎn)型)     屬性為動(dòng)畫相對于物件的Y坐標(biāo)的開始位置 
說明: 
以上兩個(gè)屬性值 從0%
-100%中取值
50%為物件的X或Y方向坐標(biāo)上的中點(diǎn)位置
duration(長整型)屬性為動(dòng)畫持續(xù)時(shí)間 。說明:   時(shí)間以毫秒為單位
fillAfter(布爾型)屬性當(dāng)設(shè)置為true,該動(dòng)畫轉(zhuǎn)化在動(dòng)畫結(jié)束后被應(yīng)用


2> 在layout文件添加<ImageView>節(jié)點(diǎn):

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="horizontal"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     >
 7 <ImageView
 8    android:layout_width="wrap_content"
 9    android:layout_height="wrap_content"
10    android:src="@drawable/icon"
11    android:id="@+id/imageView"
12    />
13 </LinearLayout>
說明:除了可以對<ImageView>實(shí)現(xiàn)動(dòng)畫效果,其實(shí)也可以對其他View實(shí)現(xiàn)動(dòng)畫效果,如:<TextView>

3>在Activity里對ImageView使用前面定義好的動(dòng)畫效果:

 1 public class AnimationActivity extends Activity {
 2     @Override
 3     public void onCreate(Bundle savedInstanceState) {
 4         super.onCreate(savedInstanceState);
 5         setContentView(R.layout.main);
 6         ImageView imageView = (ImageView)this.findViewById(R.id.imageView);
 7         //加載動(dòng)畫XML文件,生成動(dòng)畫指令
 8         Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
 9         //開始執(zhí)行動(dòng)畫
10         imageView.startAnimation(animation);
11     }
12 }
13 

備注:上面采用的是xml文件定義動(dòng)畫效果,作為代替,也可以采用編碼方式實(shí)現(xiàn)。下面采用編碼方式實(shí)現(xiàn)上述例子同樣的效果:

 1 public class AnimationActivity extends Activity {
 2     @Override
 3     public void onCreate(Bundle savedInstanceState) {
 4         super.onCreate(savedInstanceState);
 5         setContentView(R.layout.main);
 6         ImageView imageView = (ImageView)this.findViewById(R.id.imageView);
 7         ScaleAnimation animation = new ScaleAnimation(0.0f, 5f, 0.0f, 5f, 
 8         Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
 9         animation.setDuration(5000); //設(shè)置持續(xù)時(shí)間5秒
10         imageView.startAnimation(animation);
11     }
12 }


=====================  Frame動(dòng)畫例子  ===============================
(1)把準(zhǔn)備好的圖片放進(jìn)項(xiàng)目res/ drawable下。
   圖片有:girl_1.gif, girl_2.gif, girl_3.gif
(2)在項(xiàng)目的res目錄下創(chuàng)建文件夾anim,然后在anim文件夾下面定義動(dòng)畫XML文件,文件名稱可以自定義,如:frame.xml。

1 <?xml version="1.0" encoding="utf-8"?>
2 <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
3     android:oneshot="false">
4     <item android:drawable="@drawable/girl_1" android:duration="200" />
5     <item android:drawable="@drawable/girl_2" android:duration="200" />
6     <item android:drawable="@drawable/girl_3" android:duration="200" />
7 </animation-list>

上面的XML就定義了一個(gè)Frame動(dòng)畫,其包含3幀動(dòng)畫,3幀動(dòng)畫中分別應(yīng)用了drawable中的3張圖片:girl_1.gif, girl_2.gif, girl_3.gif,每幀動(dòng)畫持續(xù)200毫秒。android:oneshot屬性如果為true,表示動(dòng)畫只播放一次停止在最后一幀上,如果設(shè)置為false表示動(dòng)畫循環(huán)播放。
(3)為View控件綁定動(dòng)畫效果,調(diào)用代表動(dòng)畫的AnimationDrawable的start()方法開始動(dòng)畫。

 1 public class FrameActivity extends Activity {
 2     private AnimationDrawable animationDrawable;
 3     @Override
 4     public void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.main);
 7         ImageView imageView = (ImageView)this.findViewById(R.id.imageView);
 8         imageView.setBackgroundResource(R.anim.frame);
 9         animationDrawable = (AnimationDrawable) imageView.getBackground();
10     }
11     @Override
12     public boolean onTouchEvent(MotionEvent event) {
13           if (event.getAction() == MotionEvent.ACTION_DOWN) {//按下
14               animationDrawable.start();
15               return true;
16           }
17           return super.onTouchEvent(event);
18     }
19 }
有一點(diǎn)需要強(qiáng)調(diào)的是:啟動(dòng)Frame動(dòng)畫的代碼animationDrawable.start();不能應(yīng)用在OnCreate()方法中,因?yàn)樵贠nCreate()中 AnimationDrawable還沒有完全的與ImageView綁定。在OnCreate()中啟動(dòng)動(dòng)畫,只能看到第一張圖片。這里在拖曳事件中實(shí)現(xiàn)的。