編輯:關於 Android 程式設計
先上圖,看看接下來要介紹的是什麼,如下圖:

接下來要介紹的就是如何實作上圖中的波紋效果。如果沒體驗過,可以看看百度手機衛士或 360 手機衛士,裡面的按鈕點擊效果都是這樣,另外 Android 5.0 以上版本也內建了這種效果。廢話不多說,直接談談具體怎麼實作。
大家看到的是三個 button,水波紋給人的錯覺是直接把波紋繪製在 button 上,但這樣做得到嗎?button 本身有 background 和 src,若把半透明水波紋當作 background 或 src 繪上去,勢必會蓋掉 button 原有的樣式。可能有朋友會猜:那就把水波紋繪製在整個螢幕上——恭喜這位朋友答對了,至少我就是這麼做的。具體思路是:自己實作一個 layout,在其中攔截事件並做相應處理,在 down 事件中找出當前被點擊的是哪個 view、找出該 view 所在的矩形區域,將一個半透明的圓環繪製到這個區域;在 up 事件中,延遲分發 view 的 onclick 事件。
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()) { // 這說明被點擊的view找到了 targetView = child; break; } } return targetView; } 接著找出 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); } 矩形區域找到後,這裡就是我們要繪製波紋的地方。前面說過,波紋其實就是圓環,而畫圓需要知道圓心座標與半徑。圓心座標就是 down 時的 x 和 y,但半徑怎麼算才合適呢?看上面的圖可知,若 view 的寬度大於高度,點擊左下角或右下角時半徑基本等於 view 的寬度,點擊上、中、下部時半徑則落在 0 到 view 高度之間。具體計算方式看下圖:

那麼依上圖,半徑的計算方式就應該是:
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));
半徑算出來了。雖說圓心就是 down 時的 x 和 y,但有個地方要留意:繪製圓環時提供的圓心座標 x、y 是相對於 layout 的,所以計算 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 裡畫——但在我看來這答案是錯的。我們的 layout 中有很多 child view,而 layout 是個 ViewGroup,ViewGroup 繪製時的順序是:先畫自身背景、再畫自身、最後畫 child view。如果在 onDraw 中繪製波紋,後續畫出來的 child view 就會把波紋蓋住,所以我們應該等 child view 繪製完畢後再來畫波紋,這樣才能保證 child view 位於最頂層。因此要重寫 dispatchDraw 方法:
@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(); } } 在分發繪製的過程中可以看到,波紋是一段一段地繪製,如下圖:

而這一段段的波紋,正是透過繪製一個個圓環來實現的,所以在還沒畫完一個圓環時,都需要延時重繪下一個圓環。
透過上述方式,波紋效果基本完成了。但按鈕是有點擊事件的,像 360 手機衛士、百度手機衛士等,都是等波紋播放完畢後才響應點擊,所以我們這裡也要對點擊事件做延遲響應。在 up 事件中記錄此次事件的 event 並返回 true(表示消費該事件),然後在圓環繪製完畢後,再用找到的 view 去分發這個 event:
if (ev.getAction() == MotionEvent.ACTION_UP) { // 需要讓波紋繪制完畢後再執行在up中執行的方法 // if(drawedRadius==0){ // return false; // } // long totalTime = (long) (INVALID_DURATION * (drawingRadiusDegrees+5)); // // 離波紋結束的時間 // long time = (long) (totalTime - drawedRadius*totalTime / rawRadius); 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),執行 child view 事件的延遲分發。
以上就是本文的全部內容,希望對大家學習 Android 程式設計有所幫助。
Android 系統鏡像文件的打包過程深度解析
前面分析過 Android 模組的編譯過程。當所有模組都編譯完成後,就可以著手打包,最終產出 system.img、boot.img、ramdisk.img、userda
一個應用對接多家廣告平台:版本化打包與自動化編譯實踐
Android 的廣告平台多如牛毛,而各個應用市場對不同平台的接受度差別很大。開發者想接廣告,通常得先回答兩個問題: 接哪個平台才賺錢? 接哪個平台才不會被市場拒絕?
Android Clean Architecture 實戰與快取改造
Clean Architecture 的核心精神 在 Android 專案中,Retrofit、RxJava 與 MVP 已相當常見,而 Clean Architectu
Android 設計模式:從 ListView 與 TimeInterpolator 看策略模式
策略模式的核心策略模式將不同演算法封裝為可互換的實作,外部只依賴抽象介面。這樣一來,當需求變化時,不需要大幅修改呼叫端。案例一:ListAdapterListView 透