本文介绍了在ImageView的背景模糊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ImageView的模糊背景。

我有这个,但一切都模糊了,无论是图像和背景:

  ...
    imageView.setImageBitmap(bitmapWithoutBlur);    位图bitmapBlur =模糊(getApplicationContext(),bitmapWithoutBlur);    BitmapDrawable bitmapDrawable =新BitmapDrawable(getResources(),bitmapBlur);    imageView.setBackgroundDrawable(bitmapDrawable);    ...    公共静态位图模糊(上下文的背景下,位图位图){
            INT宽度= Math.round(bitmap.getWidth());
            INT高度= Math.round(bitmap.getHeight());            位图inputBitmap = Bitmap.createScaledBitmap(位图,宽度,高度,假);
            位图outputBitmap = Bitmap.createBitmap(inputBitmap);            RenderScript RS = RenderScript.create(背景);
            ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(RS,Element.U8_4(RS));
            分配tmpIn = Allocation.createFromBitmap(RS,inputBitmap);
            分配tmpOut = Allocation.createFromBitmap(RS,outputBitmap);
            theIntrinsic.setRadius(25F);
            theIntrinsic.setInput(tmpIn);
            theIntrinsic.forEach(tmpOut);
            tmpOut.copyTo(outputBitmap);            返回outputBitmap;
    }


解决方案

从RenderScript使用库来ScriptIntrinsicBlur模糊quickly.Follow这如何访问RenderScript API的.以下是我做了模糊意见和位图类:

 公共类BlurBuilder {
    私有静态最终浮动BITMAP_SCALE = 0.4f;
    私有静态最终浮动BLUR_RADIUS = 7.5f;    公共静态位图模糊(视图v){
        返回模糊(v.getContext(),getScreenshot(ⅴ));
    }    公共静态位图模糊(上下文CTX,位图图像){
        INT宽度= Math.round(image.getWidth()* BITMAP_SCALE);
        INT高度= Math.round(image.getHeight()* BITMAP_SCALE);        位图inputBitmap = Bitmap.createScaledBitmap(图像,宽度,高度,假);
        位图outputBitmap = Bitmap.createBitmap(inputBitmap);        RenderScript RS = RenderScript.create(CTX);
        ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(RS,Element.U8_4(RS));
        分配tmpIn = Allocation.createFromBitmap(RS,inputBitmap);
        分配tmpOut = Allocation.createFromBitmap(RS,outputBitmap);
        theIntrinsic.setRadius(BLUR_RADIUS);
        theIntrinsic.setInput(tmpIn);
        theIntrinsic.forEach(tmpOut);
        tmpOut.copyTo(outputBitmap);        返回outputBitmap;
    }    私有静态位图getScreenshot(视图v){
        位图B = Bitmap.createBitmap(v.getWidth(),v.getHeight(),Bitmap.Config.ARGB_8888);
        帆布C =新的Canvas(B);
        v.draw(C);
        返回b;
    }
}

I would like to have blurred background in 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;
    }
解决方案

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. 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;
    }
}

这篇关于在ImageView的背景模糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:37