How to blur imageview in android

后端 未结 10 1950
南笙
南笙 2020-12-05 10:54

I have an imageview and i set Image Resources programmatically like this:

int resourceId = getResources().getIdentifier(\"imagename\", \"drawable\", \"mypack         


        
相关标签:
10条回答
  • 2020-12-05 11:29
    import android.renderscript.Allocation;
    import android.renderscript.Element;
    import android.renderscript.RenderScript;
    import android.renderscript.ScriptIntrinsicBlur;
    
    Bitmap blurred = blurRenderScript(this,yourBitmap, 25);
    //second parametre is radius
    yourImageView.setImageBitmap(blurred); 
    
    @SuppressLint("NewApi")
    public static Bitmap blurRenderScript(Context context,Bitmap smallBitmap, int radius) {
    try {
            smallBitmap = RGB565toARGB888(smallBitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        Bitmap bitmap = Bitmap.createBitmap(
                smallBitmap.getWidth(), smallBitmap.getHeight(),
                Bitmap.Config.ARGB_8888);
    
        RenderScript renderScript = RenderScript.create(context);
    
        Allocation blurInput = Allocation.createFromBitmap(renderScript, smallBitmap);
        Allocation blurOutput = Allocation.createFromBitmap(renderScript, bitmap);
    
        ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(renderScript,
                Element.U8_4(renderScript));
        blur.setInput(blurInput);
        blur.setRadius(radius); // radius must be 0 < r <= 25
        blur.forEach(blurOutput);
    
        blurOutput.copyTo(bitmap);
        renderScript.destroy();
    
        return bitmap;
    }
    
    private static Bitmap RGB565toARGB888(Bitmap img) throws Exception {
        int numPixels = img.getWidth() * img.getHeight();
        int[] pixels = new int[numPixels];
    
        //Get JPEG pixels.  Each int is the color values for one pixel.
        img.getPixels(pixels, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight());
    
        //Create a Bitmap of the appropriate format.
        Bitmap result = Bitmap.createBitmap(img.getWidth(), img.getHeight(), Bitmap.Config.ARGB_8888);
    
        //Set RGB pixels.
        result.setPixels(pixels, 0, result.getWidth(), 0, 0, result.getWidth(), result.getHeight());
        return result;
    }
    
    0 讨论(0)
  • 2020-12-05 11:31

    This is simple method

    set blur color with alpha

    public class BlurImageView extends ImageView {
        Paint rectPaint;
    
      private  int blurcolor=Color.parseColor("#aeffffff");
        public BlurImageView(Context context) {
            this(context, null);
    
        }
    
        public BlurImageView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
    
        }
    
        public BlurImageView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
    
            rectPaint=new Paint();
            rectPaint.setAntiAlias(true);
            rectPaint.setStyle(Paint.Style.FILL);
            rectPaint.setColor(blurcolor);
            invalidate();
        }
    
        public void setBlurcolor(int blurcolor) {
            this.blurcolor = blurcolor;
            invalidate();
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
    
            Log.i("BlurImageView","canvas");
    
            canvas.drawRect(getLeft(),0,getRight(),getHeight(),rectPaint);
        }
    }
    
    0 讨论(0)
  • 2020-12-05 11:35

    This project is clearly make an image blur. It works for me. https://github.com/Cutta/Simple-Image-Blur

    0 讨论(0)
  • 2020-12-05 11:37

    Add dependencies

    compile 'jp.wasabeef:fresco-processors:2.1.0'
    

    Use following code in layout file:

    <com.facebook.drawee.view.SimpleDraweeView
       android:id="@+id/imageView"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>
    

    Use following code in your java file:

    SimpleDraweeView imgView = (SimpleDraweeView) findViewById(R.id.imageView);
                ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                        .setPostprocessor(new IterativeBoxBlurPostProcessor(20))
                        .build();
                DraweeController controller = Fresco.newDraweeControllerBuilder()
                        .setImageRequest(request)
                        .setOldController(imgView.getController())
                        .build();
                imgView.setController(controller);
    
    0 讨论(0)
  • 2020-12-05 11:40

    There are different ways to make view blur in android, But i found the easiest and fastest way to make views blur using Fresco library.

    Add following dependency inside your build.gradle of your module.

       compile 'jp.wasabeef:fresco-processors:2.1.0'
    

    And inside onCreate() of Activity.

            Fresco.initialize(this);
        setContentView(R.layout.activity_main);
    
        SimpleDraweeView  simpleDraweeView = (SimpleDraweeView) findViewById(R.id.sdv_image);
    
        //INSTANTIATE BLUR POST PROCESSOR
        Postprocessor  postprocessor = new BlurPostprocessor(this, BLUR_PRECENTAGE);
    
        //INSTATNTING IMAGE REQUEST USING POST PROCESSOR AS PARAMETER
        ImageRequest  imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(IMAGE_URL))
                .setPostprocessor(postprocessor)
                .build();
    
        //INSTANTATE CONTROLLOR()
        PipelineDraweeController  controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                .setImageRequest(imageRequest)
                .setOldController(simpleDraweeView.getController())
                .build();
    
        //LOAD BLURRED IMAGE ON SimpleDraweeView(VIEW)
        simpleDraweeView.setController(controller);
    

    If you need complete implementation please visit this blog Fastest Image Blur in Android Using Fresco.

    0 讨论(0)
  • 2020-12-05 11:45
    private Bitmap CreateBlurredImage (int radius)
    {
       // Load a clean bitmap and work from that
        Bitmap originalBitmap=
        BitmapFactory.DecodeResource(Resources,Resource.Drawable.dog_and_monkeys);
    
    // Create another bitmap that will hold the results of the filter.
    Bitmap blurredBitmap;
    blurredBitmap = Bitmap.CreateBitmap (originalBitmap);
    
    // Create the Renderscript instance that will do the work.
    RenderScript rs = RenderScript.Create (this);
    
    // Allocate memory for Renderscript to work with
    Allocation input = Allocation.CreateFromBitmap (rs, originalBitmap, Allocation.MipmapControl.MipmapFull, AllocationUsage.Script);
    Allocation output = Allocation.CreateTyped (rs, input.Type);
    
    // Load up an instance of the specific script that we want to use.
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.Create (rs, Element.U8_4 (rs));
    script.SetInput (input);
    
    // Set the blur radius
    script.SetRadius (radius);
    
    // Start the ScriptIntrinisicBlur
    script.ForEach (output);
    
    // Copy the output to the blurred bitmap
    output.CopyTo (blurredBitmap);
    
    return blurredBitmap;
    

    }

    protected override void OnCreate (Bundle bundle)
    {
    base.OnCreate (bundle);
    
    SetContentView (Resource.Layout.Main);
    _imageView = FindViewById<ImageView> (Resource.Id.originalImageView);
    
    _seekbar = FindViewById<SeekBar> (Resource.Id.seekBar1);
    _seekbar.StopTrackingTouch += BlurImageHandler;
    

    }

    private void BlurImageHandler (object sender, SeekBar.StopTrackingTouchEventArgs e)
    {
    int radius = e.SeekBar.Progress;
    if (radius == 0) {
        // We don't want to blur, so just load the un-altered image.
        _imageView.SetImageResource (Resource.Drawable.dog_and_monkeys);
    } else {
        DisplayBlurredImage (radius);
    }
    

    }

    private void DisplayBlurredImage (int radius)
    {
    _seekbar.StopTrackingTouch -= BlurImageHandler;
    _seekbar.Enabled = false;
    
    ShowIndeterminateProgressDialog ();
    
    Task.Factory.StartNew (() => {
        Bitmap bmp = CreateBlurredImage (radius);
        return bmp;
    })
    .ContinueWith (task => {
        Bitmap bmp = task.Result;
        _imageView.SetImageBitmap (bmp);
        _seekbar.StopTrackingTouch += BlurImageHandler;
        _seekbar.Enabled = true;
        DismissIndeterminateProgressDialog ();
    }, TaskScheduler.FromCurrentSynchronizationContext ());
    

    }

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent">
        <SeekBar
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/seekBar1"
                android:max="25" />
        <ImageView
                android:src="@drawable/dog_and_monkeys"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/originalImageView" />
    </LinearLayout>
    

    click here deatiled code example

    0 讨论(0)
提交回复
热议问题