Android 馬賽克(Mosaics)效果
前幾天看見開源項目效果好贊,看了下代碼,實現(xiàn)大致就是在原界面之上覆蓋一成自定義的View,獲取到點擊的那個View的內(nèi)容(Bitmap),然后在覆蓋的那個自定義View的特定位置畫出來,之后就是對這個Bitmap做一些列拆分,變化重繪的過程。在這里根據(jù)他對bitmap的拆分,感覺用來實現(xiàn)Bitmap的效果也是不錯的,就試著做一做。
在這里介紹使用兩種方式實現(xiàn)馬賽克效果.開始之前先看看效果

感覺還不錯吧!
1、直接繪制
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;
}
2、修改像素點
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);
}
從效率上來看,第二中方式效率會高10倍,只要是因為第一種方式繪制的次數(shù)太多了,而繪制是比較費時間的。這里特別提示,不要在大量的循環(huán)語句內(nèi)部使用Log.v(...),這是一個很耗時間的操作。
是不是很有趣,大家可以親自動手試驗一下。
相關(guān)文章
基于Android應(yīng)用中如何反饋Crash報告的詳解
本篇文章是對在Android應(yīng)用中如何反饋Crash報告的詳細(xì)分析介紹。需要的朋友參考下2013-05-05
Android SurfaceView基礎(chǔ)用法詳解
這篇文章主要介紹了Android SurfaceView基礎(chǔ)用法詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android ListView物流獲取追蹤功能實現(xiàn)
這篇文章主要介紹了Android ListView物流獲取追蹤功能實現(xiàn)的相關(guān)資料,需要的朋友可以參考下2016-03-03
Android ListView長按彈出菜單二種實現(xiàn)方式示例
這篇文章主要介紹了Android ListView長按彈出菜單的方法,大家參考實現(xiàn)2013-11-11
Android開發(fā)實現(xiàn)的圖片點擊切換功能示例
這篇文章主要介紹了Android開發(fā)實現(xiàn)的圖片點擊切換功能,涉及Android ImageView組件創(chuàng)建、布局及實現(xiàn)圖形切換相關(guān)操作技巧,需要的朋友可以參考下2019-04-04

