由於項目中用得比較多的兩個功能 截圖 和 模糊,我就整理成一個項目來概述。
源工程:http://download.csdn.net/download/li_xiao_kang/8240567
一 、截圖
核心代碼
View.setDrawingCacheEnabled(true);
View.buildDrawingCache();
Bitmap = View.getDrawingCache().copy(Config.ARGB_8888, true);
可以看出,所有的View都可以得到位圖。View.getDrawingCache()此方法得到的是該View的副本,如要使用該副本,得創建該位圖 Bitmap.copy(Config config, boolean isMutable)。不然會報Bitmap is recycled 異常。
二、模糊
關於模糊,使用的是FastBlur算法。FastBlur類可以去源代碼中獲取。你懂的。
核心代碼
Bitmap blurBmp = Bitmap.createBitmap(layoutBmp.getWidth(), layoutBmp.getHeight()
, Config.ARGB_8888);
Canvas canvas = new Canvas(blurBmp);
Paint paint = new Paint();
paint.setFlags(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(layoutBmp, 0, 0, paint);
//有更加效率的方法,就是將圖片縮小進行模糊,但是爲了直觀模糊省去縮小代碼。直接進行模糊。
//radius 理論上值越大越模糊,但耗時越多。
int radius = 20;
blurBmp = FastBlur.doBlur(blurBmp, radius, true); //模糊
思路:創建與layoutBmp(截圖)同樣大小的blurBmp,繪製該blurBmp。通過設置radius大小來控制模糊程度。調用FastBlur.doBlur(Bitmap sentBitmap, int radius, boolean canReuseInBitmap)來進行模糊。其返回值就是模糊後的位圖了。