編輯:關於 Android 開發
先來看成果:點擊時按壓位置會盪開一圈半透明的圓環,最後才觸發原本的點擊邏輯,質感接近 360 手機衛士那類安全軟體的互動回饋。
自訂的 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;
}
水波紋必須被裁切在按鈕區域內,所以要先算出它的螢幕座標範圍:
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);
}
波紋本質是同心圓,需要圓心與半徑。圓心就是手指按下的 (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;
}
許多人直覺會寫在 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() 觸發下一幀。
好的水波紋效果會等動畫播完才觸發點擊。做法是:在 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 就能獲得官方水波紋。若需相容舊版本或要更細緻的控制(自訂顏色、動畫時長、點擊延遲),才需要像上面這樣自行實作。
SwipeBackLayout 使用指南:為 Activity 加上側滑返回
SwipeBackLayout 使用指南:為 Activity 加上側滑返回 1、關於 SwipeBackLayout 專案網址:https://github.com/i
用 PinnedSectionListView 打造分組懸浮的通訊錄頁面
延伸閱讀:PinnedSectionListView 的下載、用法與注意事項:https://www.cnblogs.com/zzw1994/p/4997601.html
React Native 中的 ViewPagerAndroid 使用解析
React Native 中的 ViewPagerAndroid 使用解析ViewPagerAndroid 是 React Native 中對應 Android View
聊聊 LoveLive SIF,以及即將誕生的 LL 練習器
聊聊 LoveLive SIF,以及即將誕生的 LL 練習器 基於課程需要、自身需求,以及廣大 LLer 的期待,這個學期我打算做一款造福全世界 LLer 的 Andro