diff --git a/CHANGELOG.md b/CHANGELOG.md index 806bbb1..eba5ef1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ Change Log ========== +Version 1.2.0 *(2015-09-16)* +---------------------------- + +add new transformations MaskTransformation and NinePatchMaskTransformation. +Thanks to [start141](https://github.com/start141) Version 1.1.0 *(2015-09-05)* ---------------------------- diff --git a/README.md b/README.md index 5c77265..af8a9c4 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ repositories { } dependencies { - compile 'jp.wasabeef:glide-transformations:1.1.0' + compile 'jp.wasabeef:glide-transformations:1.2.0' // If you want to use the GPU Filters compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.3.0' } @@ -54,7 +54,7 @@ You can set a multiple transformations. ```java Glide.with(this).load(R.drawable.demo) - .bitmapTransform(new BlurTransformation(context, 25, 2), new CropCircleTransformation(pool)) + .bitmapTransform(new BlurTransformation(context, 25, 2), new CropCircleTransformation(context)) .into((ImageView) findViewById(R.id.image)); ``` @@ -76,7 +76,8 @@ android { ## Transformations ### Crop -`CropTransformation`, `CropCircleTransformation`, `CropSquareTransformation` +`CropTransformation`, `CropCircleTransformation`, `CropSquareTransformation`, +`RoundedCornersTransformation` ### Color `ColorFilterTransformation`, `GrayscaleTransformation` @@ -84,14 +85,16 @@ android { ### Blur `BlurTransformation` -### Filter (use [GPUImage](https://github.com/CyberAgent/android-gpuimage)) +### Mask +`MaskTransformation`, `NinePatchMaskTransformation` + +### GPU Filter (use [GPUImage](https://github.com/CyberAgent/android-gpuimage)) +**Will require add dependencies for GPUImage.** + `ToonFilterTransformation`, `SepiaFilterTransformation`, `ContrastFilterTransformation` `InvertFilterTransformation`, `PixelationFilterTransformation`, `SketchFilterTransformation` `SwirlFilterTransformation`, `KuwaharaFilterTransformation`, `VignetteFilterTransformation` -### Other -`RoundedCornersTransformation` - Applications using Glide Transformations --- diff --git a/art/demo.gif b/art/demo.gif index 32482f3..d5e9959 100644 Binary files a/art/demo.gif and b/art/demo.gif differ 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 dc7b9ee..59ea463 100644 --- a/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java +++ b/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java @@ -9,13 +9,9 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; - import com.bumptech.glide.Glide; -import com.bumptech.glide.load.Transformation; import com.bumptech.glide.load.resource.bitmap.CenterCrop; - import java.util.List; - import jp.wasabeef.glide.transformations.BlurTransformation; import jp.wasabeef.glide.transformations.ColorFilterTransformation; import jp.wasabeef.glide.transformations.CropCircleTransformation; @@ -79,90 +75,144 @@ public class MainAdapter extends RecyclerView.Adapter { } @Override public void onBindViewHolder(MainAdapter.ViewHolder holder, int position) { - Transformation transformation = null; switch (mDataSet.get(position)) { case Mask: - transformation = new MaskTransformation(mContext, R.drawable.mask210); Glide.with(mContext) - .load(R.drawable.demo) - .override(210, 210) - .bitmapTransform(new CenterCrop(mContext), transformation) - .into(holder.image); + .load(R.drawable.demo) + .override(210, 210) + .bitmapTransform(new CenterCrop(mContext), + new MaskTransformation(mContext, R.drawable.mask210)) + .into(holder.image); break; case NinePatchMask: - transformation = new NinePatchMaskTransformation(mContext, R.drawable.chat_me_mask, 300, 300); Glide.with(mContext) - .load(R.drawable.demo) - .override(300, 300) - .bitmapTransform(new CenterCrop(mContext), transformation) - .into(holder.image); + .load(R.drawable.demo) + .override(300, 300) + .bitmapTransform(new CenterCrop(mContext), + new NinePatchMaskTransformation(mContext, R.drawable.chat_me_mask)) + .into(holder.image); break; case CropTop: - transformation = - new CropTransformation(mContext, 300, 100, CropTransformation.CropType.TOP); + Glide.with(mContext) + .load(R.drawable.demo) + .bitmapTransform( + new CropTransformation(mContext, 300, 100, CropTransformation.CropType.TOP)) + .into(holder.image); break; case CropCenter: - transformation = new CropTransformation(mContext, 300, 100); + Glide.with(mContext) + .load(R.drawable.demo) + .bitmapTransform(new CropTransformation(mContext, 300, 100)) + .into(holder.image); break; case CropBottom: - transformation = - new CropTransformation(mContext, 300, 100, CropTransformation.CropType.BOTTOM); + Glide.with(mContext) + .load(R.drawable.demo) + .bitmapTransform( + new CropTransformation(mContext, 300, 100, CropTransformation.CropType.BOTTOM)) + .into(holder.image); + break; case CropSquare: - transformation = new CropSquareTransformation(mContext); + Glide.with(mContext) + .load(R.drawable.demo) + .bitmapTransform(new CropSquareTransformation(mContext)) + .into(holder.image); break; case CropCircle: - transformation = new CropCircleTransformation(mContext); + Glide.with(mContext) + .load(R.drawable.demo) + .bitmapTransform(new CropCircleTransformation(mContext)) + .into(holder.image); break; case ColorFilter: - transformation = new ColorFilterTransformation(mContext, Color.argb(80, 255, 0, 0)); + Glide.with(mContext) + .load(R.drawable.demo) + .bitmapTransform(new ColorFilterTransformation(mContext, Color.argb(80, 255, 0, 0))) + .into(holder.image); break; case Grayscale: - transformation = new GrayscaleTransformation(mContext); + Glide.with(mContext) + .load(R.drawable.demo) + .bitmapTransform(new GrayscaleTransformation(mContext)) + .into(holder.image); break; case RoundedCorners: - transformation = new RoundedCornersTransformation(mContext, 100, 0); + Glide.with(mContext) + .load(R.drawable.demo) + .bitmapTransform(new RoundedCornersTransformation(mContext, 100, 0)) + .into(holder.image); break; case Blur: - transformation = new BlurTransformation(mContext, 25, 1); + Glide.with(mContext) + .load(R.drawable.check) + .bitmapTransform(new BlurTransformation(mContext, 25, 1)) + .into(holder.image); break; case Toon: - transformation = new ToonFilterTransformation(mContext); + Glide.with(mContext) + .load(R.drawable.demo) + .bitmapTransform(new ToonFilterTransformation(mContext)) + .into(holder.image); break; case Sepia: - transformation = new SepiaFilterTransformation(mContext); + Glide.with(mContext) + .load(R.drawable.check) + .bitmapTransform(new SepiaFilterTransformation(mContext)) + .into(holder.image); break; case Contrast: - transformation = new ContrastFilterTransformation(mContext, 2.0f); + Glide.with(mContext) + .load(R.drawable.check) + .bitmapTransform(new ContrastFilterTransformation(mContext, 2.0f)) + .into(holder.image); break; case Invert: - transformation = new InvertFilterTransformation(mContext); + Glide.with(mContext) + .load(R.drawable.check) + .bitmapTransform(new InvertFilterTransformation(mContext)) + .into(holder.image); break; case Pixel: - transformation = new PixelationFilterTransformation(mContext, 20); + Glide.with(mContext) + .load(R.drawable.check) + .bitmapTransform(new PixelationFilterTransformation(mContext, 20)) + .into(holder.image); break; case Sketch: - transformation = new SketchFilterTransformation(mContext); + Glide.with(mContext) + .load(R.drawable.check) + .bitmapTransform(new SketchFilterTransformation(mContext)) + .into(holder.image); break; case Swirl: - transformation = - new SwirlFilterTransformation(mContext, 0.5f, 1.0f, new PointF(0.5f, 0.5f)); + Glide.with(mContext) + .load(R.drawable.check) + .bitmapTransform( + new SwirlFilterTransformation(mContext, 0.5f, 1.0f, new PointF(0.5f, 0.5f))) + .into(holder.image); + break; case Brightness: - transformation = new BrightnessFilterTransformation(mContext, 0.5f); + Glide.with(mContext) + .load(R.drawable.check) + .bitmapTransform(new BrightnessFilterTransformation(mContext, 0.5f)) + .into(holder.image); break; case Kuawahara: - transformation = new KuwaharaFilterTransformation(mContext, 25); + Glide.with(mContext) + .load(R.drawable.check) + .bitmapTransform(new KuwaharaFilterTransformation(mContext, 25)) + .into(holder.image); break; case Vignette: - transformation = new VignetteFilterTransformation(mContext, new PointF(0.5f, 0.5f), - new float[] { 0.0f, 0.0f, 0.0f }, 0f, 0.75f); + Glide.with(mContext) + .load(R.drawable.check) + .bitmapTransform(new VignetteFilterTransformation(mContext, new PointF(0.5f, 0.5f), + new float[] { 0.0f, 0.0f, 0.0f }, 0f, 0.75f)) + .into(holder.image); break; } - - if (mDataSet.get(position) != Type.Mask && mDataSet.get(position) != Type.NinePatchMask) { - Glide.with(mContext).load(R.drawable.demo).bitmapTransform(transformation).into(holder.image); - } holder.title.setText(mDataSet.get(position).name()); } diff --git a/example/src/main/res/drawable-xhdpi/check.png b/example/src/main/res/drawable-xhdpi/check.png new file mode 100644 index 0000000..270a993 Binary files /dev/null and b/example/src/main/res/drawable-xhdpi/check.png differ diff --git a/gradle.properties b/gradle.properties index 010e193..8df4ba7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ -VERSION_NAME=1.1.0 -VERSION_CODE=9 +VERSION_NAME=1.2.0 +VERSION_CODE=10 GROUP=jp.wasabeef ARTIFACT_ID=glide-transformations diff --git a/transformations/build.gradle b/transformations/build.gradle index 10be9d3..7bb4bd1 100644 --- a/transformations/build.gradle +++ b/transformations/build.gradle @@ -16,7 +16,7 @@ android { dependencies { compile "com.github.bumptech.glide:glide:${GLIDE_VERSION}" - compile "jp.co.cyberagent.android.gpuimage:gpuimage-library:${GPUIMAGE_VERSION}" + provided "jp.co.cyberagent.android.gpuimage:gpuimage-library:${GPUIMAGE_VERSION}" } android.libraryVariants.all { variant -> @@ -39,4 +39,4 @@ android.libraryVariants.all { variant -> apply from: 'android-artifacts.gradle' apply from: 'central-publish.gradle' -apply from: 'bintray-publish.gradle' \ No newline at end of file +apply from: 'bintray-publish.gradle' diff --git a/transformations/src/main/java/jp/wasabeef/glide/transformations/MaskTransformation.java b/transformations/src/main/java/jp/wasabeef/glide/transformations/MaskTransformation.java index 00eb5e6..d2bfbab 100644 --- a/transformations/src/main/java/jp/wasabeef/glide/transformations/MaskTransformation.java +++ b/transformations/src/main/java/jp/wasabeef/glide/transformations/MaskTransformation.java @@ -24,70 +24,55 @@ import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.drawable.Drawable; -import android.os.Build; - import com.bumptech.glide.Glide; import com.bumptech.glide.load.Transformation; import com.bumptech.glide.load.engine.Resource; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.resource.bitmap.BitmapResource; +import jp.wasabeef.glide.transformations.internal.Util; public class MaskTransformation implements Transformation { - private Context mContext; - private BitmapPool mBitmapPool; - private int mMaskId; + private Context mContext; + private BitmapPool mBitmapPool; + private int mMaskId; - public MaskTransformation(Context context, int maskId) { - mBitmapPool = Glide.get(context).getBitmapPool(); - mContext = context; - mMaskId = maskId; + public MaskTransformation(Context context, int maskId) { + mBitmapPool = Glide.get(context).getBitmapPool(); + mContext = context; + mMaskId = maskId; + } + + @Override + public Resource transform(Resource resource, int outWidth, int outHeight) { + Bitmap source = resource.get(); + + int width = source.getWidth(); + int height = source.getHeight(); + + Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + + Drawable drawable = Util.getMaskDrawable(mContext, mMaskId); + drawable.setBounds(0, 0, width, height); + Canvas canvas = new Canvas(mask); + drawable.draw(canvas); + + Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888); + if (bitmap == null) { + bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); } - @Override - public Resource transform(Resource resource, int outWidth, int outHeight) { - Bitmap source = resource.get(); + Paint paint = new Paint(); + paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); - boolean maskFromBitmapPool = true; - Bitmap mask = mBitmapPool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); - if (mask == null) { - maskFromBitmapPool = false; - mask = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); - } + canvas = new Canvas(bitmap); + canvas.drawBitmap(mask, new Rect(0, 0, width, height), new Rect(0, 0, width, height), null); + canvas.drawBitmap(source, 0, 0, paint); - Drawable drawable; - if (Build.VERSION.SDK_INT >= 21) { - drawable = mContext.getDrawable(mMaskId); - } else { - drawable = mContext.getResources().getDrawable(mMaskId); - } - drawable.setBounds(0, 0, source.getWidth(), source.getHeight()); - Canvas canvas = new Canvas(mask); - drawable.draw(canvas); - - Bitmap bitmap = mBitmapPool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); - if (bitmap == null) { - bitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); - } - - Paint paint = new Paint(); - paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); - - canvas = new Canvas(bitmap); - canvas.drawBitmap(mask, new Rect(0, 0, mask.getWidth(), mask.getHeight()), new Rect(0, 0, - source.getWidth(), source.getHeight()), null); - canvas.drawBitmap(source, 0, 0, paint); - - if (maskFromBitmapPool) { - mBitmapPool.put(mask); - } - - return BitmapResource.obtain(bitmap, mBitmapPool); - } - - @Override - public String getId() { - return "MaskTransformation(maskId=" + mMaskId + ")"; - } + return BitmapResource.obtain(bitmap, mBitmapPool); + } + @Override public String getId() { + return "MaskTransformation(maskId=" + mMaskId + ")"; + } } diff --git a/transformations/src/main/java/jp/wasabeef/glide/transformations/NinePatchMaskTransformation.java b/transformations/src/main/java/jp/wasabeef/glide/transformations/NinePatchMaskTransformation.java index b7d3b53..6184034 100644 --- a/transformations/src/main/java/jp/wasabeef/glide/transformations/NinePatchMaskTransformation.java +++ b/transformations/src/main/java/jp/wasabeef/glide/transformations/NinePatchMaskTransformation.java @@ -24,79 +24,59 @@ import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.drawable.Drawable; -import android.os.Build; - import com.bumptech.glide.Glide; import com.bumptech.glide.load.Transformation; import com.bumptech.glide.load.engine.Resource; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.resource.bitmap.BitmapResource; import com.bumptech.glide.request.target.Target; +import jp.wasabeef.glide.transformations.internal.Util; public class NinePatchMaskTransformation implements Transformation { - private Context mContext; - private BitmapPool mBitmapPool; - private int mMaskId; - private int mWidth; - private int mHeight; + private Context mContext; + private BitmapPool mBitmapPool; + private int mMaskId; - public NinePatchMaskTransformation(Context context, int maskId, int width, int height) { - mBitmapPool = Glide.get(context).getBitmapPool(); - mContext = context; - mMaskId = maskId; - mWidth = width; - mHeight = height; + public NinePatchMaskTransformation(Context context, int maskId) { + mBitmapPool = Glide.get(context).getBitmapPool(); + mContext = context; + mMaskId = maskId; + } + + @Override + public Resource transform(Resource resource, int outWidth, int outHeight) { + Bitmap source = resource.get(); + + int width = source.getWidth(); + int height = source.getHeight(); + int maskWidth = outWidth == Target.SIZE_ORIGINAL ? width : outWidth; + int maskHeight = outHeight == Target.SIZE_ORIGINAL ? height : outHeight; + + Bitmap mask = Bitmap.createBitmap(maskWidth, maskHeight, Bitmap.Config.ARGB_8888); + + Drawable drawable = Util.getMaskDrawable(mContext, mMaskId); + drawable.setBounds(0, 0, maskWidth, maskHeight); + Canvas canvas = new Canvas(mask); + drawable.draw(canvas); + + Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888); + if (bitmap == null) { + bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); } - @Override - public Resource transform(Resource resource, int outWidth, int outHeight) { - Bitmap source = resource.get(); + Paint paint = new Paint(); + paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); - int width = outWidth == Target.SIZE_ORIGINAL ? source.getWidth() : outWidth; - int height = outHeight == Target.SIZE_ORIGINAL ? source.getHeight() : outHeight; + canvas = new Canvas(bitmap); + canvas.drawBitmap(mask, new Rect(0, 0, maskWidth, maskHeight), new Rect(0, 0, width, height), + null); + canvas.drawBitmap(source, 0, 0, paint); - boolean maskFromBitmapPool = true; - Bitmap mask = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888); - if (mask == null) { - maskFromBitmapPool = false; - mask = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); - } - - Drawable drawable; - if (Build.VERSION.SDK_INT >= 21) { - drawable = mContext.getDrawable(mMaskId); - } else { - drawable = mContext.getResources().getDrawable(mMaskId); - } - drawable.setBounds(0, 0, width, height); - Canvas canvas = new Canvas(mask); - drawable.draw(canvas); - - Bitmap bitmap = mBitmapPool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); - if (bitmap == null) { - bitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); - } - - Paint paint = new Paint(); - paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); - - canvas = new Canvas(bitmap); - canvas.drawBitmap(mask, new Rect(0, 0, mask.getWidth(), mask.getHeight()), new Rect(0, 0, - source.getWidth(), source.getHeight()), null); - canvas.drawBitmap(source, 0, 0, paint); - - if (maskFromBitmapPool) { - mBitmapPool.put(mask); - } - - return BitmapResource.obtain(bitmap, mBitmapPool); - } - - @Override - public String getId() { - return "NinePatchMaskTransformation(mMaskId=" + mMaskId - + ", width=" + mWidth + ", height=" + mHeight + ")"; - } + return BitmapResource.obtain(bitmap, mBitmapPool); + } + @Override public String getId() { + return "NinePatchMaskTransformation(mMaskId=" + mMaskId + ")"; + } } diff --git a/transformations/src/main/java/jp/wasabeef/glide/transformations/internal/Util.java b/transformations/src/main/java/jp/wasabeef/glide/transformations/internal/Util.java new file mode 100644 index 0000000..075e494 --- /dev/null +++ b/transformations/src/main/java/jp/wasabeef/glide/transformations/internal/Util.java @@ -0,0 +1,43 @@ +package jp.wasabeef.glide.transformations.internal; + +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.os.Build; + +/** + * Copyright (C) 2015 Wasabeef + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +public final class Util { + + private Util() { + // Utility class. + } + + public static Drawable getMaskDrawable(Context context, int maskId) { + Drawable drawable; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + drawable = context.getDrawable(maskId); + } else { + drawable = context.getResources().getDrawable(maskId); + } + + if (drawable == null) { + throw new IllegalArgumentException("maskId is invalid"); + } + + return drawable; + } +}