Android 图片高级绘图效果---高斯模糊
高斯模糊就是将指定像素变换为其与周边像素加权平均后的值,权重就是高斯分布函数计算出来的值。高斯模糊能够将图片制作成类似磨砂的图片效果,一般这些图片都用来作为背景。
目前使用到的是RenderScript ,其特点是使用起来比较方便,虽然效率不是很高,但是也能够满足目前的使用。使用流程如下:
1. 添加混淆配置
-keepclasseswithmembernames class * { native <methods>; } -keep class android.support.v8.renderscript.** { *; }
2.使用高斯模糊
private void blur(Bitmap bkg, View view) { float radius = 25; Bitmap overlay = Bitmap.createBitmap((int) (view.getMeasuredWidth()), (int) (view.getMeasuredHeight()), Bitmap.Config.ARGB_8888); float scaleW = (float) view.getMeasuredWidth() / (float) bkg.getWidth(); float scaleH = (float) view.getMeasuredHeight() / (float) bkg.getHeight(); Matrix matrix = new Matrix(); matrix.postScale(scaleW, scaleH); Bitmap resizeBmp = Bitmap.createBitmap(bkg, 0, 0, bkg.getWidth(), bkg.getHeight(), matrix, true); Canvas canvas = new Canvas(overlay); canvas.translate(-view.getLeft(), -view.getTop()); canvas.drawBitmap(resizeBmp, 0, 0, null); RenderScript rs = RenderScript.create(this); Allocation overlayAlloc = Allocation.createFromBitmap(rs, overlay); ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement()); blur.setInput(overlayAlloc); blur.setRadius(radius); blur.forEach(overlayAlloc); overlayAlloc.copyTo(overlay); view.setBackground(new BitmapDrawable(getResources(), overlay)); rs.destroy(); }