1. 程式人生 > >Android實現高斯模糊(也叫毛玻璃效果)

Android實現高斯模糊(也叫毛玻璃效果)

/**
* 高斯模糊
* @param bkg
* @param view
*/
private void blur(Bitmap bkg, View view) {
float radius = 25;
Bitmap overlay = Bitmap.createBitmap((int) (view.getMeasuredWidth()),
                (int) (view.getMeasuredHeight()), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(overlay);
canvas.translate(-view.getLeft(), -view.getTop());
       canvas.drawBitmap(bkg, 0, 0, null);
        RenderScript rs = RenderScript.create(mContext);
        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.setBackgroundDrawable(new BitmapDrawable(
        mContext.getResources(), overlay));
        rs.destroy();
}