Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 動畫實戰:復刻樂動力旋轉動畫

編輯:關於 Android 程式設計

一、前言

在 App 泛濫的市場上,各家功能抄來抄去,想做出精品,沒有自己的風格、沒有讓用戶眼前一亮的效果,很難留住人——隨時都會被下一個替代品頂掉。「簡約而不簡單」「用戶體驗至上」的口號喊了這麼多年,真正做到的卻寥寥無幾,很多時候設計師想著想著就把初衷忘了。

吐槽到此為止。今天來拆一個樂動力裡挺有意思的動畫效果,看著挺爽,正好拿來練手熟悉一下 Android 動畫。

二、動畫分解

整個效果拆開來其實只有兩部分:

  1. 中間那塊電池圖標在做「變大 → 變小」的循環呼吸動畫。
  2. 上下兩個箭頭圍繞各自基點不停旋轉,左右兩個小圖標則一邊自轉一邊繞中心公轉。

想清楚這一層,剩下的就是選對 API:中間用 ScaleAnimation 配合 setStartOffset 做先後銜接,其餘用 RotateAnimation,複雜的則用 AnimationSet 把自轉與公轉疊在一起。

三、自定義 GuideView

我把整個動畫封裝在一個 ViewGroup 裡,裡面掛五個 ImageView,並在 onLayout 中完成定位與動畫啟動:

public class GuideView extends ViewGroup {
    private ImageView left, right, top, bottom, center;
    private int screenW;
    private int screenH;
    private List<Bitmap> bitmaps = new ArrayList<Bitmap>();
    private LinearInterpolator interpolator = new LinearInterpolator();

    private void init(Context context) {
        left   = createView(context, R.drawable.day);
        right  = createView(context, R.drawable.night);
        top    = createView(context, R.drawable.up);
        bottom = createView(context, R.drawable.down);
        center = createView(context, R.drawable.animation_battery);

        screenW = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth();
        screenH = ((Activity) context).getWindowManager().getDefaultDisplay().getHeight();

        this.addView(center);
        this.addView(left);
        this.addView(right);
        this.addView(top);
        this.addView(bottom);
    }

    private ImageView createView(Context context, int resId) {
        ImageView iv = new ImageView(context);
        Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), resId);
        bitmaps.add(bmp);
        iv.setImageBitmap(bmp);
        return iv;
    }

    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
        // 中間居中
        center.layout(screenW / 2 - bitmaps.get(4).getWidth() / 2,
                      screenH / 2 - bitmaps.get(4).getHeight() / 2,
                      screenW / 2 + bitmaps.get(4).getWidth() / 2,
                      screenH / 2 + bitmaps.get(4).getHeight() / 2);
        // 上下左右按各自尺寸與間距擺放,此處省略具體計算
        playCenter();
        playTop();
        playBottom();
        playLeft();
        playRight();
    }
}

左右兩個圖標的動畫是重點,需要把「繞屏幕中心公轉」和「自身反方向自旋」疊在一起,形成滾動的視錯覺:

private void playRight() {
    AnimationSet animationSet = new AnimationSet(false);

    // 繞屏幕中心公轉
    RotateAnimation rotateRight = new RotateAnimation(0, 359,
            Animation.ABSOLUTE, screenW / 2 - right.getLeft(),
            Animation.ABSOLUTE, (right.getBottom() - right.getTop()) / 2);

    // 自身反向自旋
    RotateAnimation rotateSelf = new RotateAnimation(0, -359,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);

    rotateSelf.setDuration(10 * 1000);
    rotateSelf.setInterpolator(interpolator);
    rotateSelf.setRepeatCount(-1);      // 無限循環

    rotateRight.setDuration(10 * 1000);
    rotateRight.setRepeatCount(-1);
    rotateRight.setInterpolator(interpolator);

    animationSet.addAnimation(rotateSelf);
    animationSet.addAnimation(rotateRight);
    right.startAnimation(animationSet);
}

中間的呼吸動畫則靠兩個 ScaleAnimation 接力,一個放大、一個縮小,用 setStartOffset 排隊:

private void playCenter() {
    AnimationSet animationSet = new AnimationSet(false);

    ScaleAnimation scaleBig = new ScaleAnimation(1.0f, 5.0f / 3, 1.0f, 5.0f / 3,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    ScaleAnimation scaleSmall = new ScaleAnimation(1.0f, 0.6f, 1.0f, 0.6f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

    scaleBig.setDuration(2 * 1000);
    scaleBig.setStartOffset(2 * 1000);   // 先等 2 秒
    scaleBig.setRepeatCount(-1);
    scaleBig.setFillEnabled(true);
    scaleBig.setFillAfter(true);
    scaleBig.setInterpolator(interpolator);

    scaleSmall.setDuration(2 * 1000);
    scaleSmall.setStartOffset(2 * 1000);
    scaleSmall.setRepeatCount(-1);
    scaleSmall.setFillEnabled(true);
    scaleSmall.setFillAfter(true);
    scaleSmall.setInterpolator(interpolator);

    animationSet.addAnimation(scaleBig);
    animationSet.addAnimation(scaleSmall);
    center.startAnimation(animationSet);
}

四、遇到的問題

寫這個 demo 最卡的地方,是「小圖標自轉」與「繞中點公轉」這兩件事怎麼疊加。網上這方面的資料並不多,我自己試了幾輪才調對。關鍵在於理解 pivotXType / pivotYType 的三種取值:

  • Animation.RELATIVE_TO_SELF:相對於自身尺寸的比例,0.5f 就是自身中心,最常用。
  • Animation.RELATIVE_TO_PARENT:相對於父控件定位。
  • Animation.ABSOLUTE:絕對像素坐標,需要自己算好偏移。

公轉那部分用的就是 ABSOLUTE,把旋轉中心設在屏幕中心,於是圖標繞著中間那塊電池轉;自旋則用 RELATIVE_TO_SELF 的 0.5f,讓圖片繞自己的中心轉。兩者半徑不同、方向 one 正一負,滾動的感覺就出來了。

五、小結

這個效果看著花哨,代碼量其實不大,難點全在旋轉中心的計算上。把三種 pivot 類型的含義吃透,Android 裡大部分旋轉類動畫都不難下手。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved