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

Android 雷達掃描動畫 UI 實作解析

編輯:關於 Android 程式設計

「搜尋附近裝置」這種介面很適合用雷達掃描的視覺效果來表達——一圈圈擴散的波紋加上旋轉的掃描線,比單純的轉圈圈更有科技感。本文拆解一個以自定義 RelativeLayout 為基礎的雷達效果實作。

一、基類:BaseView

先把常用的三個建構子與 context 封裝起來,並保留 onDraw 的擴充點,後續所有自定義 View 都從這裡繼承。

public class BaseView extends RelativeLayout {

    public Context context;

    public BaseView(Context context) {
        super(context);
        this.context = context;
    }

    public BaseView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public BaseView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
}

二、雷達本體:SearchDevicesView

這個 View 由三張圖組成:雷達背景、中心按鈕、以及需要旋轉的掃描臂。掃描時靠 offsetArgs 累加角度並 invalidate() 觸發重繪,形成連續旋轉。

public class SearchDevicesView extends BaseView {

    public static final String TAG = "SearchDevicesView";
    public static final boolean D = BuildConfig.DEBUG;

    int[] lineColor = new int[]{0x7B, 0x7B, 0x7B};
    int[] innerCircle0 = new int[]{0xb9, 0xff, 0xFF};
    int[] innerCircle1 = new int[]{0xdf, 0xff, 0xFF};
    int[] innerCircle2 = new int[]{0xec, 0xff, 0xFF};
    int[] argColor = new int[]{0xF3, 0xf3, 0xfa};

    private float offsetArgs = 0;
    private boolean isSearching = false;

    private Bitmap bitmap;   // 雷達背景
    private Bitmap bitmap1;  // 中心按鈕
    private Bitmap bitmap2;  // 掃描臂

    public boolean isSearching() {
        return isSearching;
    }

    public void setSearching(boolean isSearching) {
        this.isSearching = isSearching;
        offsetArgs = 0;
        invalidate();
    }

    public SearchDevicesView(Context context) {
        super(context);
        initBitmap();
    }

    public SearchDevicesView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initBitmap();
    }

    public SearchDevicesView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initBitmap();
    }

    private void initBitmap() {
        if (bitmap == null) {
            bitmap = BitmapFactory.decodeResource(
                    context.getResources(), R.drawable.gplus_search_bg);
        }
        if (bitmap1 == null) {
            bitmap1 = BitmapFactory.decodeResource(
                    context.getResources(), R.drawable.locus_round_click);
        }
        if (bitmap2 == null) {
            bitmap2 = BitmapFactory.decodeResource(
                    context.getResources(), R.drawable.gplus_search_args);
        }
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 1. 畫背景(置中)
        canvas.drawBitmap(bitmap,
                getWidth() / 2 - bitmap.getWidth() / 2,
                getHeight() / 2 - bitmap.getHeight() / 2, null);

        // 2. 畫掃描臂
        if (isSearching) {
            Rect rMoon = new Rect(getWidth() / 2 - bitmap2.getWidth(),
                    getHeight() / 2,
                    getWidth() / 2,
                    getHeight() / 2 + bitmap2.getHeight());
            canvas.rotate(offsetArgs, getWidth() / 2, getHeight() / 2);
            canvas.drawBitmap(bitmap2, null, rMoon, null);
            offsetArgs = offsetArgs + 3;   // 每幀旋轉 3 度
        } else {
            canvas.drawBitmap(bitmap2,
                    getWidth() / 2 - bitmap2.getWidth(),
                    getHeight() / 2, null);
        }

        // 3. 畫中心按鈕
        canvas.drawBitmap(bitmap1,
                getWidth() / 2 - bitmap1.getWidth() / 2,
                getHeight() / 2 - bitmap1.getHeight() / 2, null);

        // 4. 持續重繪以維持動畫
        if (isSearching) {
            invalidate();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                handleActionDownEvenet(event);
                return true;
            case MotionEvent.ACTION_MOVE:
                return true;
            case MotionEvent.ACTION_UP:
                return true;
        }
        return super.onTouchEvent(event);
    }

    private void handleActionDownEvenet(MotionEvent event) {
        RectF rectF = new RectF(
                getWidth() / 2 - bitmap1.getWidth() / 2,
                getHeight() / 2 - bitmap1.getHeight() / 2,
                getWidth() / 2 + bitmap1.getWidth() / 2,
                getHeight() / 2 + bitmap1.getHeight() / 2);
        if (rectF.contains(event.getX(), event.getY())) {
            if (D) {
                Log.d(TAG, "click search device button");
            }
            setSearching(!isSearching());
        }
    }
}

三、幾個可以更好的地方

1. 用 Rect 取代 Rect 的頻繁配置。onDraw 中每次都 new 一個 Rect,會造成不必要的 GC 壓力。把 Rect 提升為成員變數、在 onSizeChanged 中計算一次即可。

2. 用 invalidate 驅動動畫並非最佳解。這裡靠 onDraw 內部呼叫 invalidate 形成迴圈,雖然簡單直覺,但幀率與系統同步不好控制。若追求流暢度,建議改用 ValueAnimator 或 ObjectAnimator 驅動角度,並記得在 onDetachedFromWindow 中取消動畫,避免離開畫面後仍在空轉耗電。

3. 角度遞增要注意溢位與精度。offsetArgs 長期累加會不斷變大,可以隨時對 360 取餘數,保持數值穩定。

4. 觸摸回饋。目前只在 ACTION_DOWN 中切換狀態,實務上建議加上按下時的縮放或顏色變化,讓使用者明確感受到「按到了」。

四、成果下載

完整專案可參考:https://download.csdn.net/detail/u014600432/8248689

總體來說,雷達效果的本質就是「旋轉一張扇形圖 + 持續重繪」。掌握了 Canvas 的 rotate 與中心點設定,要換成擴散圓環、漸層波紋等風格都只是替換繪製邏輯而已。

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