Android 系統提供了兩種實作動畫的方式:Tween(補間)動畫與 Frame(逐幀)動畫。本文聚焦於 Tween 動畫,它能讓視圖實現放大、縮小、旋轉、漸變等效果,相關類別皆位於 android.view.animation 套件下。
Animation:動畫的抽象類別,其他實作類別皆繼承自它。AnimationSet:動畫屬性的集合類。AnimationUtils:動畫工具類。AlphaAnimation:控制透明度變化。RotateAnimation:控制旋轉。ScaleAnimation:控制大小縮放。TranslateAnimation:控制位置移動。AlphaAnimation(float fromAlpha, float toAlpha)
fromAlpha 為起始透明度,toAlpha 為結束透明度,取值範圍 0.0~1.0。
RotateAnimation(float fromDegrees, float toDegrees) RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY) RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
fromDegrees/toDegrees 為起始與結束角度;pivotXType/pivotYType 為 X、Y 方向的伸縮方式;pivotXValue/pivotYValue 為對應的伸縮值。
ScaleAnimation(float fromX, float toX, float fromY, float toY) ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY) ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
fromX/toX 為 X 方向起訖比例,fromY/toY 為 Y 方向起訖比例,pivot 參數則用於指定縮放中心。
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
分別代表 X、Y 方向的起始與結束位移量。
Tween 動畫有兩種建立方式:直接在程式碼中設定,或在 XML 檔中配置。以下示範直接編碼的做法,透過四顆按鈕分別觸發四種動畫:
package com.android.animation;
import com.android.R;
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 Demo_Animation extends Activity {
// 定義四個按鈕,分別對應四種動畫效果操作
private Button alpha, roate, scale, translate;
// 聲明 ImageView 組件
private ImageView animation_test;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.animation);
animation_test = (ImageView) this.findViewById(R.id.test_animation);
alpha = (Button) this.findViewById(R.id.alpha);
roate = (Button) this.findViewById(R.id.roate);
scale = (Button) this.findViewById(R.id.scale);
translate = (Button) this.findViewById(R.id.translate);
// 為按鈕組件添加監聽事件
alpha.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Animation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
alphaAnimation.setDuration(3000);
animation_test.setAnimation(alphaAnimation);
}
});
roate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Animation roateAnimation = new RotateAnimation(0f, +180f,
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 1.0f);
roateAnimation.setDuration(3000);
animation_test.setAnimation(roateAnimation);
}
});
scale.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Animation scaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f,
1.0f, Animation.RELATIVE_TO_SELF, 1.5f,
Animation.RELATIVE_TO_SELF, 1.5f);
scaleAnimation.setDuration(3000);
animation_test.setAnimation(scaleAnimation);
}
});
translate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Animation translateAnimation = new TranslateAnimation(0.0f,
15.0f, 0.0f, 15.0f);
translateAnimation.setDuration(3000);
animation_test.setAnimation(translateAnimation);
}
});
}
}
其中的伸縮模式常使用 Animation.RELATIVE_TO_SELF(相對於自身)、Animation.RELATIVE_TO_PARENT(相對於父容器)與 Animation.ABSOLUTE(絕對座標)三種。對應的版面配置只需放入一個 ImageView 與四顆 Button,按下按鈕即可播放各自設定了 3 秒時長的動畫效果。