1
0
mirror of https://github.com/wasabeef/glide-transformations.git synced 2025-06-08 07:24:05 +08:00

fix: Blur transformation not working

This commit is contained in:
wasabeef 2015-01-20 23:34:47 +09:00
parent bf2eaadf2d
commit 3c3d2c0123
3 changed files with 39 additions and 17 deletions

View File

@ -52,6 +52,21 @@ Glide.with(this).load(R.drawable.demo).bitmapTransform(
.into((ImageView) findViewById(R.id.image)); .into((ImageView) findViewById(R.id.image));
``` ```
## Step 4
If you are using BlurTransformation.
```groovy
android {
...
defaultConfig {
...
renderscriptTargetApi 21
renderscriptSupportModeEnabled true
}
}
```
## Transformations ## Transformations
### Crop ### Crop

View File

@ -9,6 +9,8 @@ android {
targetSdkVersion TARGET_SDK_VERSION as int targetSdkVersion TARGET_SDK_VERSION as int
versionCode VERSION_CODE as int versionCode VERSION_CODE as int
versionName VERSION_NAME versionName VERSION_NAME
renderscriptTargetApi RENDERSCRIPT_TARGET_API as int
renderscriptSupportModeEnabled true
} }
signingConfigs { signingConfigs {

View File

@ -23,11 +23,11 @@ import com.bumptech.glide.load.resource.bitmap.BitmapResource;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Build; import android.os.Build;
import android.renderscript.Allocation; import android.support.v8.renderscript.Allocation;
import android.renderscript.Element; import android.support.v8.renderscript.RenderScript;
import android.renderscript.RenderScript; import android.support.v8.renderscript.ScriptIntrinsicBlur;
import android.renderscript.ScriptIntrinsicBlur;
public class BlurTransformation implements Transformation<Bitmap> { public class BlurTransformation implements Transformation<Bitmap> {
@ -45,7 +45,7 @@ public class BlurTransformation implements Transformation<Bitmap> {
public BlurTransformation(Context context, BitmapPool pool, int radius) { public BlurTransformation(Context context, BitmapPool pool, int radius) {
mContext = context; mContext = context;
mBitmapPool = pool; mBitmapPool = pool;
this.mRadius = radius; mRadius = radius;
} }
@Override @Override
@ -53,21 +53,26 @@ public class BlurTransformation implements Transformation<Bitmap> {
Bitmap source = resource.get(); Bitmap source = resource.get();
if (Build.VERSION.SDK_INT > 16) { if (Build.VERSION.SDK_INT > 16) {
Bitmap bitmap = source.copy(source.getConfig(), true);
final RenderScript rs = RenderScript.create(mContext); //recreate new bitmap based on source bitmap
final Allocation input = Allocation.createFromBitmap(rs, source, Bitmap outBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(),
Allocation.MipmapControl.MIPMAP_NONE, Bitmap.Config.ARGB_8888);
Allocation.USAGE_SCRIPT); Canvas canvas = new Canvas(outBitmap);
final Allocation output = Allocation.createTyped(rs, input.getType()); canvas.drawBitmap(source, 0, 0, null);
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setRadius(mRadius /* e.g. 3.f */); //apply blur effect on image
script.setInput(input); RenderScript rs = RenderScript.create(mContext);
script.forEach(output); Allocation overlayAlloc = Allocation.createFromBitmap(rs, outBitmap);
output.copyTo(bitmap); ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
blur.setInput(overlayAlloc);
blur.setRadius(mRadius);
blur.forEach(overlayAlloc);
overlayAlloc.copyTo(outBitmap);
source.recycle(); source.recycle();
return BitmapResource.obtain(bitmap, mBitmapPool); rs.destroy();
return BitmapResource.obtain(outBitmap, mBitmapPool);
} }
// Stack Blur v1.0 from // Stack Blur v1.0 from