Blur Image in Android After imageView Click

Ecodor

Senior Member
Joined
Nov 5, 2017
Messages
896
Reaction score
232
I cannot manage to make the image blurred after i click the image, is there someone who can help me do this. I am new in android about 4 - 5 days... Here is the code

Code:
imageView4.setOnClickListener {
   if (!haveImage) {
      Toast.makeText(this, "Image not imported yet!", Toast.LENGTH_LONG).show()
   } else {
      val imgUri = Uri.parse("android.resource://com.example.myapplication/" + R.drawable.ic_launcher)
      imageView4.setImageURI(imgUri)
   }
}
 
This piece of code just shows a toast message if variable haveImage is false, and set image in the imageview if haveImage is true. To blur an imageView, consider using Glide library with this code :
Code:
Glide.with(this).load(R.drawable.ic_launcher)
    .bitmapTransform(new BlurTransformation(this))
    .into(imageView4);

Don't forget to import the Glide library in your gradle file like this :
Code:
implementation 'com.github.bumptech.glide:glide:4.10.0'
 
Yes Glide solves my problem but i have another one over here, with passing image data...
BTW this needs to be imported into build.gradle
Code:
implementation 'com.github.bumptech.glide:glide:4.9.0'
    implementation 'jp.wasabeef:glide-transformations:4.1.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'

So i have function which user can import custom image in that imageView
Code:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if(resultCode == Activity.RESULT_OK && requestCode == IMAGE_PICK_CODE) {
            imageView4.setImageURI(data?.data)
            haveImage = true
        }
    }

and i want to use Glide to blur the same image and not the ic_launcher as the example above... i am having trouble passing the image to Glide .into() after imageView is pressed as the example above
could you please help?
 
What do you want exactly to do? Do you want the imageView to be blurred when the user click on it? Or when you receive the image data on the
onActivityResult method?
 
Yes i want the imageView to be blurred on the current image imported from the user when is clicked the imageView
 
anyone have an solution ?
 
Yes u can use the below article for ur requirement. http://www.tellmehow.co/dynamically-blur-imageview-android/
 
Use this.
Code:
Glide.with(context).load(imagePath).transform(new BlurTransformation(context))
                                        .into(imageView)

//  Use the below class for ur requirement

 Glide.with(context).load(imagePath).transform(new BlurTransformation(context))
                                        .into(imageView);
I am using this Class for Blur Transformation

public class BlurTransformation extends BitmapTransformation {

private RenderScript rs;

public BlurTransformation(Context context) {
    super( context );

    rs = RenderScript.create( context );
}

@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    Bitmap blurredBitmap = toTransform.copy( Bitmap.Config.ARGB_8888, true );

    // Allocate memory for Renderscript to work with
    Allocation input = Allocation.createFromBitmap(
        rs,
        blurredBitmap,
        Allocation.MipmapControl.MIPMAP_FULL,
        Allocation.USAGE_SHARED
    );
    Allocation output = Allocation.createTyped(rs, input.getType());

    // 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(10);

    // Start the ScriptIntrinisicBlur
    script.forEach(output);

    // Copy the output to the blurred bitmap
    output.copyTo(blurredBitmap);

    toTransform.recycle();

    return blurredBitmap;
}

@Override
public String getId() {
    return "blur";
}
}
 
Back
Top