編輯:關於 Android 程式設計
Android 動畫 API 提供了四種基本效果:縮放、平移、旋轉、透明度變化。這篇用純程式碼的方式把四種都跑一遍,理解參數含義之後,XML 寫法自然一通百通。
用 RelativeLayout 依序排出一個 ImageView 與四個按鈕,每個按鈕觸發一種動畫:
<ImageView
android:id="@+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/scaleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imgLayout"
android:text="@string/scale" />
<Button
android:id="@+id/translateButton"
android:layout_below="@id/scaleButton"
... />
<Button
android:id="@+id/rotateButton"
android:layout_below="@id/translateButton"
... />
<Button
android:id="@+id/alphaButton"
android:layout_below="@id/rotateButton"
... />
ImageView 建議包在一個有 padding 的 LinearLayout 裡,這樣縮放與平移時才不會貼邊。
class setScaleListener implements OnClickListener {
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
// 從 (1,1) 縮小到 (0.5,0.5)
Animation animation = new ScaleAnimation(1f, 0.5f, 1f, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(1000);
animation.setStartOffset(1000);
animation.setRepeatCount(0);
animation.setFillAfter(true);
animationSet.addAnimation(animation);
imageView.startAnimation(animation);
}
}
ScaleAnimation 有六個參數,前四個是 X 與 Y 的起訖比例,後四個是縮放中心的定位方式與位置。這裡用 RELATIVE_TO_SELF 搭配 0.5,表示以自身中心為縮放點——若寫成 RELATIVE_TO_PARENT,同樣的 0.5 就會變成父容器的中心,效果完全不同。
setStartOffset(1000) 讓動畫延遲一秒才開始;setFillAfter(true) 讓動畫結束後停在最終狀態,不會跳回原樣。這兩個是實務上最常用的設定。
class setTranslateListener implements OnClickListener {
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
// 從 (0,0) 移到 (1,1)
Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f);
animation.setDuration(1000);
animationSet.addAnimation(animation);
imageView.startAnimation(animation);
}
}
參數依序是:起點 X 的類型與值、終點 X 的類型與值、起點 Y 的類型與值、終點 Y 的類型與值。類型可選 RELATIVE_TO_SELF(自身比例)、RELATIVE_TO_PARENT(父容器比例)或絕對像素值。
class setRotateListener implements OnClickListener {
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
// 0 度轉到 360 度,以自身底部中心為軸
Animation animation = new RotateAnimation(0f, 360f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f);
animation.setDuration(1000);
animationSet.addAnimation(animation);
imageView.startAnimation(animation);
}
}
五個參數:起始角度、結束角度、X 軸類型與值、Y 軸類型與值。正值為順時針,負值為逆時針。旋轉軸的設定邏輯與縮放中心相同。
class setAlphaListener implements OnClickListener {
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
// 1 代表完全可見,0 代表完全透明
Animation animation = new AlphaAnimation(1, 0);
animation.setDuration(1000);
animationSet.addAnimation(animation);
imageView.startAnimation(animation);
}
}
兩個參數分別是起始與結束的透明度。1 到 0 是淡出,0 到 1 是淡入。
第一,範例中每個監聽器都建立了 AnimationSet,卻只把單一動畫加進去,最後呼叫 imageView.startAnimation(animation) 啟動的是動畫本身而非集合。若確實要組合多個動畫,應該啟動 animationSet,並注意 AnimationSet 的建構子參數 true 表示共用同一個插值器。
第二,setRepeatCount 若要無限循環可設為 Animation.INFINITE,搭配 setRepeatMode 決定是重播(RESTART)還是來回(REVERSE)。
第三,View 動畫只改變繪製效果,不真正改變 View 的位置與狀態。也就是說平移之後,View 的實際點擊區域仍留在原位,這是初學者最常遇到的困惑。若需要真正的位置變更,應該用属性動畫(ObjectAnimator / ValueAnimator)。
第四,記得在 MainActivity 的 onCreate 中把四個按鈕與對應監聽器綁定,並透過 findViewById 取得 myImage 參照。
AppWidget 原始碼分析:API 類與跨進程更新
API 類總覽 AppWidget 是 Android 桌面小工具的重要機制。開發者若要實作一個可被桌面使用的元件,通常會接觸到 AppWidgetProvider、Ap
自製安全鍵盤:用 KeyboardView 打造字母、數字、符號三合一輸入盤
一直知道 Android 有 KeyboardView 這個元件可以做自訂鍵盤,只是過去沒派上用場。某天看到某銀行 App 的密碼輸入鍵盤,配色與排版都很講究,於是決定動
Android 5.0 RecyclerView 使用指南:適配器、布局管理器與分割線
RecyclerView 的定位 Android 5.0 引入 RecyclerView 後,列表與網格類應用有了更靈活的選擇。相比傳統 ListView 與 GridV
Android View 繪製流程解析:measure、layout 與 draw
Android 的 View 繪製流程可以概括為 measure、layout 與 draw 三個階段。當 Activity 取得焦點後,系統會從根節點開始,對整棵 Vi