在Android系統(tǒng)中提供了兩種動畫實現(xiàn)方式:一種是Tween動畫,通過視圖組件移動、放大、縮小,以及產(chǎn)生透明度的變化等來實現(xiàn)動畫;另一種是Frame動畫,這是一種傳統(tǒng)的通過順序播放排列好的圖片來實現(xiàn)的動畫方法,類似電影。
本文先介紹一下Tween動畫。
Tween動畫能完成一系列諸如位置、尺寸、透明度和旋轉(zhuǎn)等簡單的變化。例如,對程序中的ImageView組件,可以通過Tween動畫使該視圖實現(xiàn)放大、縮小、旋轉(zhuǎn)、漸變等變化。下面就對Tween動畫類進(jìn)行簡單介紹。
Tween動畫類位于android.view.animation包中,該包中包含了一些常用的動畫實現(xiàn)類。
□ Animation:動畫抽象類,其他幾個實現(xiàn)類繼承該類。
□ ScaleAnimation:控制尺寸變化的動畫類。
□ AlphaAnimation:控制透明度變化的動畫類。
□ TranslateAnimation:控制位置變化的動畫類。
□ AnimationSet:定義動畫屬性集合類。
□ AnimationUtils:動畫工具類。
總體來講,在Android系統(tǒng)中,Tween動畫有4種表現(xiàn)方式:漸變、縮放、平移,以及旋轉(zhuǎn)。對這4種方式的實現(xiàn)方法詳細(xì)說明如表1所示。
表1 Tween動畫實現(xiàn)表
名稱 |
實現(xiàn)類 |
常用構(gòu)造方法 |
參數(shù)說明 |
Alpha(漸變動畫) |
AlphaAnimation |
AlphaAnimation(float fromAlpha,float toAlpha) |
fromAlpha:動畫開始透明度
toAlpha:動畫結(jié)束透明度(取值范圍0.0~1.0) |
Scale(尺寸變化動畫) |
ScaleAnimation |
public ScaleAnimation (float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) |
fromX:起始X坐標(biāo)上的伸縮尺寸
toX:結(jié)束X坐標(biāo)上的伸縮尺寸
fromY:起始Y坐標(biāo)上的伸縮尺寸
toY:結(jié)束Y坐標(biāo)上的伸縮尺寸
pivotXType:X坐標(biāo)伸縮模式(取值有Animation.ABSOLUTE、 Animation.RELATIVE_TO_SELF、 Animation.RELATIVE_TO_PARENT)
pivotXValue:相對于X坐標(biāo)伸縮值
pivotYType:Y坐標(biāo)伸縮模式(取值有Animation.ABSOLUTE、 Animation.RELATIVE_TO_SELF、 Animation.RELATIVE_TO_PARENT)
pivotYValue:相對于Y坐標(biāo)伸縮值
|
Translate(位置變化動畫) |
TranslateAnimation |
public TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) |
fromXDelta:起始X坐標(biāo)
toXDelta;結(jié)束X坐標(biāo)
fromYDelta:起始Y坐標(biāo)
toYDelta:結(jié)束Y坐標(biāo)
|
Rotate(旋轉(zhuǎn)動畫) |
RotateAnimation |
public RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) |
fromDegrees:旋轉(zhuǎn)開始角度
toDegrees:旋轉(zhuǎn)結(jié)束角度
pivotXType:X坐標(biāo)伸縮模式(取值有Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF、Animation.RELATIVE_TO_PARENT)
pivotXValue:相對于X坐標(biāo)伸縮值
pivotYType:Y坐標(biāo)伸縮模式(取值有Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF、 Animation.RELATIVE_TO_PARENT.)
pivotYValue:相對于Y坐標(biāo)伸縮值
|
Tween動畫可以通過兩種編碼方式來實現(xiàn):一種是直接通過硬編碼的方式在程序代碼中實現(xiàn);另一種是在配置文件中定義。對于Android系統(tǒng)來講,建議使用配置文件的方法,這種方式可擴展性較高,便于維護。
首先,通過一個實現(xiàn)了各種動畫實例來演示硬編碼方式的實現(xiàn)過程。
在該實例中,界面中添加一個ImageView組件和4個Button組件。ImageView組件用于顯示動畫的圖標(biāo)。4個Button分別相應(yīng)單擊事件實現(xiàn)不同的動畫效果。實例步驟說明如下:
(1)創(chuàng)建一個Android工程“GraphicAnimation”。
(2)在工程的res//drawable//目錄中添加一個girl.png圖片資源。
(3)在該工程的res//layout//目錄中創(chuàng)建一個main.xml布局文件,在該布局文件中添加4個Button組件和一個ImageView組件,并為ImageView組件設(shè)置資源引用android:src屬性。代碼如代碼清單1所示。
代碼清單1 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ImageView01"
android:src="@drawable/girl"
></ImageView>
<Button
android:layout_height="wrap_content"
android:id="@+id/Button01"
android:text="Test Scale..."
android:layout_width="wrap_content"></Button>
<Button
android:layout_height="wrap_content"
android:id="@+id/Button02"
android:text="Test Alpha..."
android:layout_width="wrap_content"></Button>
<Button
android:layout_height="wrap_content"
android:id="@+id/Button03"
android:text="Test Translate..."
android:layout_width="wrap_content"></Button>
<Button
android:layout_height="wrap_content"
android:id="@+id/Button04"
android:text="Test Ratate..."
android:layout_width="wrap_content"></Button>
</LinearLayout>
(4)在MainActivity類的頂部聲明使用到的Button和ImageView組件,在onCreate()方法中實例化。代碼如代碼清單2所示。
代碼清單2 MainActivity.java
package cn.com.farsight.graphicanimation;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
//聲明使用到的Button視圖組件
private Button b1,b2,b3,b4;
//聲明使用到的ImageView組件
private ImageView girlImage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//實例化視圖組件
girlImage = (ImageView) findViewById(R.id.ImageView01);
b1 = (Button) findViewById(R.id.Button01);
b2 = (Button) findViewById(R.id.Button02);
b3 = (Button) findViewById(R.id.Button03);
b4 = (Button) findViewById(R.id.Button04);
}
}
(5)分別為4個按鈕添加單擊事件監(jiān)聽器,在監(jiān)聽器的方法中通過構(gòu)造方法定義不同的動畫,調(diào)用動畫的setDuration()方法設(shè)置動畫持續(xù)事件,調(diào)用ImageView的startAnimation()方法開始動畫。代碼如代碼清單3所示。
代碼清單3 添加事件監(jiān)聽
//為按鈕添加監(jiān)聽事件
b1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//創(chuàng)建Scale(尺寸)變化動畫
Animation scaleAnimation = new ScaleAnimation(0f, 1f, 0f, 1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
//設(shè)置動畫持續(xù)時間
scaleAnimation.setDuration(3000);
//開始動畫
girlImage.startAnimation(scaleAnimation);
}
});
b2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//創(chuàng)建Alpha(漸變)動畫
Animation alphaAnimation = new AlphaAnimation(0.1f,0.1f);
//設(shè)置動畫持續(xù)時間
alphaAnimation.setDuration(3000);
//開始動畫
girlImage.startAnimation(alphaAnimation);
}
});
b3.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//創(chuàng)建Translate(位置變化)動畫
Animation translateAnimation = new TranslateAnimation(10,100,10,100);
//設(shè)置動畫持續(xù)時間
translateAnimation.setDuration(3000);
//開始動畫
girlImage.startAnimation(translateAnimation);
}
});
b4.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//創(chuàng)建Rotate(旋轉(zhuǎn))動畫
Animation rotateAnimation = new RotateAnimation(0f,+360f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
//設(shè)置動畫持續(xù)時間
rotateAnimation.setDuration(3000);
//開始動畫
girlImage.startAnimation(rotateAnimation);
}
});
程序運行結(jié)果如圖1和圖2所示。

圖1 動畫效果Scale和Alpha

圖2 動畫效果Translate和Rotate
以上講述的是通過硬編碼的方式來實現(xiàn)的4種不同的動畫效果,其實Android系統(tǒng)更推薦我們使用XML配置文件的方式來實現(xiàn),該XML文件位于工程的res//anim//目錄中。XML文件的格式如代碼清單4所示。
代碼清單4 XML配置文件格式
<set android:shareInterpolator=boolean>
<alpha android:fromAlpha=float
android:toAlpha=float>
<scale android:fromXScale=float
android:toXScale=float
android:fromYScale=float
android:toYScale=float
android:pivotX=string
android:pivotY=string>
<translate android:fromX=string
android:toX=string
android:fromY=string
android:toY=string>
<rotate android:fromDegrees=float
android:toDegrees=float
android:pivotX=string
android:pivotY=string>
<interpolator tag>
<set>
</set>
如上所示,必須要有一個set根元素,根元素里邊定義不同的動畫,如漸變(Alpha)、尺寸(Scale)、位置變化(Translate)和旋轉(zhuǎn)(Rotate)等。
下面通過實例來演示如何使用XML配置文件實現(xiàn)各種動畫效果;舅悸肥窃诠こ痰膔es/anim/目錄下定義不同的動畫配置文件。通過調(diào)用AnimationUtils.loadAnimatioin()方法獲得動畫實例,調(diào)用視圖組件的startAnimation()方法開始動畫。實例具體步驟說明如下。
(1)創(chuàng)建一個Android工程“Graphic_Animation”。
(2)在工程的res/drawable/目錄中添加一個girl.png圖片資源。
(3)在該工程的res/layout/目錄中創(chuàng)建一個main.xml布局文件,在該布局文件中添加4個Button組件和一個ImageView組件,并為ImageView組件設(shè)置資源引用android:src屬性。代碼如代碼清單5所示。
代碼清單5 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ImageView01"
android:src="@drawable/girl"
></ImageView>
<Button
android:layout_height="wrap_content"
android:id="@+id/Button01"
android:text="Test Scale..."
android:layout_width="wrap_content"></Button>
<Button
android:layout_height="wrap_content"
android:id="@+id/Button02"
android:text="Test Alpha..."
android:layout_width="wrap_content"></Button>
<Button
android:layout_height="wrap_content"
android:id="@+id/Button03"
android:text="Test Translate..."
android:layout_width="wrap_content"></Button>
<Button
android:layout_height="wrap_content"
android:id="@+id/Button04"
android:text="Test Rotate..."
android:layout_width="wrap_content"></Button>
</LinearLayout>
(4)在MainActivity類的頂部聲明使用到的Button和ImageView組件,在onCreate()方法中將其實例化。
代碼清單6 MainActivity.java
package cn.com.farsight.graphicanimation;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
//聲明使用到的Button視圖組件
private Button b1,b2,b3,b4;
//聲明使用到的ImageView組件
private ImageView girlImage;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//實例化視圖組件
girlImage = (ImageView) findViewById(R.id.ImageView01);
b1 = (Button) findViewById(R.id.Button01);
b2 = (Button) findViewById(R.id.Button02);
b3 = (Button) findViewById(R.id.Button03);
b4 = (Button) findViewById(R.id.Button04);
}
}
(5)在工程的res/anim/目錄下創(chuàng)建各種動畫的XML配置文件,代碼如代碼清單7-10所示。
□ Alpha:
代碼清單7 Alpha
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlhpa="0.1"
android:toAlpha="1.0"
android:duration="5000"
/>
</set>
□ Translate:
代碼清單8 Translate
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android: fromXDelta="10"
android:toXDelta="100"
android:fromYDelta="10"
android:toYDelta="100">
</translate>
</set>
□ Scale:
代碼清單9 Scale
<set xmlns:android="http://schemas.android.com/apk/res/android">
<Scale
android:fromXScale="0.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000">
</scale>
</set>
□ Rotate:
代碼清單10 Rotate
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="-180"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000">
</rotate>
</set>
分別為4個按鈕添加單擊事件監(jiān)聽器,在監(jiān)聽器的方法中通過AnimationUtils. loadAnimation()方法定義不同的動畫,調(diào)用ImageView的startAnimation()方法開始動畫。
代碼清單11 添加事件監(jiān)聽
//為按鈕添加監(jiān)聽事件
b1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//創(chuàng)建Scale(尺寸)變化動畫
Animation scaleAnimation = AnimationUtils.loadAnimation
(MainActivity.this,R.anim.my_scale);
//開始動畫
girlImage.startAnimation(scaleAnimation);
}
});
b2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//創(chuàng)建Alpha(漸變)動畫
Animation alphaAnimation = AnimationUtils.loadAnimation
(MainActivity.this,R.anim.my_alpha);
//開始動畫
girlImage.startAnimation(alphaAnimation);
}
});
b3.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//創(chuàng)建Translate(位置變化)動畫
Animation translateAnimation = AnimationUtils.loadAnimation
(MainActivity.this,R.anim.my_translate);
//開始動畫
girlImage.startAnimation(translateAnimation);
}
});
b4.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//創(chuàng)建Rotate(旋轉(zhuǎn))動畫
Animation rotateAnimation = AnimationUtils.loadAnimation
(MainActivity.this,R.anim.my_rotate);
//開始動畫
girlImage.startAnimation(rotateAnimation);
}
});
程序運行結(jié)果如圖3和圖4所示

圖3 動畫效果Scale和Alpha

圖4 動畫效果Translate和Rotate