From cfe6dc313a7b9d57594cf19312fe692bf3fe9c89 Mon Sep 17 00:00:00 2001 From: wasabeef Date: Fri, 24 Jul 2015 18:00:07 +0900 Subject: [PATCH] Added the DownSampling to BlurTransformation --- build.gradle | 4 +- example/build.gradle | 8 ++-- .../wasabeef/example/glide/MainAdapter.java | 4 +- .../src/main/res/layout/layout_list_item.xml | 5 +-- gradle.properties | 8 +++- transformations/build.gradle | 5 +-- .../transformations/BlurTransformation.java | 37 ++++++++++++++----- 7 files changed, 45 insertions(+), 26 deletions(-) diff --git a/build.gradle b/build.gradle index 95c3cdc..0a0e8a3 100644 --- a/build.gradle +++ b/build.gradle @@ -5,8 +5,8 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:1.2.3' - classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0' + classpath 'com.android.tools.build:gradle:1.3.0-beta4' + classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/example/build.gradle b/example/build.gradle index 3d5ffd3..751ace9 100644 --- a/example/build.gradle +++ b/example/build.gradle @@ -53,8 +53,8 @@ def getKeyAliasPasswordProperty() { dependencies { compile project(':transformations') - compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.2.3' - compile 'com.android.support:appcompat-v7:22.2.0' - compile 'com.android.support:recyclerview-v7:22.2.0' - compile 'com.github.bumptech.glide:glide:3.6.1' + compile "com.github.bumptech.glide:glide:${GLIDE_VERSION}" + compile "jp.co.cyberagent.android.gpuimage:gpuimage-library:${GPUIMAGE_VERSION}" + compile "com.android.support:appcompat-v7:${SUPPORT_PACKAGE_VERSION}" + compile "com.android.support:recyclerview-v7:${SUPPORT_PACKAGE_VERSION}" } diff --git a/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java b/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java index c6af123..aa4a3cc 100644 --- a/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java +++ b/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java @@ -81,7 +81,7 @@ public class MainAdapter extends RecyclerView.Adapter { @Override public void onBindViewHolder(MainAdapter.ViewHolder holder, int position) { - Transformation transformation = null; + Transformation transformation = null; switch (mDataSet.get(position)) { case CropTop: transformation = @@ -110,7 +110,7 @@ public class MainAdapter extends RecyclerView.Adapter { transformation = new RoundedCornersTransformation(mPool, 100, 0); break; case Blur: - transformation = new BlurTransformation(mContext, mPool, 10); + transformation = new BlurTransformation(mContext, mPool, 25, 1); break; case Toon: transformation = new ToonFilterTransformation(mContext, mPool); diff --git a/example/src/main/res/layout/layout_list_item.xml b/example/src/main/res/layout/layout_list_item.xml index 0355c27..005b7e4 100644 --- a/example/src/main/res/layout/layout_list_item.xml +++ b/example/src/main/res/layout/layout_list_item.xml @@ -6,11 +6,10 @@ + android:contentDescription="@null"/> diff --git a/transformations/src/main/java/jp/wasabeef/glide/transformations/BlurTransformation.java b/transformations/src/main/java/jp/wasabeef/glide/transformations/BlurTransformation.java index 6054a94..ed6c765 100644 --- a/transformations/src/main/java/jp/wasabeef/glide/transformations/BlurTransformation.java +++ b/transformations/src/main/java/jp/wasabeef/glide/transformations/BlurTransformation.java @@ -24,27 +24,36 @@ import com.bumptech.glide.load.resource.bitmap.BitmapResource; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; +import android.graphics.Paint; import android.support.v8.renderscript.Allocation; +import android.support.v8.renderscript.Element; import android.support.v8.renderscript.RenderScript; import android.support.v8.renderscript.ScriptIntrinsicBlur; public class BlurTransformation implements Transformation { private static int MAX_RADIUS = 25; + private static int DEFAULT_DOWN_SAMPLING = 1; private Context mContext; private BitmapPool mBitmapPool; private int mRadius; + private int mSampling; public BlurTransformation(Context context, BitmapPool pool) { - this(context, pool, MAX_RADIUS); + this(context, pool, MAX_RADIUS, DEFAULT_DOWN_SAMPLING); } public BlurTransformation(Context context, BitmapPool pool, int radius) { + this(context, pool, radius, DEFAULT_DOWN_SAMPLING); + } + + public BlurTransformation(Context context, BitmapPool pool, int radius, int sampling) { mContext = context; mBitmapPool = pool; mRadius = radius; + mSampling = sampling; } @Override @@ -53,22 +62,30 @@ public class BlurTransformation implements Transformation { int width = source.getWidth(); int height = source.getHeight(); + int scaledWidth = width / mSampling; + int scaledHeight = height / mSampling; - Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888); + Bitmap bitmap = mBitmapPool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888); if (bitmap == null) { - bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(bitmap); - canvas.drawBitmap(source, 0, 0, null); + canvas.scale(1 / (float) mSampling, 1 / (float) mSampling); + Paint paint = new Paint(); + paint.setFlags(Paint.FILTER_BITMAP_FLAG); + canvas.drawBitmap(source, 0, 0, paint); RenderScript rs = RenderScript.create(mContext); - Allocation overlayAlloc = Allocation.createFromBitmap(rs, bitmap); - ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement()); - blur.setInput(overlayAlloc); + Allocation input = Allocation.createFromBitmap(rs, bitmap, + Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT); + Allocation output = Allocation.createTyped(rs, input.getType()); + ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); + + blur.setInput(input); blur.setRadius(mRadius); - blur.forEach(overlayAlloc); - overlayAlloc.copyTo(bitmap); + blur.forEach(output); + output.copyTo(bitmap); rs.destroy(); @@ -77,6 +94,6 @@ public class BlurTransformation implements Transformation { @Override public String getId() { - return "BlurTransformation(radius=" + mRadius + ")"; + return "BlurTransformation(radius=" + mRadius + ", sampling=" + mSampling + ")"; } }