編輯:關於 Android 程式設計

Android 提供了 AlphaAnimation(透明度)、ScaleAnimation(縮放)、TranslateAnimation(平移)、RotateAnimation(旋轉)四種基本動畫。使用方式有兩種:XML 與 Java 程式碼,兩者都相當簡單。此外還有 AnimationSet,可將多種動畫組合在一起使用。
建立縮放/透明度動畫集合:
// 建立 AnimationSet 物件 aSet = new AnimationSet(false); // 建立縮放動畫 sAnim = new ScaleAnimation(1, 0.5f, 1, 0.5f); sAnim.setDuration(3000); aSet.addAnimation(sAnim); // 建立透明度動畫 aAnim = new AlphaAnimation(1, 0.5f); aAnim.setDuration(3000); aSet.addAnimation(aAnim);
開始執行動畫:
btn_sys.setAnimation(aSet);
如此便完成了一個動畫集合的小範例,其他幾種動畫用法類似。
這四種動畫其實都是 Animation 的子類別,若要實現動畫效果,必須重寫 applyTransformation() 方法——這一點可從該方法的註解看出:
/** * Helper for getTransformation. Subclasses should implement this to apply * their transforms given an interpolation value. */ protected void applyTransformation(float interpolatedTime, Transformation t) { }
下面以 ScaleAnimation 為例分析,其餘三種可自行推敲。
先看建構子:
public ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY) { mResources = null; mFromX = fromX; mToX = toX; mFromY = fromY; mToY = toY; mPivotXType = ABSOLUTE; mPivotYType = ABSOLUTE; mPivotXValue = pivotX; mPivotYValue = pivotY; initializePivotPoint(); }
它只是將傳入的變數賦值,然後呼叫 initializePivotPoint():
private void initializePivotPoint() { if (mPivotXType == ABSOLUTE) { mPivotX = mPivotXValue; } if (mPivotYType == ABSOLUTE) { mPivotY = mPivotYValue; } }
建構子執行完後,馬上會執行 initialize(),取得縮放中心點座標:
@Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); mFromX = resolveScale(mFromX, mFromXType, mFromXData, width, parentWidth); mToX = resolveScale(mToX, mToXType, mToXData, width, parentWidth); mFromY = resolveScale(mFromY, mFromYType, mFromYData, height, parentHeight); mToY = resolveScale(mToY, mToYType, mToYData, height, parentHeight); mPivotX = resolveSize(mPivotXType, mPivotXValue, width, parentWidth); mPivotY = resolveSize(mPivotYType, mPivotYValue, height, parentHeight); }
接著呼叫 start() 方法:
public void setStartTime(long startTimeMillis) { mStartTime = startTimeMillis; mStarted = mEnded = false; mCycleFlip = false; mRepeated = 0; mMore = true; } public void start() { setStartTime(-1); }
它只是設定起始時間,關鍵其實是 view.setAnimation()。若範例中不寫 view.setAnimation(),就不會出現動畫效果;而只寫 start() 雖仍可執行,但第二次會在第一次的基礎上累加,並非我們想要的結果:
public void setAnimation(Animation animation) { mCurrentAnimation = animation; if (animation != null) { if (mAttachInfo != null && !mAttachInfo.mScreenOn && animation.getStartTime() == Animation.START_ON_FIRST_FRAME) { animation.setStartTime(AnimationUtils.currentAnimationTimeMillis()); } animation.reset(); } }
接著執行 applyTransformation(),而它由 getTransformation() 呼叫:
public boolean getTransformation(long currentTime, Transformation outTransformation) { if (mStartTime == -1) { mStartTime = currentTime; } final long startOffset = getStartOffset(); final long duration = mDuration; float normalizedTime; if (duration != 0) { normalizedTime = ((float) (currentTime - (mStartTime + startOffset))) / (float) duration; } else { normalizedTime = currentTime < mStartTime ? 0.0f : 1.0f; } final boolean expired = normalizedTime >= 1.0f; mMore = !expired; if (!mFillEnabled) normalizedTime = Math.max(Math.min(normalizedTime, 1.0f), 0.0f); if ((normalizedTime >= 0.0f || mFillBefore) && (normalizedTime <= 1.0f || mFillAfter)) { if (!mStarted) { fireAnimationStart(); mStarted = true; } if (mFillEnabled) normalizedTime = Math.max(Math.min(normalizedTime, 1.0f), 0.0f); if (mCycleFlip) { normalizedTime = 1.0f - normalizedTime; } final float interpolatedTime = mInterpolator.getInterpolation(normalizedTime); applyTransformation(interpolatedTime, outTransformation); } if (expired) { if (mRepeatCount == mRepeated) { if (!mEnded) { mEnded = true; fireAnimationEnd(); } } else { if (mRepeatCount > 0) { mRepeated++; } if (mRepeatMode == REVERSE) { mCycleFlip = !mCycleFlip; } mStartTime = -1; mMore = true; fireAnimationRepeat(); } } if (!mMore && mOneMoreTime) { mOneMoreTime = false; return true; } return mMore; }
其中最重要的程式碼是 applyTransformation(interpolatedTime, outTransformation),它會呼叫 applyTransformation 透過矩陣實現變換。
那麼 ScaleAnimation 具體如何做到縮放?整個縮放動畫不到 300 行,真正起決定作用的只有幾十行:
@Override protected void applyTransformation(float interpolatedTime, Transformation t) { float sx = 1.0f; float sy = 1.0f; float scale = getScaleFactor(); if (mFromX != 1.0f || mToX != 1.0f) { sx = mFromX + ((mToX - mFromX) * interpolatedTime); } if (mFromY != 1.0f || mToY != 1.0f) { sy = mFromY + ((mToY - mFromY) * interpolatedTime); } if (mPivotX == 0 && mPivotY == 0) { t.getMatrix().setScale(sx, sy); } else { t.getMatrix().setScale(sx, sy, scale * mPivotX, scale * mPivotY); } }
由此可見,動畫最終還是透過矩陣變換實現。interpolatedTime 表示插值器的結果:先取得縮放比例,再依不同時間段取得不同的 sx 值,最後透過矩陣設定。這個方法會與 getTransformation() 一起執行,直到動畫結束。
既然能用 AnimationSet 結合 ScaleAnimation 與 AlphaAnimation,那麼能否自定義一個 Animation 來達到同樣效果?答案是肯定的。
a. 首先繼承 Animation:
public class ScaleAndAlphaAnimation extends Animation
b. 透過建構子傳入所需參數:
public ScaleAndAlphaAnimation(float fromX, float toX, float fromY, float toY, float fromAlpha, float toAlpha){ this.mFromX = fromX; this.mFromY = fromY; this.mToX = toX; this.mToY = toY; this.mFromAlpha = fromAlpha; this.mToAlpha = toAlpha; }
c. 最後重寫 applyTransformation,參照 ScaleAnimation 與 AlphaAnimation 的源碼:
@Override protected void applyTransformation(float interpolatedTime, Transformation t) { // 縮放動畫 float sx = 1.0f; float sy = 1.0f; if (mFromX != 1.0f || mToX != 1.0f) { sx = mFromX + ((mToX - mFromX) * interpolatedTime); } if (mFromY != 1.0f || mToY != 1.0f) { sy = mFromY + ((mToY - mFromY) * interpolatedTime); } t.getMatrix().setScale(sx, sy); // 透明度動畫 final float alpha = mFromAlpha; t.setAlpha(alpha + ((mToAlpha - alpha) * interpolatedTime)); }
透過 log 輸出可以觀察到,這幾個方法的執行順序為:建構子 → initialize() → 接著 applyTransformation() 與 getTransformation() 反覆執行直到動畫結束(先 applyTransformation() 後 getTransformation())。
ScaleAndAlphaAnimation.ScaleAndAlphaAnimation() ScaleAndAlphaAnimation.initialize() ScaleAndAlphaAnimation.applyTransformation() ScaleAndAlphaAnimation.getTransformation() ScaleAndAlphaAnimation.applyTransformation() ScaleAndAlphaAnimation.getTransformation() ...
本篇就介紹到這裡,下篇將繼續分析動畫,並附上完整源碼。
Android Service 基礎學習
Service 簡介Service 是 Android 四大元件之一,適合在背景執行不需與使用者互動的工作,例如播放音樂、監聽變化或記錄位置。啟動方式startServi
微信讀書怎麼換背景顏色 三步調整閱讀底色
騰訊推出的微信讀書引來不少討論,究竟它能否兼顧閱讀與社交?答案也許還需要時間驗證。不過使用中確實有些小問題常被問到,例如微信讀書怎麼設定背景顏色。 首先當然要下載並安裝微
夜神安卓模擬器怎麼玩手遊?安裝、載入 APK 與實用功能
夜神是目前效能表現相當突出的一款安卓模擬器。與傳統方案相比,它基於 Android 4.4.2,同時相容 X86 與 AMD 架構,在效能、穩定性與相容性上都有明顯改善,
Fragment + ViewPager 打造類微信底部導航,並實現消息紅點提醒
本篇整理自菜鳥教程的 Android 移動端內容,記錄如何用 Fragment 配合 ViewPager 做出一個類微信的底部導航欄,並在 Tab 上疊加未讀消息的紅色圓