简体   繁体   English

ImageView中的背景模糊

[英]Blurred background in ImageView

I would like to have blurred background in ImageView. 我想在ImageView中模糊背景。

I have this, but everything is blurred, both image and the background: 我有这个,但是图像和背景都模糊了:

    ...
    imageView.setImageBitmap(bitmapWithoutBlur);

    Bitmap bitmapBlur = blur(getApplicationContext(), bitmapWithoutBlur);

    BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmapBlur);

    imageView.setBackgroundDrawable(bitmapDrawable);

    ...

    public static Bitmap blur(Context context, Bitmap bitmap) {
            int width = Math.round(bitmap.getWidth());
            int height = Math.round(bitmap.getHeight());

            Bitmap inputBitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
            Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

            RenderScript rs = RenderScript.create(context);
            ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
            Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
            Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
            theIntrinsic.setRadius(25f);
            theIntrinsic.setInput(tmpIn);
            theIntrinsic.forEach(tmpOut);
            tmpOut.copyTo(outputBitmap);

            return outputBitmap;
    }
    //In here you have a bitmap without blur
    //delete this setting bitmap
    //imageView.setImageBitmap(bitmapWithoutBlur);


    // Edited lines from here
    Bitmap bitmapToBlur = bitmapWithoutBlur;
    Bitmap bitmapBlur = blur(getApplicationContext(), bitmapToBlur);
    Canvas canvas = new Canvas(BitmapBlur); 
    canvas.drawBitmap(bitmapToBlur,0f,0f,null);



    imageView.setImageBitmap(bitmapToBlur);

In your code look at the commend lines that i wrote.In the second line you make both of the images blurred.Just send bitmapwithoutblur to method and before sending it,use a temp value to withoutblurred version. 在您的代码中查看我编写的推荐行。在第二行中,使两个图像均变得模糊。只需将bitmapwithoutblur发送给method,然后再将其发送,请将temp值使用为notblurred版本。

Use ScriptIntrinsicBlur from the RenderScript library to blur quickly.Follow this to how to access the RenderScript API http://developer.android.com/guide/topics/renderscript/compute.html#access-rs-apis . 使用RenderScript库中的ScriptIntrinsicBlur可以快速使图像模糊。接下来是如何访问RenderScript API http://developer.android.com/guide/topics/renderscript/compute.html#access-rs-apis The following is a class I made to blur Views and Bitmaps: 以下是我制作的用于模糊视图和位图的类:

public class BlurBuilder {
    private static final float BITMAP_SCALE = 0.4f;
    private static final float BLUR_RADIUS = 7.5f;

    public static Bitmap blur(View v) {
        return blur(v.getContext(), getScreenshot(v));
    }

    public static Bitmap blur(Context ctx, Bitmap image) {
        int width = Math.round(image.getWidth() * BITMAP_SCALE);
        int height = Math.round(image.getHeight() * BITMAP_SCALE);

        Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
        Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);

        RenderScript rs = RenderScript.create(ctx);
        ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
        Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
        theIntrinsic.setRadius(BLUR_RADIUS);
        theIntrinsic.setInput(tmpIn);
        theIntrinsic.forEach(tmpOut);
        tmpOut.copyTo(outputBitmap);

        return outputBitmap;
    }

    private static Bitmap getScreenshot(View v) {
        Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        v.draw(c);
        return b;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM