編輯:關於 Android 程式設計
移動互聯網發展到今天,App 之間的競爭早已不只看功能,界面呈現與交互手感同樣關鍵。動畫已經成為交互中不可分割的一部分。平時記錄的知識點比較零碎,這次做個統一整理,順便溫故知新。
Android 系統提供三種實現動畫的方式:幀動畫(Frame / Drawable Animation)、補間動畫(Tween / View Animation)、屬性動畫(Property Animation)。其中前兩者在 API 11 之前就已推出,屬性動畫則是 API 11 之後新增的。
下面分別說明它們的用法。
幀動畫在 SDK 中被稱為 Drawable Animation,原理和 GIF 圖片一樣:把一系列 Drawable 資源依次播放,模擬出動畫效果。Java 中對應的類是 AnimationDrawable。
實現步驟如下。
第一步,在 res/drawable 目錄下建立資源文件。注意早期教程常說放在 res/anim,但在 Android Studio 中必須放 drawable 目錄,否則會報錯。
<animation-list xmlns:android="https://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/blank01" android:duration="200" />
<item android:drawable="@drawable/blank02" android:duration="200" />
<item android:drawable="@drawable/blank03" android:duration="200" />
<item android:drawable="@drawable/blank04" android:duration="200" />
<item android:drawable="@drawable/blank05" android:duration="200" />
<item android:drawable="@drawable/blank06" android:duration="200" />
</animation-list>
幾個要點:animation-list 必須作為根元素;android:oneshot 為 true 表示只播放一次,false 表示循環;每個 item 引用一張靜態圖片,代表一幀獨立畫面;android:duration 是整數,表示該幀顯示時長,單位毫秒。
第二步,在 View 上設置動畫。可以直接在 XML 裡寫 android:background,也可以用代碼調用 setBackgroundResource。
第三步,用 AnimationDrawable 控制播放:
imageView = (ImageView) findViewById(R.id.imageView); imageView.setBackgroundResource(R.drawable.run); animationDrawable = (AnimationDrawable) imageView.getBackground(); animationDrawable.start(); // 開始播放 animationDrawable.stop(); // 停止播放
補間動畫是對 View 做一系列圖形變換來實現的,包括縮放、平移、旋轉、漸變四種。由於操作對象是 View 控件,所以也叫 View Animation。
View 動畫資源統一放在 res/anim 目錄,建議命名為 [alpha | rotate | scale | translate | set]_anim.xml。
漸變(alpha)效果的資源文件:
<alpha xmlns:android="https://schemas.android.com/apk/res/android"
android:duration="500"
android:fillAfter="true"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
fromAlpha 是起始透明度,toAlpha 是結束透明度,取值 0.0 到 1.0:0.0 全透明,1.0 完全不透明。duration 單位毫秒。fillAfter 為 true 時,動畫結束後效果仍會保留應用在 View 上。
旋轉(rotate)效果的資源文件:
<rotate xmlns:android="https://schemas.android.com/apk/res/android"
android:duration="500"
android:fillAfter="true"
android:fromDegrees="0"
android:toDegrees="360"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%" />
interpolator 指定插值器,常用三種:accelerate_decelerate_interpolator(先加速後減速)、accelerate_interpolator(加速)、decelerate_interpolator(減速)。fromDegrees 與 toDegrees 分別是起止角度,數值可以大於 360。角度為負代表逆時針,為正代表順時針。pivotX / pivotY 是旋轉中心相對於 View 的百分比位置,50% 即中心點。
平移(translate)效果的資源文件:
<translate xmlns:android="https://schemas.android.com/apk/res/android"
android:duration="500"
android:fillAfter="true"
android:fromXDelta="0"
android:toXDelta="200"
android:fromYDelta="0"
android:toYDelta="0" />
fromXDelta、toXDelta、fromYDelta、toYDelta 分別是 X、Y 方向上的起止位置。若不特別指定 fromXType / toXType / fromYType / toYType,默認以自身為相對參照物。
縮放(scale)效果的資源文件:
<scale xmlns:android="https://schemas.android.com/apk/res/android"
android:duration="500"
android:fillAfter="false"
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:interpolator="@android:anim/decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%" />
四個 scale 屬性中,0.0 表示收縮到無,1.0 表示正常大小,小於 1.0 是縮小,大於 1.0 是放大。pivotX / pivotY 同樣取 0% 到 100%。
組合動畫(set)的資源文件:
<set xmlns:android="https://schemas.android.com/apk/res/android"
android:duration="500"
android:fillAfter="true"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:shareInterpolator="true">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%" />
<scale
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%" />
</set>
set 作為根元素是一個動畫集合,裡面可以包含 rotate、translate、scale、alpha 中的一個或多個作為子節點。特別注意:如果希望動畫結束後效果保留在 View 上,android:fillAfter 必須寫在 set 上,寫在子節點上不生效。
Animation scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.scale_anim); imageView.startAnimation(scaleAnimation);
與幀動畫不同,這裡要用 AnimationUtils 加載動畫資源生成 Animation 對象,再透過 startAnimation 啟動。
每種動畫類型都有對應的類:scale 對應 ScaleAnimation,translate 對應 TranslateAnimation,rotate 對應 RotateAnimation,alpha 對應 AlphaAnimation,set 對應 AnimationSet。
ScaleAnimation scaleAnimation = new ScaleAnimation(
0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 200.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
RotateAnimation rotateAnimation = new RotateAnimation(
0.0f, +350.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
AnimationSet animationSet = new AnimationSet(true);
animationSet.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(scaleAnimation);
imageView.startAnimation(animationSet);
如果需要精確控制動畫過程,可以掛上監聽器:
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) { /* 動畫開始 */ }
@Override
public void onAnimationEnd(Animation animation) { /* 動畫結束 */ }
@Override
public void onAnimationRepeat(Animation animation) { /* 動畫重複運行 */ }
});
關鍵區別在這裡:補間動畫只是改變了 View 的顯示效果,並沒有修改 View 的真實屬性——位置、寬高其實都沒變,只是「看起來」變了。而屬性動畫能真正改變對象的屬性。
上圖是屬性動畫的五大核心類,其中最常用的是三個:
ValueAnimator:對一個值做平滑的動畫過渡,本身不作用於具體對象。
ObjectAnimator:可以直接對任意對象的任意屬性做動畫操作,但前提是目標對象必須提供該屬性的 get 和 set 方法。
AnimatorSet:同時操作一個或多個屬性動畫,並按特定順序執行,作用類似 AnimationSet 但用法不同。
屬性動畫資源放在 res/animator 目錄,建議命名為 [value | objectvalue | set]_animator.xml。可用的三種標籤分別對應 ValueAnimator、ObjectAnimator、AnimatorSet。
<valueAnimator xmlns:android="https://schemas.android.com/apk/res/android"
android:duration="500"
android:repeatCount="-1"
android:repeatMode="reverse"
android:startOffset="0"
android:valueFrom="#FF0000"
android:valueTo="#0000FF"
android:valueType="colorType" />
幾個屬性含義:duration 是時長,單位毫秒,默認 300ms;repeatCount 是重複次數,-1 表示無限循環,0 表示不重複;repeatMode 中 reverse 表示反向播放,restart 表示每次從頭開始;startOffset 是延遲時間;valueFrom 與 valueTo 是起止值,類型需與 valueType 對應。valueType 有四種:colorType(顏色變化)、intType(整數變化)、floatType(小數變化)、pathType(路徑變化,5.0 新增,配合 VectorDrawable 使用)。
在代碼中調用 animator 資源:
ValueAnimator animator = (ValueAnimator) AnimatorInflater.loadAnimator(
this, R.animator.value_animator);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
number.setBackgroundColor((Integer) animation.getAnimatedValue());
}
});
animator.start();
注意與 View Animation 的差別:加載屬性動畫資源要用 AnimatorInflater,生成的是 Animator 對象。
ObjectAnimator 資源的調用需要多一步:
Animator objectAnimator = AnimatorInflater.loadAnimator(this, R.animator.valueobject_animator); objectAnimator.setTarget(imageView); objectAnimator.start();
必須調用 setTarget 才能把動畫關聯到具體 View 上,否則屬性更新無處落地。
AnimatorSet 資源的調用同理:
Animator animator = AnimatorInflater.loadAnimator(this, R.animator.set); animator.setTarget(imageView); animator.start();
set 標籤中的 android:ordering 用來控制播放順序,together 表示同時執行,sequentially 表示依次執行。
// ValueAnimator
ValueAnimator animator = ValueAnimator.ofInt(1, 100);
animator.setDuration(1000);
animator.setInterpolator(new DecelerateInterpolator());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
number.setText(String.valueOf(animation.getAnimatedValue()));
}
});
animator.start();
// ObjectAnimator
ObjectAnimator animator = new ObjectAnimator();
animator.setDuration(500); // 播放時長
animator.setPropertyName("translationX"); // 要修改的屬性
animator.setTarget(imageView); // 目標對象
animator.setFloatValues(0, 500, 0); // 屬性值範圍
animator.setStartDelay(2000); // 延遲多久開始
animator.start();
// AnimatorSet
ObjectAnimator translation = new ObjectAnimator();
translation.setPropertyName("translationX");
translation.setFloatValues(0, 500);
ObjectAnimator rotation = new ObjectAnimator();
rotation.setPropertyName("rotationX");
rotation.setFloatValues(0, 360);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(translation).with(rotation);
animatorSet.setDuration(500);
animatorSet.setStartDelay(1000);
animatorSet.setTarget(imageView);
animatorSet.start();
順序編排的四個方法:after(Animator anim) 插入到傳入動畫之後執行;after(long delay) 延遲指定毫秒執行;before(Animator anim) 插入到傳入動畫之前執行;with(Animator anim) 與傳入動畫同時執行。
動畫監聽器可以這樣添加:
animatorSet.addListener(new AnimatorListenerAdapter()); animatorSet.addListener(new AnimatorListener());
第一,性質不同。AnimatorSet 是屬性動畫的集合,AnimationSet 是補間動畫的集合。
第二,能力不同。AnimationSet 透過 addAnimation 把多個動畫組織到一起,再調用 View 的 startAnimation 觸發,功能相對簡單,無法對集合中的動畫做精細的順序編排。更關鍵的是,它屬於針對視圖外觀的動畫:動畫生效時外觀改變,但視圖的觸發點仍留在原來的位置——這也是補間動畫最容易被詬病的地方,用戶看到按鈕移動了,點擊卻沒反應。
AnimatorSet 則透過 play、before、with、after 等方法設置執行順序,再調用 start 觸發。它是針對視圖屬性的動畫:動畫生效時對象屬性真正發生變化,外觀隨之改變,觸發點也同步跟著移動。
理解這一點,選型就不糾結了:只是想要個視覺效果、不涉及交互響應,補間動畫更輕量;需要真實改變狀態、後續還要讀取或依賴這些屬性,就該用屬性動畫。
小米「請勿遮擋橙色區域」怎麼解?防誤觸與距離感測器校正
小米手機出現請勿遮擋橙色區域,通常是防誤觸或距離感測器作用。可先到設定、鎖屏和密碼,關閉防誤觸模式。 若仍異常,可在撥號盤輸入工程模式,進入距離感應器測試,將手指靠近感測
msm8909 Android 5.1.1 打開調試串口
本文記錄在 msm8909 平台上開啟 Android 5.1.1 調試串口的方法。不同主板版本使用的 UART 與 GPIO 不同,需要修改的代碼也不同。公共配置內核需
Android 常用控件全覽:Activity 類列表控件與適配器體系
列表與適配器是 Android UI 開發的地基。本文把 Activity 層面幾個常用列表類控件、以及適配器的類型劃分與工作流程做系統整理,適合當作速查手冊。 一、Ac
手機QQ怎麼完全退出 兩種登出方式說明
有時關掉了手機QQ,卻仍然能收到消息,想徹底退出該怎麼做?下面我們一起來看。 手機QQ退出登入教學 方法一:退出QQ程序 第一步:打開手機QQ。 第二步:點擊手機上的【菜