編輯:關於 Android 程式設計
前陣子看到一個開源專案效果很讚,翻了一下原始碼,思路大致是:在原界面上覆蓋一層自訂 View,取得被點擊那個 View 的內容(Bitmap),在覆蓋層的特定位置畫出來,接著對這張 Bitmap 做一系列拆分與重繪。我覺得它處理 Bitmap 的方式本身就很有參考價值,於是拿來試著做馬賽克。
本文介紹兩種實作方式。先看成果:

概念是把影像切成網格,每格取一個代表色(此處取格子的中心點),再用 Paint 把該格填滿。
public static Bitmap getMosaicsBitmap(Bitmap bmp, double precent) { long start = System.currentTimeMillis(); int bmpW = bmp.getWidth(); int bmpH = bmp.getHeight(); Bitmap resultBmp = Bitmap.createBitmap(bmpW, bmpH, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(resultBmp); Paint paint = new Paint(); double unit; if (precent == 0) { unit = bmpW; } else { unit = 1 / precent; } double resultBmpW = bmpW / unit; double resultBmpH = bmpH / unit; for (int i = 0; i < resultBmpH; i++) { for (int j = 0; j < resultBmpW; j++) { int pickPointX = (int) (unit * (j + 0.5)); int pickPointY = (int) (unit * (i + 0.5)); int color; if (pickPointX >= bmpW || pickPointY >= bmpH) { color = bmp.getPixel(bmpW / 2, bmpH / 2); } else { color = bmp.getPixel(pickPointX, pickPointY); } paint.setColor(color); canvas.drawRect((int) (unit * j), (int) (unit * i), (int) (unit * (j + 1)), (int) (unit * (i + 1)), paint); } } canvas.setBitmap(null); long end = System.currentTimeMillis(); Log.v(TAG, "DrawTime:" + (end - start)); return resultBmp; }
先把整張圖的像素倒進一個 int 陣列,再以左上角像素為代表色,把每一格內的像素全部覆蓋成該值。
public static Bitmap getMosaicsBitmaps(Bitmap bmp, double precent) { long start = System.currentTimeMillis(); int bmpW = bmp.getWidth(); int bmpH = bmp.getHeight(); int[] pixels = new int[bmpH * bmpW]; bmp.getPixels(pixels, 0, bmpW, 0, 0, bmpW, bmpH); int raw = (int) (bmpW * precent); int unit; if (raw == 0) { unit = bmpW; } else { unit = bmpW / raw; //原來的unit*unit像素點合成一個,使用原左上角的值 } if (unit >= bmpW || unit >= bmpH) { return getMosaicsBitmap(bmp, precent); } for (int i = 0; i < bmpH; ) { for (int j = 0; j < bmpW; ) { int leftTopPoint = i * bmpW + j; for (int k = 0; k < unit; k++) { for (int m = 0; m < unit; m++) { int point = (i + k) * bmpW + (j + m); if (point < pixels.length) { pixels[point] = pixels[leftTopPoint]; } } } j += unit; } i += unit; } long end = System.currentTimeMillis(); Log.v(TAG, "DrawTime:" + (end - start)); return Bitmap.createBitmap(pixels, bmpW, bmpH, Bitmap.Config.ARGB_8888); }
關鍵在於繪製次數。方式一每格都要呼叫一次 canvas.drawRect(),而 Canvas 繪製本身就是重量級操作,格子一多就吃不消;方式二全程只在記憶體陣列中做整數赋值,最後才建立一張 Bitmap,成本自然低得多。
另外提醒一個容易被忽略的效能陷阱:不要在大量迴圈內部呼叫 Log.v(...)。Log 本身會做字串拼接與 I/O,放在雙層迴圈裡足以讓效能直接腰斬。除錯時建議用累加變數在迴圈結束後再一次輸出。
想再進一步優化的話,可以把格子尺寸做等比抽樣(先縮圖再放大),或改用 RenderScript/RenderEffect 的 blur 這類硬體加速路徑,都能比純 Java 迴圈快上更多。不妨動手實測兩種寫法的耗時差異,會很有感。
Android TextView 兩端對齊:原理拆解與自定義實作
Android 的 TextView 預設只提供靠左對齊,做不到排版雜誌那種左右都齊平的效果。原因之一在於系統的行內斷行規則:數字與英文字母屬於半角字元,預設不允許出現在
Android 四大元件之 Service 詳解:啟動、綁定與生命週期
Service 是 Android 四大元件之一,也是最容易觀念混淆的一個。這篇把它的定位、兩種啟動方式、生命週期回呼與通訊機制完整梳理一次。 一、Service 是什
Android 資料傳遞筆記(三):用介面回呼把 Activity 的資料送進 Fragment
Activity 與 Fragment 之間要交換資料,做法其實不少,但最常見、也最能體現解耦思想的一種,就是「介面回呼」(Interface Callback)。本文用
Android 自定義時鐘控件:onMeasure、onDraw 與定時刷新
自定義 View 是 Android 的必備技能,而時鐘是個特別合適的練習題——它同時涵蓋了自定義屬性、View 測量、Canvas 繪製、線程定時刷新幾個知識點。本文記