編輯:關於 Android 程式設計
前兩天看了仿 Android L 裡面水波紋效果的兩篇博客:一篇實現了一個水波紋佈局,放在裡面的所有控件點擊後都會出現波紋效果;另一篇實現了一個水波紋 view,點擊之後自身會出現波紋效果。
根據對這兩篇博客的理解,我自己實現了一個類似的東西。沒找到合適的錄屏軟件,只好把波紋的速度調快了很多才錄下來。如下圖,就是這樣一個控件,裡面的背景用了一個重寫的 TextView,背景一直有一個不斷「呼吸」的氣泡。

這裡結合前面兩篇博客,介紹如何在 View 和 ViewGroup 中實現背景動畫,同時記錄下自己對裡面知識點的理解。暫且把這種效果命名成「會呼吸的氣泡」——整個背景有一個圓,不停地放大縮小,圓心隨機。
這裡以第一篇博客為例,記錄下自己的理解。
1. 獲取坐標,這一步比較重要,之後判斷點擊的控件還有繪制波紋都需要用到:
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); this.getLocationOnScreen(mLocationInScreen); }這裡獲取到的是當前佈局左上角在整個屏幕中的坐標。
2. 獲取點擊到的控件,這裡需要根據坐標挨個判斷。在 dispatchTouchEvent 方法裡面會傳入當前點擊事件 MotionEvent,它會帶入當前點擊的坐標。這裡有 getX 和 getRawX 兩種方式,為了計算點擊事件,這裡要獲取的是相對屏幕的坐標;因為佈局會出現嵌套,相對坐標會不準,所以全都使用相對屏幕的坐標去計算。獲取到點擊事件的坐標,就可以拿坐標去找到所點擊的控件了。
3. 拿到點擊的控件之後就要在控件上面繪制波紋了:
// view繪制流程:先繪制背景,再繪制自己(onDraw),接著繪制子元素(dispatchDraw),最後繪制一些裝飾等比如滾動條(onDrawScrollBars) // 為了防止繪制繪制的子元素把波紋擋住,這裡選擇在子元素繪制完成再繪制波紋 @Override protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); if (!mShouldDoAnimation || mTargetWidth < 0 || mTouchTarget == null) { return; } if (mRevealRadius > mMinBetweenWidthAndHeight / 2) {// 當半徑超過短邊之後,增加擴散速度盡快完成擴散 mRevealRadius += mRevealRadiusGap * 4; } else {// 波紋當半徑遞增擴散 mRevealRadius += mRevealRadiusGap; } this.getLocationOnScreen(mLocationInScreen);// 獲取本布局的坐標 int[] location = new int[2]; mTouchTarget.getLocationOnScreen(location);// 獲取點擊控件的坐標 int left = location[0] - mLocationInScreen[0]; int top = location[1] - mLocationInScreen[1]; int right = left + mTouchTarget.getMeasuredWidth(); int bottom = top + mTouchTarget.getMeasuredHeight(); canvas.save(); canvas.clipRect(left, top, right, bottom); canvas.drawCircle(mCenterX, mCenterY, mRevealRadius, mPaint); canvas.restore(); if (mRevealRadius <= mMaxRevealRadius) { postInvalidateDelayed(INVALIDATE_DURATION, left, top, right, bottom); } else if (!mIsPressed) { mShouldDoAnimation = false; postInvalidateDelayed(INVALIDATE_DURATION, left, top, right, bottom); } }重點關注畫布的切割和坐標的計算:獲取佈局與控件左上角坐標後相減,得到控件相對於佈局的坐標;在佈局的畫布上把控件對應位置切割下來再畫圓,切割是為了提高性能;畫完圓後立即 postInvalidateDelayed 刷新更大半徑的圓達到擴散效果,且只刷新控件對應的區域,同樣是為了性能。
分幾個步驟:1. 獲取當前控件的寬高信息(用來初始化氣泡的半徑等);2. 獲取點擊事件(作為氣泡圓心,沒有點擊時是隨機坐標);3. 繪制氣泡。下面是重寫的 BreathTextView 代碼:
public class JasonBreathTextView extends TextView { private JasonBreathCircle breathCircle; public JasonBreathTextView(Context context) { super(context); } public JasonBreathTextView(Context context, AttributeSet attrs) { super(context, attrs); breathCircle = new JasonBreathCircle(context); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); breathCircle.initParameters(this); } @Override public boolean onTouchEvent(MotionEvent event) { breathCircle.setCircleCenter((int) event.getX(), (int) event.getY()); return super.onTouchEvent(event); } @Override protected void onDraw(Canvas canvas) { breathCircle.draw(canvas); super.onDraw(canvas); } public void startReveal() { breathCircle.start(); } public void stopReveal() { breathCircle.stop(); } } 為了使用方便,我把氣泡的實現和控件的實現分開了,這樣不管實現哪種 View 都可以直接使用分離出來的氣泡類,使用方法就像上面這樣,只需要把 view 控件本身傳給氣泡,氣泡就會在控件上繪制了。氣泡 BreathCircle 的代碼如下:
public class JasonBreathCircle { private static int DIFFUSE_GAP = 2; // 擴散半徑增量 private static final int INVALIDATE_DURATION = 10; // 每次刷新的時間間隔 private Context mContext; private boolean needToDrawReveal = false;// 繪畫標志位 private boolean isLargerMode = true;// 呼吸模式 private Paint mPaintReveal;// 畫筆 private int mCircleCenterX;// 圓心x private int mCircleCenterY;// 圓心y private int mCurRadius;// 當前半徑 private int mMaxRadius;// 最大半徑 private View mParentView;// 依附的控件 private int mParentHeight;// 控件高度 private int mParentWidth;// 控件寬度 public JasonBreathCircle(Context context) { mContext = context; initPaint(); } public void initParameters(View view) { this.mParentView = view; mParentHeight = mParentView.getHeight(); mParentWidth = mParentView.getWidth(); mMaxRadius = (int) Math.hypot(view.getHeight(), view.getWidth()) / 2; mCircleCenterX = mParentWidth / 2; mCircleCenterY = mParentHeight / 2; } public void draw(Canvas canvas) { if (needToDrawReveal) { canvas.save(); canvas.drawCircle(mCircleCenterX, mCircleCenterY, mCurRadius, mPaintReveal); canvas.restore(); if (isLargerMode && mCurRadius < mMaxRadius) { mCurRadius += DIFFUSE_GAP; postRevealInvalidate(); } else if (mCurRadius > 0 && !isLargerMode) { mCurRadius -= DIFFUSE_GAP; postRevealInvalidate(); } else { isLargerMode = !isLargerMode; setCircleCenter(JasonRandomUtil.nextInt(0, mParentWidth), JasonRandomUtil.nextInt(0, mParentHeight)); if (!isLargerMode) { mCurRadius = mMaxRadius; } postRevealInvalidate(); } } } public void start() { if (needToDrawReveal) { return; } needToDrawReveal = true; postRevealInvalidate(); } public void stop() { if (!needToDrawReveal) { return; } needToDrawReveal = false; reset(); postRevealInvalidate(); } public void setCircleCenter(int x, int y) { mCircleCenterX = x; mCircleCenterY = y; mMaxRadius = JasonRadiusUtil.getMaxRadius(mCircleCenterX, mCircleCenterY, mParentWidth, mParentHeight); } public void setHollow(boolean isHollow) { mPaintReveal.setStyle(isHollow ? Paint.Style.STROKE : Paint.Style.FILL); } private void reset() { mCurRadius = 0; isLargerMode = true; } private void initPaint() { mPaintReveal = new Paint(); mPaintReveal.setColor(mContext.getResources().getColor( R.color.jason_bg_common_green_light)); mPaintReveal.setAntiAlias(true); } private void postRevealInvalidate() { mParentView.postInvalidateDelayed(INVALIDATE_DURATION); } } 獲取控件的寬高信息是在 onSizeChanged 方法中;獲取點擊事件是在 onTouchEvent 裡面;最後繪制選擇了 onDraw 方法。
這裡記錄兩個地方:其一,關於繪制其實有好幾個方法可選,View 繪制流程為:先繪制背景,再繪制自己(onDraw),接著繪制子元素(dispatchDraw),最後繪制裝飾(onDrawScrollBars)。因為要把繪制出來的氣泡做背景,所以要在氣泡繪制完成才去繪制 view 自身的一些東西,因此在 onDraw 裡面最合適。其二,關於坐標,在 view 的重寫裡面,重繪時使用坐標只需要知道 view 的寬高就夠了,因為 onDraw 傳進來的 canvas 就是控件本身的大小,所以不需要像佈局裡面那樣對畫布進行裁剪,只要直接在上面畫就行了,坐標系直接按照寬高建立,左上角是原點。
Android 自定義 DateTimePicker:一次選好年月日與時間
距離上一篇 Android 主題的文章已經有一陣子了。關於自定義元件,手邊累積了好幾個題目想陸續分享,本篇要示範的是一個日期時間選擇器,讓使用者能在同一個彈窗裡同時挑選年
以 AccessibilityService 實作自動化:QQ 紅包插件的技術拆解
本文從技術角度記錄一個以 AccessibilityService 實作的自動化範例。這個 API 的正規用途是協助身心障礙使用者操作裝置——例如螢幕閱讀器會靠它讀出畫面
用 ViewPager 打造微信風格主頁面的完整實作
ViewPager 需要一個 PagerAdapter 作為資料容器,而 PagerAdapter 的資料來源則是一組 View。掌握這個關係,做出類似微信底部 Tab
Android 高仿小米時鐘:Camera 與 Matrix 實現 3D 效果
這個範例會製作一個接近小米風格的時鐘,除了自定義 View,也會用到 Camera 與 Matrix 呈現 3D 觸摸效果。 繪製順序 建議由外到內、由深到淺繪製。先處