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

Android 為 Button 加上點擊水波紋效果的實作解析

編輯:關於 Android 開發

Android 點擊 Button 水波紋效果

先來看成果:點擊時按壓位置會盪開一圈半透明的圓環,最後才觸發原本的點擊邏輯,質感接近 360 手機衛士那類安全軟體的互動回饋。

1. 先找到被點擊的子 View

自訂的 ViewGroup 需要攔截事件,並判斷手指按下的位置落在哪個可點擊的子 View 上:

public View findTargetView(float x, float y, View anchorView) {
    ArrayList<View> touchablesView = anchorView.getTouchables();
    View targetView = null;
    for (View child : touchablesView) {
        RectF rectF = getViewRectF(child);
        if (rectF.contains(x, y) && child.isClickable()) {
            targetView = child;
            break;
        }
    }
    return targetView;
}

2. 取得該 View 的矩形範圍

水波紋必須被裁切在按鈕區域內,所以要先算出它的螢幕座標範圍:

public RectF getViewRectF(View view) {
    int[] location = new int[2];
    view.getLocationOnScreen(location);
    int childLeft = location[0];
    int childTop = location[1];
    int childRight = childLeft + view.getMeasuredWidth();
    int childBottom = childTop + view.getMeasuredHeight();
    return new RectF(childLeft, childTop, childRight, childBottom);
}

3. 計算波紋半徑

波紋本質是同心圓,需要圓心與半徑。圓心就是手指按下的 (x, y);半徑則要能覆蓋整個按鈕——觀察可知,當按鈕寬度大於高度時,点在左右兩側時半徑約等於寬度,点在上下時則介於 0 與高度之間。因此取「按壓點到四邊的最大距離」即可:

float left = circleCenterX - targetTouchRectF.left;
float right = targetTouchRectF.right - circleCenterX;
float top = circleCenterY - targetTouchRectF.top;
float bottom = targetTouchRectF.bottom - circleCenterY;
// 四個方向取最大值作為半徑
rawRadius = Math.max(bottom, Math.max(Math.max(left, right), top));

由於繪製時的圓心座標是相對於版面而非螢幕,Y 值需要扣掉容器自身的螢幕偏移:

/** 取得圓環中心座標(相對於版面) */
public float[] getCircleCenterPostion(float x, float y) {
    int[] location = new int[2];
    float[] mDownPositon = new float[2];
    getLocationOnScreen(location);
    mDownPositon[0] = x;
    mDownPositon[1] = y - location[1];
    return mDownPositon;
}

4. 在哪裡繪製?dispatchDraw 而非 onDraw

許多人直覺會寫在 onDraw(),但這是錯的。ViewGroup 的繪製順序是「自身背景 → 自身內容 → 子 View」,若在 onDraw() 畫波紋,稍後繪製的子 View 會把它蓋住。正確做法是覆寫 dispatchDraw(),等子 View 畫完再疊上波紋:

@Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);
    // 子元素繪製完畢後才畫波紋
    if (mTargetTouchView != null) {
        RectF clipRectF = clipRectF(mTargetTouchView);
        canvas.save();
        canvas.clipRect(clipRectF); // 避免圓環超出範圍
        if (drawedRadius < rawRadius) {
            drawedRadius += rawRadius / drawingRadiusDegrees;
            canvas.drawCircle(mDownPositon[0], mDownPositon[1], drawedRadius, mHalfTransPaint);
            postInvalidateDelayed(INVALID_DURATION);
        } else {
            canvas.drawCircle(mDownPositon[0], mDownPositon[1], rawRadius, mTransPaint);
            post(delayedRunnable);
        }
        canvas.restore();
    }
}

波紋之所以看起來是連續擴散,其實是一次次重繪半徑遞增的圓環,每畫完一輪就延時 postInvalidateDelayed() 觸發下一幀。

5. 延後回應點擊事件

好的水波紋效果會等動畫播完才觸發點擊。做法是:在 ACTION_UP 時暫存事件並回傳 true 消費掉,待圓環繪製完成後再重新分發:

if (ev.getAction() == MotionEvent.ACTION_UP) {
    // 讓波紋繪製完畢後再執行原本在 UP 中的行為
    delayedRunnable.event = ev;
    return true;
}

class postUpEventDelayed implements Runnable {
    private MotionEvent event;
    @Override
    public void run() {
        if (mTargetTouchView != null && mTargetTouchView.isClickable() && event != null) {
            mTargetTouchView.dispatchTouchEvent(event);
        }
    }
}

對應地,在 dispatchDraw() 判斷波紋繪製完成後呼叫 post(delayedRunnable),即可完成事件的延遲派送。

補充

從 API 21 起,View 已內建 ripple 效果,透過 ?attr/selectableItemBackground 或主题中的 android:colorControlHighlight 就能獲得官方水波紋。若需相容舊版本或要更細緻的控制(自訂顏色、動畫時長、點擊延遲),才需要像上面這樣自行實作。

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