Skip to content

Instantly share code, notes, and snippets.

@josecostamartins
Last active August 29, 2015 14:14
Show Gist options
  • Save josecostamartins/57cd01e6719a70365f5f to your computer and use it in GitHub Desktop.
Save josecostamartins/57cd01e6719a70365f5f to your computer and use it in GitHub Desktop.
BlurredImageViewNetwork from volley network library
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.Element;
import android.support.v8.renderscript.RenderScript;
import android.support.v8.renderscript.ScriptIntrinsicBlur;
import android.util.AttributeSet;
import com.android.volley.toolbox.NetworkImageView;
/**
* Created by josecostamartins on 24/01/15.
* This class automatically blurs an networkimageview
* This code was created by deriving the following resources:
* https://gist.github.com/Mariuxtheone/903c35b4927c0df18cf8
* https://gist.github.com/bkurzius/99c945bd1bdcf6af8f99
*/
public class BlurredNetworkImageView extends NetworkImageView {
Context mContext;
public BlurredNetworkImageView(Context context) {
super(context);
mContext = context;
}
public BlurredNetworkImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
mContext = context;
}
public BlurredNetworkImageView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
mContext = context;
}
@Override
public void setImageBitmap(Bitmap bm) {
if(bm==null) return;
setImageDrawable(new BitmapDrawable(mContext.getResources(),
getBlurBitmap(bm)));
}
public Bitmap getBlurBitmap(Bitmap bitmap){
//Let's create an empty bitmap with the same size of the bitmap we want to blur
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
//Instantiate a new Renderscript
RenderScript rs = RenderScript.create(mContext);
//Create an Intrinsic Blur Script using the Renderscript
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
//Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
//Set the radius of the blur
blurScript.setRadius(25.f);
//Perform the Renderscript
blurScript.setInput(allIn);
blurScript.forEach(allOut);
//Copy the final bitmap created by the out Allocation to the outBitmap
allOut.copyTo(outBitmap);
/**
* The original post asks to recycle the bitmap, to better handle memory management.
* This is not possible in this current situation, since volley uses a cached version of
* this image. If you do recycle, the mBuffer property of the bitmap will be null and the
* Allocation.createFromBitmap method will fail due to this.
* Practically, this means, that the second time the user tries to access the blurred image
* the system will crash
**/
//recycle the original bitmap
//bitmap.recycle();
//After finishing everything, we destroy the Renderscript.
rs.destroy();
return outBitmap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment