Skip to content

Instantly share code, notes, and snippets.

@erikjhordan-rey
Last active August 29, 2015 14:23
Show Gist options
  • Save erikjhordan-rey/894505cac564c4965bb5 to your computer and use it in GitHub Desktop.
Save erikjhordan-rey/894505cac564c4965bb5 to your computer and use it in GitHub Desktop.
EffectBlur Picasso
import android.content.Context;
import android.graphics.Bitmap;
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 com.squareup.picasso.Transformation;
/*This code was not written by me but is really good,I found the way to do Compatible with sdkVersion 15!
You can use this form
1- add dependece compile 'com.squareup.picasso:picasso:2.5.2'
2- defaultConfig {
applicationId ...
minSdkVersion 15 // This code can be used with API 15
targetSdkVersion 22
renderscriptTargetApi 19
renderscriptSupportModeEnabled true
versionCode ...
versionName ...
}
3- Picasso.with(context).load("url").transform(new EffectBlur(context, radiusBlur)).into(ImageView);
*/
public class EffectBlur implements Transformation {
protected static final int UP_LIMIT = 25;
protected static final int LOW_LIMIT = 1;
protected final Context context;
protected final int blurRadius;
public EffectBlur(Context context, int radius) {
this.context = context;
if(radius<LOW_LIMIT){
this.blurRadius = LOW_LIMIT;
}else if(radius>UP_LIMIT){
this.blurRadius = UP_LIMIT;
}else
this.blurRadius = radius;
}
@Override
public Bitmap transform(Bitmap source) {
Bitmap sourceBitmap = source;
Bitmap blurredBitmap;
blurredBitmap = Bitmap.createBitmap(sourceBitmap);
RenderScript renderScript = RenderScript.create(context);
Allocation input = Allocation.createFromBitmap(renderScript,
sourceBitmap,
Allocation.MipmapControl.MIPMAP_FULL,
Allocation.USAGE_SCRIPT);
Allocation output = Allocation.createTyped(renderScript, input.getType());
ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(renderScript,
Element.U8_4(renderScript));
script.setInput(input);
script.setRadius(blurRadius);
script.forEach(output);
output.copyTo(blurredBitmap);
return blurredBitmap;
}
@Override
public String key() {
return "blurred";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment