How to blur imageview in android
I have an imageview and i set Image Resources programmatically like this:
int resourceId = getResources().getIdentifier("imagename", "drawable", "mypackage");
imgLock.setImageResource(resourceId);
Is there any easy way to show my ImageView with blurry image?
Solution 1:
You can use glide transformations https://github.com/wasabeef/glide-transformations you can blur the image with one line of code
Glide.with(this).load(R.drawable.demo)
.bitmapTransform(new BlurTransformation(context))
.into((ImageView) findViewById(R.id.image));
Solution 2:
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;
}
Solution 3:
There are many libraries are available you can use any of these. I prefer Blurry library for it. it is very simple and optimized.
dependency:
dependencies {
compile 'jp.wasabeef:blurry:4.x.x'
}
Functions
Blurry.with(context).radius(25).sampling(2).onto(rootView)
-
// from View
Blurry.with(context).capture(view).into(imageView)
-
// from Bitmap
Blurry.with(context).from(bitmap).into(imageView)
Blur Options
- Radius
- Down Sampling
- Color Filter
- Asynchronous Support
- Animation (Overlay Only)
Blurry.with(context)
.radius(10)
.sampling(8)
.color(Color.argb(66, 255, 255, 0))
.async()
.animate(500)
.onto(rootView);
Get a bitmap directly
// Sync
val bitmap = Blurry.with(this)
.radius(10)
.sampling(8)
.capture(findViewById(R.id.right_bottom)).get()
imageView.setImageDrawable(BitmapDrawable(resources, bitmap))
// Async
Blurry.with(this)
.radius(25)
.sampling(4)
.color(Color.argb(66, 255, 255, 0))
.capture(findViewById(R.id.left_bottom))
.getAsync {
imageView.setImageDrawable(BitmapDrawable(resources, it))
}
Solution 4:
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
Solution 5:
Just simply use this library https://github.com/ChathuraHettiarachchi/BlurIM , I was having problem with BlurTransformation class had errors thats why couldn't use Glide transformation but this works fine.
BlurImage.withContext(this)
.blurFromResource(R.drawable.YOUR_RESOURCE)
.into(imageView);