編輯:關於 Android 程式設計
在 Android 3.0(API Level 11)以前,Android 僅支援兩種動畫:Frame Animation(逐幀動畫)與 Tween Animation(補間動畫);到了 3.0 之後,才引進了一套全新的動畫系統——Property Animation(屬性動畫)。本篇就為這三種動畫做一次完整的梳理。
顧名思義,就是一幀一幀地播放圖片,利用人眼的視覺殘留形成動畫的錯覺,原理和 GIF 圖片、電影播放完全相同。
定義方式很簡單,在 animation-list 中用 <item> 子元素定義每一幀即可,主要屬性有:
android:oneshot 是否僅播放一次
android:drawable 每一幀的圖片
android:duration 圖片間的切換間隔
習慣上會把 AnimationDrawable 設為 ImageView 的背景:
android:background="@anim/frame_anim"
接著在程式碼中取得 AnimationDrawable 物件(注意它預設不播放,要呼叫 start() 才會開始,stop() 則停止):
AnimationDrawable anim = (AnimationDrawable) imageView.getBackground();
若偏好用程式碼建立,也可以用 addFrame(Drawable frame, int duration) 逐幀加入動畫。
補間動畫只需指定開始與結束的「關鍵幀」,中間的過渡幀由系統自動補上,不必自己定義每一幀。
Android 以 Animation 代表抽象動畫,包含四種子類:AlphaAnimation(透明度)、ScaleAnimation(縮放)、TranslateAnimation(位移)、RotateAnimation(旋轉)。雖然也能在 Java 中建立,但一般都會用動畫資源檔來定義,讓界面與邏輯分離:
一個 set 可同時定義多個動畫一起執行。而 android:interpolator 則控制動畫的補幀與速度(有些地方譯為「插值」),常見實作有 LinearInterpolator、AccelerateInterpolator、AccelerateDecelerateInterpolator、CycleInterpolator、DecelerateInterpolator 等。
定義好 anim 檔後,透過 AnimationUtils 載入,再用 View 的 startAnimation 啟動即可:
Animation anim = AnimationUtils.loadAnimation(this, R.anim.anim); anim.setFillAfter(true); // 動畫結束後保留結束狀態 anim.setInterpolator(interpolator); // 設定插值效果 view.startAnimation(anim);
屬性動畫在 Android 3.0 才引進,它能直接更改物件的屬性。前面提過,Tween Animation 只是改變 View 的繪製效果,真實屬性並未改變——所以用補間動畫把按鈕從左移到右後,點擊移動後的位置反而沒反應,得點原來的位置才行。而 Property Animation 能直接改變 View 的屬性值,省下不少額外處理。
(1)ValueAnimator:包含屬性動畫的所有核心功能,如動畫時間、起訖屬性值、對應時間的屬性值計算方法等。使用時分兩步:先計算屬性值,再依值執行相應動作。重點在第二步,需實作 onUpdateListener 的 onAnimationUpdate():
animation.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// do your work
}
});
(2)ObjectAnimator:繼承自 ValueAnimator,只需指定一個物件及其屬性,值算好後會自動套用回該物件,等於一口氣完成兩個步驟。使用它需滿足:物件要有 set<PropertyName> 方法;若只給目標值,還要有 get<PropertyName> 方法,且 getter 的傳回型別須與 setter 的參數一致。
ObjectAnimator oa = ObjectAnimator.ofFloat(tv, "alpha", 0f, 1f); oa.setDuration(3000); oa.start();
(3)Animator.AnimatorListener:可為動畫設定監聽,需重寫 onAnimationStart()、onAnimationEnd()、onAnimationRepeat()、onAnimationCancel()。若只想監聽部分事件,可改用 AnimatorListenerAdapter,它為每個方法提供了空實作:
anim.addListener(new AnimatorListenerAdapter() {
public void onAnimationEnd(Animator animation) {
// do your work
}
});
(4)AnimatorSet:可組合多個動畫共同工作。
AnimatorSet bouncer = new AnimatorSet(); bouncer.play(anim1).before(anim2); bouncer.play(anim2).with(anim3); bouncer.play(anim2).with(anim4); bouncer.play(anim5).after(anim2); bouncer.start();
以上意思為:先播放 anim1,接著同時播放 anim2、anim3、anim4,最後播放 anim5。
(5)TimeInterpolator:與 Tween 中的 interpolator 類似,常見的有:
AccelerateInterpolator 加速,起步慢中間加速
DecelerateInterpolator 減速,起步快然後減速
AccelerateDecelerateInterpolator 先加速後減速
AnticipateInterpolator 反向,先往反方向移動再加速
AnticipateOvershootInterpolator 反向加回彈
BounceInterpolator 跳躍,接近目標值時反覆彈跳
CycleInterpolator 循環,值的變化為正弦函數
LinearInterpolator 線性,均勻變化
OvershootInterpolator 回彈,先超出目標再緩慢回到目標
TimeInterpolator 一個介面,可自訂 interpolator
(6)Keyframe:可定義除了起點與終點以外的關鍵幀。Keyframe 是抽象類別,需透過 ofInt()、ofFloat()、ofObject() 取得,再用 PropertyValuesHolder.ofKeyframe 組合:
Keyframe kf0 = Keyframe.ofInt(0, 400);
Keyframe kf1 = Keyframe.ofInt(0.25f, 200);
Keyframe kf2 = Keyframe.ofInt(0.5f, 400);
Keyframe kf4 = Keyframe.ofInt(0.75f, 100);
Keyframe kf3 = Keyframe.ofInt(1f, 500);
PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("width", kf0, kf1, kf2, kf4, kf3);
ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(btn, pvhRotation);
(7)ViewPropertyAnimator:非常推薦用來同時改變 View 的多種屬性,它對多屬性動畫做了優化,會合併一些 invalidate() 來減少刷新,而且寫法簡潔,一行就能完成水平和垂直移動(需 API Level 12 以上):
myView.animate().translationX(50f).translationY(100f);
(8)常需改變的屬性:
translationX、translationY 相對於原始位置的偏移量
rotation、rotationX、rotationY 旋轉
scaleX、scaleY 縮放比
x、y View 的最終座標(left/top 加上 translation)
alpha 透明度
Frame Animation(幀動畫):適合播放預先準備好的一幀幀圖片,類似 GIF。優點是簡單方便,缺點是必須事先準備每一幀。
Tween Animation(補間動畫):只需定義起訖關鍵幀,中間幀由系統補上。優點是不必準備每一幀,缺點是只改變了繪製效果而未改變 View 本身屬性,因此移動按鈕後仍得點擊原位才有效。
Property Animation(屬性動畫):3.0 後推出,優點是簡單、能直接改變物件屬性、幾乎適用於任何物件而非僅限 View,缺點是需要 3.0 以上 API 支援。所幸目前已有開源庫能為低版本提供相容。
自定義 View 實作:弧線進度條控件完整教學
這次要做的東西以弧線為主體,畫面構成很單純:一段背景圓弧、一段表示進度的圓弧、一個圓,以及中央的文字。 點擊「開始」按鈕後會播放一段動畫,進度逐漸增加。下面開始編碼。
Android Intent 機制與常見用法:頁面跳轉、傳值與系統功能呼叫
Activity 與 Intent Activity 是 Android 應用程式的核心元件,負責畫面與使用者互動。Activity 之間的跳轉與資料傳遞,通常透過 In
Android 單片機與藍牙模塊通信實例:打造紅綠燈控制 App
作為畢業設計,需要製作一個簡單的藍牙 App 來與單片機交互。透過參考資料並反覆調試,問題最終順利解決,下面把完整的實現思路分享給大家。 一、Android 藍牙編程基礎
Android 圖片開發內幕(一):Bitmap 內存計算與 OOM 防範基礎
前言:原本我做的是 TV 應用,因為公司要出手機產品、人手緊張,就被抽調去支援——誰讓我是「雷鋒」呢。當時負責的模塊是處理手機裡的應用 ICON,做美化、與底板合成裁剪,