編輯:關於 Android 程式設計
Android 平台提供了兩類動畫:一類是 Tween 動畫,對場景中的物件連續做外觀變化來產生動畫效果(旋轉、平移、縮放、漸變);另一類是 Frame 動畫,也就是逐幀播放圖片序列。本文聚焦在程式碼版的 Tween 動畫。
Animation — 動畫基底類別AlphaAnimation — 漸變透明度RotateAnimation — 畫面旋轉ScaleAnimation — 漸變尺寸縮放TranslateAnimation — 位置移動AnimationSet — 動畫集合,用於組合多個動畫第一個參數 fromAlpha 表示動畫起始時的透明度,第二個參數 toAlpha 表示結束時的透明度。setDuration 用來設定動畫持續時間(毫秒)。
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f); // 從不透明淡出 alphaAnimation.setDuration(2000);
第一個參數 fromDegrees 是起始角度,第二個參數 toDegrees 是結束角度。另外還有四個參數用來指定旋轉中心:
RotateAnimation rotateAnimation = new RotateAnimation(
0, 360,
Animation.RELATIVE_TO_PARENT, 1f,
Animation.RELATIVE_TO_PARENT, 0f);
其中 pivotXType / pivotYType 決定座標的參考基準(RELATIVE_TO_SELF 相對於自身、RELATIVE_TO_PARENT 相對於父容器、ABSOLUTE 為絕對像素),pivotXValue / pivotYValue 則是該基準下的比例位置。
ScaleAnimation scaleAnimation = new ScaleAnimation(
1, 0.1f, // fromX, toX:X 軸的伸縮比例
1, 0.1f, // fromY, toY:Y 軸的伸縮比例
Animation.RELATIVE_TO_SELF, 0.5f, // 中心點 X
Animation.RELATIVE_TO_SELF, 0.5f); // 中心點 Y
同樣可以設定 pivotXType、pivotYType 與 pivotXValue、pivotYValue。0.5f 搭配 RELATIVE_TO_SELF 就是「以自身中心為軸」,這是最常用來做「向中心收攏」效果的寫法。
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, // X 軸起始位置
Animation.RELATIVE_TO_SELF, 0.5f, // X 軸結束位置
Animation.RELATIVE_TO_SELF, 0f, // Y 軸起始位置
Animation.RELATIVE_TO_SELF, 1.0f);// Y 軸結束位置
位移的意義是「從哪裡移動到哪裡」,用比例表示時特別方便適配不同螢幕尺寸。
下面的例子讓同一張圖片分別做出上述四種效果,第五個按鈕則示範「漸變 + 位移」的組合動畫。

點擊各按鈕會觸發對應反應。
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="https://schemas.android.com/apk/res/android">
<Button
android:id="@+id/button_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scale"
android:layout_x="5dp"
android:layout_y="383dp" />
<Button
android:id="@+id/button_rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rotate"
android:layout_x="158dp"
android:layout_y="383dp" />
<Button
android:id="@+id/button_alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="alpha"
android:layout_x="5dp"
android:layout_y="331dp" />
<Button
android:id="@+id/button_translate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="translate"
android:layout_x="160dp"
android:layout_y="329dp" />
<Button
android:id="@+id/button_alpha_translate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="alpha_translate"
android:layout_x="84dp"
android:layout_y="265dp" />
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="105dp"
android:layout_y="133dp"
android:src="@drawable/ic_launcher" />
</AbsoluteLayout>
public class Animations_Activity extends Activity {
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animations_);
button1 = (Button) findViewById(R.id.button_alpha);
button2 = (Button) findViewById(R.id.button_rotate);
button3 = (Button) findViewById(R.id.button_scale);
button4 = (Button) findViewById(R.id.button_translate);
button5 = (Button) findViewById(R.id.button_alpha_translate);
imageView = (ImageView) findViewById(R.id.imageview);
button1.setOnClickListener(new MyButton());
button2.setOnClickListener(new MyButton());
button3.setOnClickListener(new MyButton());
button4.setOnClickListener(new MyButton());
button5.setOnClickListener(new MyButton());
}
class MyButton implements OnClickListener {
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.button_alpha:
Alpha();
break;
case R.id.button_rotate:
Rotata();
break;
case R.id.button_scale:
Scale();
break;
case R.id.button_translate:
Translate();
break;
case R.id.button_alpha_translate:
Alpha_Translate();
break;
default:
break;
}
}
}
/*
* 1. 創建一個 AnimationSet 對象,該對象存儲的是動畫的集合
* 2. 根據需要創建相應的 Animation 對象
* 3. 根據動畫的需求,為 Animation 對象設置相應的數據(即執行效果)
* 4. 將 Animation 對象添加到 AnimationSet 對象當中
* 5. 使用控制項對象開始執行 AnimationSet
*/
public void Alpha() {
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
alphaAnimation.setDuration(2000);
animationSet.addAnimation(alphaAnimation);
imageView.startAnimation(animationSet);
}
public void Rotata() {
AnimationSet animationSet = new AnimationSet(true);
// 後面的四個參數定義的是旋轉的圓心位置
RotateAnimation rotateAnimation = new RotateAnimation(
0, 360,
Animation.RELATIVE_TO_PARENT, 1f,
Animation.RELATIVE_TO_PARENT, 0f);
rotateAnimation.setDuration(2000);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);
}
public void Scale() {
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(
1, 0.1f, 1, 0.1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(2000);
animationSet.addAnimation(scaleAnimation);
imageView.startAnimation(animationSet);
}
public void Translate() {
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, // X 軸的開始位置
Animation.RELATIVE_TO_SELF, 0.5f, // X 軸的結束位置
Animation.RELATIVE_TO_SELF, 0f, // Y 軸的開始位置
Animation.RELATIVE_TO_SELF, 1.0f);// Y 軸的結束位置
translateAnimation.setDuration(2000);
animationSet.addAnimation(translateAnimation);
/*
* 第一行的設置如果為 true,則動畫執行完之後效果定格在執行完之後的狀態
* 第二行的設置如果為 false,則動畫執行完之後效果還原
* 第三行設置的是一個 long 類型的值,是指動畫延遲多少毫秒之後執行
* 第四行定義的是動畫重複幾次執行
*/
animationSet.setFillAfter(true);
animationSet.setFillBefore(false);
animationSet.setStartOffset(2000);
animationSet.setRepeatCount(3);
imageView.startAnimation(animationSet);
}
public void Alpha_Translate() {
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
alphaAnimation.setDuration(2000);
animationSet.addAnimation(alphaAnimation);
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1.0f);
translateAnimation.setDuration(2000);
animationSet.addAnimation(translateAnimation);
imageView.startAnimation(animationSet);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_animations_, menu);
return true;
}
}
這段程式碼裡最值得留意的是 Translate 那段的三個設定:
setFillAfter(true):動畫播完後,效果定格在最終狀態。setFillBefore(false):動畫開始前不套用起始狀態。setStartOffset(2000):延遲 2000 毫秒才開始。setRepeatCount(3):重複播放 3 次。另外 new AnimationSet(true) 建構子中的那個 true 代表 shareInterpolator——所有子動畫共用同一個插值器。若你希望每個子動畫各自有不同的速度曲線(例如淡入用減速、位移用加速),就要改成 new AnimationSet(false) 並為各子動畫分別呼叫 setInterpolator。
Tween 動畫不影響真實屬性。前面提過但值得再強調:這些動畫只改變繪製外觀,View 的位置、大小與點擊判定區域都不變。動畫結束後物件會回到原位,這也是為什麼範例中的 Translate 要設 fillAfter=true 才能讓效果「停住」。若你需要真正改變 view 屬性、且完播後狀態要保留,請改用 Property Animation(ObjectAnimator / ViewPropertyAnimator)。
建議改用 ConstraintLayout。範例用 AbsoluteLayout 與絕對座標是為了示範單純,但它在不同螢幕尺寸下一定會走位,Android 也已標示為不建議使用。現行專案請用 ConstraintLayout 搭配相對約束。
startAnimation 會重置動畫狀態。同一個 Animation 物件被 start 第二次時,會從頭播放。若需要「疊加」效果,請建立新的 AnimationSet 而非重複使用同一個實例。
Android SQLite 增刪改查完整案例:從實體類到測試類
本文用一個「人員通訊錄」的範例,把 SQLite 的增刪改查完整跑一遍,包含實體類、資料庫幫助類、業務類與測試類四個部分。 一、Person 實體類 package c
ViewPager 實戰:畫面切換與引導頁輪播兩種寫法
先前寫過〈Android 中使用 ViewFlipper 實現屏幕切換〉。相比 ViewFlipper,ViewPager 更適合複雜的視圖切換,而且它有自己的 Adap
Android ListView 與 ProgressBar 實戰詳解:清單綁定與進度回饋一次搞懂
做 Android 介面時,有兩件事幾乎躲不掉:把一批資料排成可捲動的清單,以及告訴使用者「事情正在進行中」。前者靠 ListView,後者靠 ProgressBar。這
Windows 環境下打造 React Native for Android 開發環境(完整步驟)
行動 App 一向得分 iOS 與 Android 兩邊做,同一個產品要寫兩次。React Native 想解決的就是這件事:寫一份程式碼,輸出兩個平台的原生應用。 一