之前看到Android软件中用到和IOS系统类似的模糊效果,自己琢磨着也想做一个,于是在网上搜索了很多的相关资料,发现这篇博客《Android高级模糊技术》写得特别好,所以就开始好好地研究。
等到我把这个功能做到软件上,问题出现了,什么问题呢?
本来是准备用模糊图片来作为软件全屏界面的背景,可是布局显示的模糊图片在右下边缘一直出现黑色的边,不能铺满整个屏幕。一开始以为是模糊的参数需要调整,模糊后的图片变小了,但是把模糊后的图片的height和width打印出来,发现没有问题。
后来我想到,实际使用的图片比屏幕的尺寸小一点,而模糊处理的过程之前并没有对图片大小进行调整,导致输出的模糊图片虽然和视图(屏幕)大小一致,但是图片的模糊区域却和原图片相同大小,从而留下了空余的部分——黑色的边缘。于是又写了一个缩放图片的工具类,在模糊处理之前来同步图片和视图(屏幕)的大小,发现问题解决!
FastBlur.java
该文件是图片模糊的像素处理类,直接放入工程中
package com.kuk.tools; import android.graphics.Bitmap; public class FastBlur { public static Bitmap doBlur(Bitmap sentBitmap, int radius, boolean canReuseInBitmap) { // Stack Blur v1.0 from // http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html // // Java Author: Mario Klingemann <mario at quasimondo.com> // http://incubator.quasimondo.com // created Feburary 29, 2004 // Android port : Yahel Bouaziz <yahel at kayenko.com> // http://www.kayenko.com // ported april 5th, 2012 // This is a compromise between Gaussian Blur and Box blur // It creates much better looking blurs than Box Blur, but is // 7x faster than my Gaussian Blur implementation. // // I called it Stack Blur because this describes best how this // filter works internally: it creates a kind of moving stack // of colors whilst scanning through the image. Thereby it // just has to add one new block of color to the right side // of the stack and remove the leftmost color. The remaining // colors on the topmost layer of the stack are either added on // or reduced by one, depending on if they are on the right or // on the left side of the stack. // // If you are using this algorithm in your code please add // the following line: // // Stack Blur Algorithm by Mario Klingemann <mario@quasimondo.com> Bitmap bitmap; if (canReuseInBitmap) { bitmap = sentBitmap; } else { bitmap = sentBitmap.copy(sentBitmap.getConfig(), true); } if (radius < 1) { return (null); } int w = bitmap.getWidth(); int h = bitmap.getHeight(); int[] pix = new int[w * h]; bitmap.getPixels(pix, 0, w, 0, 0, w, h); int wm = w - 1; int hm = h - 1; int wh = w * h; int div = radius + radius + 1; int r[] = new int[wh]; int g[] = new int[wh]; int b[] = new int[wh]; int rsum, gsum, bsum, x, y, i, p, yp, yi, yw; int vmin[] = new int[Math.max(w, h)]; int divsum = (div + 1) >> 1; divsum *= divsum; int dv[] = new int[256 * divsum]; for (i = 0; i < 256 * divsum; i++) { dv[i] = (i / divsum); } yw = yi = 0; int[][] stack = new int[div][3]; int stackpointer; int stackstart; int[] sir; int rbs; int r1 = radius + 1; int routsum, goutsum, boutsum; int rinsum, ginsum, binsum; for (y = 0; y < h; y++) { rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; for (i = -radius; i <= radius; i++) { p = pix[yi + Math.min(wm, Math.max(i, 0))]; sir = stack[i + radius]; sir[0] = (p & 0xff0000) >> 16; sir[1] = (p & 0x00ff00) >> 8; sir[2] = (p & 0x0000ff); rbs = r1 - Math.abs(i); rsum += sir[0] * rbs; gsum += sir[1] * rbs; bsum += sir[2] * rbs; if (i > 0) { rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; } else { routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; } } stackpointer = radius; for (x = 0; x < w; x++) { r[yi] = dv[rsum]; g[yi] = dv[gsum]; b[yi] = dv[bsum]; rsum -= routsum; gsum -= goutsum; bsum -= boutsum; stackstart = stackpointer - radius + div; sir = stack[stackstart % div]; routsum -= sir[0]; goutsum -= sir[1]; boutsum -= sir[2]; if (y == 0) { vmin[x] = Math.min(x + radius + 1, wm); } p = pix[yw + vmin[x]]; sir[0] = (p & 0xff0000) >> 16; sir[1] = (p & 0x00ff00) >> 8; sir[2] = (p & 0x0000ff); rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; rsum += rinsum; gsum += ginsum; bsum += binsum; stackpointer = (stackpointer + 1) % div; sir = stack[(stackpointer) % div]; routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; rinsum -= sir[0]; ginsum -= sir[1]; binsum -= sir[2]; yi++; } yw += w; } for (x = 0; x < w; x++) { rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; yp = -radius * w; for (i = -radius; i <= radius; i++) { yi = Math.max(0, yp) + x; sir = stack[i + radius]; sir[0] = r[yi]; sir[1] = g[yi]; sir[2] = b[yi]; rbs = r1 - Math.abs(i); rsum += r[yi] * rbs; gsum += g[yi] * rbs; bsum += b[yi] * rbs; if (i > 0) { rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; } else { routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; } if (i < hm) { yp += w; } } yi = x; stackpointer = radius; for (y = 0; y < h; y++) { // Preserve alpha channel: ( 0xff000000 & pix[yi] ) pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum]; rsum -= routsum; gsum -= goutsum; bsum -= boutsum; stackstart = stackpointer - radius + div; sir = stack[stackstart % div]; routsum -= sir[0]; goutsum -= sir[1]; boutsum -= sir[2]; if (x == 0) { vmin[y] = Math.min(y + r1, hm) * w; } p = x + vmin[y]; sir[0] = r[p]; sir[1] = g[p]; sir[2] = b[p]; rinsum += sir[0]; ginsum += sir[1]; binsum += sir[2]; rsum += rinsum; gsum += ginsum; bsum += binsum; stackpointer = (stackpointer + 1) % div; sir = stack[stackpointer]; routsum += sir[0]; goutsum += sir[1]; boutsum += sir[2]; rinsum -= sir[0]; ginsum -= sir[1]; binsum -= sir[2]; yi += w; } } bitmap.setPixels(pix, 0, w, 0, 0, w, h); return (bitmap); } }
PictureZoom.java
此文件是缩放图片类
1 import android.content.Context; 2 import android.graphics.Bitmap; 3 import android.graphics.Matrix; 4 5 /** 6 * 此类的功能是用来缩放图片 7 * <p> 8 * 防止图片大小和屏幕大小不一致,造成模糊处理后会出现图片显示异常 9 * </p> 10 * @author Macneil.Gu 11 */ 12 public class PictureZoom { 13 14 private Context context; 15 16 public PictureZoom(Context context) { 17 this.context = context; 18 } 19 20 /** 21 * 缩放图片至指定的大小 22 * 23 * @param bitmap Bitmap格式的图片 24 * @param x 指定的宽 25 * @param y 指定的长 26 * @return 缩放后的指定大小图片 27 */ 28 public Bitmap Zoom(Bitmap bitmap, float x, float y) { 29 //图片的宽和高 30 int width = bitmap.getWidth(); 31 int height = bitmap.getHeight(); 32 33 //原图片和指定图片宽高比(缩放率),指定/原 34 float sx = x / width; 35 float sy = y / height; 36 37 //缩放图片动作 38 Matrix mtr = new Matrix(); 39 mtr.postScale(sx, sy); 40 41 //创建新的图片 42 Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, width, height, mtr, true); 43 return bm; 44 } 45 46 }
MainActivity.java
高斯模糊处理函数,这里对原来的函数修改了一点点
1 /** 2 * 高斯模糊处理 3 * <p> 4 * <code>将图片剪裁成1/8后进行模糊处理,可以大大减少模糊处理的时间,提高代码执行效率</code> 5 * </p> 6 * @param bitmap 需要模糊处理的图片 7 * @param view 显示图片的视图 8 */ 9 private void blur(Bitmap bitmap, View view) { 10 float scaleFactor = 8; 11 float radius = 2; 12 13 Bitmap overlay = Bitmap.createBitmap( 14 (int) (view.getMeasuredWidth() / scaleFactor), 15 (int) (view.getMeasuredHeight() / scaleFactor), 16 Bitmap.Config.ARGB_8888); 17 18 Canvas canvas = new Canvas(overlay); 19 canvas.translate(-view.getLeft() / scaleFactor, -view.getTop() 20 / scaleFactor); 21 canvas.scale(1 / scaleFactor, 1 / scaleFactor); 22 Paint paint = new Paint(); 23 paint.setFlags(Paint.FILTER_BITMAP_FLAG); // 双缓冲机制 24 canvas.drawBitmap(bitmap, 0, 0, paint); 25 26 overlay = FastBlur.doBlur(overlay, (int) radius, true); 27 view.setBackgroundDrawable(new BitmapDrawable(getResources(), overlay)); 28 }
>>原博文的注解:
● scaleFactor提供了需要缩小的等级,在代码中我把bitmap的尺寸缩小到原图的1/8。因为这个bitmap在模糊处理时会先被缩小然后再放大,所以在我的模糊算法中就不用radius这个参数了,所以把它设成2。
● 接着需要创建bitmap,这个bitmap比最后需要的小八倍。
● 请注意我给Paint提供了FILTER_BITMAP_FLAG标示,这样的话在处理bitmap缩放的时候,就可以达到双缓冲的效果,模糊处理的过程就更加顺畅了。
● 接下来和之前一样进行模糊处理操作,这次的图片小了很多,幅度也降低了很多,所以模糊过程非常快。
● 把模糊处理后的图片作为背景,它会自动进行放大操作的。
调用上述的模糊处理函数,对指定图片模糊处理,并显示到布局的ImageView上。
1 // 获取窗口服务 2 WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 3 4 // 缩放图片 5 PictureZoom pz = new PictureZoom(this); 6 // 把图片缩放到窗口的长宽 7 Bitmap mybm = pz.Zoom(bm, wm.getDefaultDisplay().getWidth(), wm.getDefaultDisplay().getHeight()); 8 9 // 模糊处理,blurImage是ImageView控件,这里是作为背景显示的 10 blur(mybm, blurImage);
>>将上述的三个文件放在同一个包下使用,否则需要导入文件使用。如果大家喜欢剖根究底,可以仔细阅读原博文。
原博文出处:Android高级模糊技术