之前看到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 <[email protected]> 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
此文件是缩放图片类
import android.content.Context; import android.graphics.Bitmap; import android.graphics.Matrix; /** * 此类的功能是用来缩放图片 * <p> * 防止图片大小和屏幕大小不一致,造成模糊处理后会出现图片显示异常 * </p> * @author Macneil.Gu */ public class PictureZoom { private Context context; public PictureZoom(Context context) { this.context = context; } /** * 缩放图片至指定的大小 * * @param bitmap Bitmap格式的图片 * @param x 指定的宽 * @param y 指定的长 * @return 缩放后的指定大小图片 */ public Bitmap Zoom(Bitmap bitmap, float x, float y) { //图片的宽和高 int width = bitmap.getWidth(); int height = bitmap.getHeight(); //原图片和指定图片宽高比(缩放率),指定/原 float sx = x / width; float sy = y / height; //缩放图片动作 Matrix mtr = new Matrix(); mtr.postScale(sx, sy); //创建新的图片 Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, width, height, mtr, true); return bm; } }
MainActivity.java
高斯模糊处理函数,这里对原来的函数修改了一点点
/** * 高斯模糊处理 * <p> * <code>将图片剪裁成1/8后进行模糊处理,可以大大减少模糊处理的时间,提高代码执行效率</code> * </p> * @param bitmap 需要模糊处理的图片 * @param view 显示图片的视图 */ private void blur(Bitmap bitmap, View view) { float scaleFactor = 8; float radius = 2; Bitmap overlay = Bitmap.createBitmap( (int) (view.getMeasuredWidth() / scaleFactor), (int) (view.getMeasuredHeight() / scaleFactor), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(overlay); canvas.translate(-view.getLeft() / scaleFactor, -view.getTop() / scaleFactor); canvas.scale(1 / scaleFactor, 1 / scaleFactor); Paint paint = new Paint(); paint.setFlags(Paint.FILTER_BITMAP_FLAG); // 双缓冲机制 canvas.drawBitmap(bitmap, 0, 0, paint); overlay = FastBlur.doBlur(overlay, (int) radius, true); view.setBackgroundDrawable(new BitmapDrawable(getResources(), overlay)); }
>>原博文的注解:
● scaleFactor提供了需要缩小的等级,在代码中我把bitmap的尺寸缩小到原图的1/8。因为这个bitmap在模糊处理时会先被缩小然后再放大,所以在我的模糊算法中就不用radius这个参数了,所以把它设成2。
● 接着需要创建bitmap,这个bitmap比最后需要的小八倍。
● 请注意我给Paint提供了FILTER_BITMAP_FLAG标示,这样的话在处理bitmap缩放的时候,就可以达到双缓冲的效果,模糊处理的过程就更加顺畅了。
● 接下来和之前一样进行模糊处理操作,这次的图片小了很多,幅度也降低了很多,所以模糊过程非常快。
● 把模糊处理后的图片作为背景,它会自动进行放大操作的。
调用上述的模糊处理函数,对指定图片模糊处理,并显示到布局的ImageView上。
// 获取窗口服务 WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); // 缩放图片 PictureZoom pz = new PictureZoom(this); // 把图片缩放到窗口的长宽 Bitmap mybm = pz.Zoom(bm, wm.getDefaultDisplay().getWidth(), wm.getDefaultDisplay().getHeight()); // 模糊处理,blurImage是ImageView控件,这里是作为背景显示的 blur(mybm, blurImage);
>>将上述的三个文件放在同一个包下使用,否则需要导入文件使用。如果大家喜欢剖根究底,可以仔细阅读原博文。