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

Merge pull request #1 from wasabeef/master

Update to 1.2.0
This commit is contained in:
start141 2015-09-17 18:25:55 +08:00
commit 57fcfce9df
10 changed files with 231 additions and 165 deletions

View File

@ -1,5 +1,10 @@
Change Log 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)* Version 1.1.0 *(2015-09-05)*
---------------------------- ----------------------------

View File

@ -32,7 +32,7 @@ repositories {
} }
dependencies { 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 // If you want to use the GPU Filters
compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.3.0' compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.3.0'
} }
@ -54,7 +54,7 @@ You can set a multiple transformations.
```java ```java
Glide.with(this).load(R.drawable.demo) 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)); .into((ImageView) findViewById(R.id.image));
``` ```
@ -76,7 +76,8 @@ android {
## Transformations ## Transformations
### Crop ### Crop
`CropTransformation`, `CropCircleTransformation`, `CropSquareTransformation` `CropTransformation`, `CropCircleTransformation`, `CropSquareTransformation`,
`RoundedCornersTransformation`
### Color ### Color
`ColorFilterTransformation`, `GrayscaleTransformation` `ColorFilterTransformation`, `GrayscaleTransformation`
@ -84,14 +85,16 @@ android {
### Blur ### Blur
`BlurTransformation` `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` `ToonFilterTransformation`, `SepiaFilterTransformation`, `ContrastFilterTransformation`
`InvertFilterTransformation`, `PixelationFilterTransformation`, `SketchFilterTransformation` `InvertFilterTransformation`, `PixelationFilterTransformation`, `SketchFilterTransformation`
`SwirlFilterTransformation`, `KuwaharaFilterTransformation`, `VignetteFilterTransformation` `SwirlFilterTransformation`, `KuwaharaFilterTransformation`, `VignetteFilterTransformation`
### Other
`RoundedCornersTransformation`
Applications using Glide Transformations Applications using Glide Transformations
--- ---

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.9 MiB

After

Width:  |  Height:  |  Size: 6.9 MiB

View File

@ -9,13 +9,9 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop; import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import java.util.List; import java.util.List;
import jp.wasabeef.glide.transformations.BlurTransformation; import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.ColorFilterTransformation; import jp.wasabeef.glide.transformations.ColorFilterTransformation;
import jp.wasabeef.glide.transformations.CropCircleTransformation; import jp.wasabeef.glide.transformations.CropCircleTransformation;
@ -79,90 +75,144 @@ public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
} }
@Override public void onBindViewHolder(MainAdapter.ViewHolder holder, int position) { @Override public void onBindViewHolder(MainAdapter.ViewHolder holder, int position) {
Transformation transformation = null;
switch (mDataSet.get(position)) { switch (mDataSet.get(position)) {
case Mask: case Mask:
transformation = new MaskTransformation(mContext, R.drawable.mask210);
Glide.with(mContext) Glide.with(mContext)
.load(R.drawable.demo) .load(R.drawable.demo)
.override(210, 210) .override(210, 210)
.bitmapTransform(new CenterCrop(mContext), transformation) .bitmapTransform(new CenterCrop(mContext),
.into(holder.image); new MaskTransformation(mContext, R.drawable.mask210))
.into(holder.image);
break; break;
case NinePatchMask: case NinePatchMask:
transformation = new NinePatchMaskTransformation(mContext, R.drawable.chat_me_mask, 300, 300);
Glide.with(mContext) Glide.with(mContext)
.load(R.drawable.demo) .load(R.drawable.demo)
.override(300, 300) .override(300, 300)
.bitmapTransform(new CenterCrop(mContext), transformation) .bitmapTransform(new CenterCrop(mContext),
.into(holder.image); new NinePatchMaskTransformation(mContext, R.drawable.chat_me_mask))
.into(holder.image);
break; break;
case CropTop: case CropTop:
transformation = Glide.with(mContext)
new CropTransformation(mContext, 300, 100, CropTransformation.CropType.TOP); .load(R.drawable.demo)
.bitmapTransform(
new CropTransformation(mContext, 300, 100, CropTransformation.CropType.TOP))
.into(holder.image);
break; break;
case CropCenter: 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; break;
case CropBottom: case CropBottom:
transformation = Glide.with(mContext)
new CropTransformation(mContext, 300, 100, CropTransformation.CropType.BOTTOM); .load(R.drawable.demo)
.bitmapTransform(
new CropTransformation(mContext, 300, 100, CropTransformation.CropType.BOTTOM))
.into(holder.image);
break; break;
case CropSquare: case CropSquare:
transformation = new CropSquareTransformation(mContext); Glide.with(mContext)
.load(R.drawable.demo)
.bitmapTransform(new CropSquareTransformation(mContext))
.into(holder.image);
break; break;
case CropCircle: case CropCircle:
transformation = new CropCircleTransformation(mContext); Glide.with(mContext)
.load(R.drawable.demo)
.bitmapTransform(new CropCircleTransformation(mContext))
.into(holder.image);
break; break;
case ColorFilter: 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; break;
case Grayscale: case Grayscale:
transformation = new GrayscaleTransformation(mContext); Glide.with(mContext)
.load(R.drawable.demo)
.bitmapTransform(new GrayscaleTransformation(mContext))
.into(holder.image);
break; break;
case RoundedCorners: 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; break;
case Blur: 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; break;
case Toon: case Toon:
transformation = new ToonFilterTransformation(mContext); Glide.with(mContext)
.load(R.drawable.demo)
.bitmapTransform(new ToonFilterTransformation(mContext))
.into(holder.image);
break; break;
case Sepia: case Sepia:
transformation = new SepiaFilterTransformation(mContext); Glide.with(mContext)
.load(R.drawable.check)
.bitmapTransform(new SepiaFilterTransformation(mContext))
.into(holder.image);
break; break;
case Contrast: 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; break;
case Invert: case Invert:
transformation = new InvertFilterTransformation(mContext); Glide.with(mContext)
.load(R.drawable.check)
.bitmapTransform(new InvertFilterTransformation(mContext))
.into(holder.image);
break; break;
case Pixel: case Pixel:
transformation = new PixelationFilterTransformation(mContext, 20); Glide.with(mContext)
.load(R.drawable.check)
.bitmapTransform(new PixelationFilterTransformation(mContext, 20))
.into(holder.image);
break; break;
case Sketch: case Sketch:
transformation = new SketchFilterTransformation(mContext); Glide.with(mContext)
.load(R.drawable.check)
.bitmapTransform(new SketchFilterTransformation(mContext))
.into(holder.image);
break; break;
case Swirl: case Swirl:
transformation = Glide.with(mContext)
new SwirlFilterTransformation(mContext, 0.5f, 1.0f, new PointF(0.5f, 0.5f)); .load(R.drawable.check)
.bitmapTransform(
new SwirlFilterTransformation(mContext, 0.5f, 1.0f, new PointF(0.5f, 0.5f)))
.into(holder.image);
break; break;
case Brightness: 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; break;
case Kuawahara: case Kuawahara:
transformation = new KuwaharaFilterTransformation(mContext, 25); Glide.with(mContext)
.load(R.drawable.check)
.bitmapTransform(new KuwaharaFilterTransformation(mContext, 25))
.into(holder.image);
break; break;
case Vignette: case Vignette:
transformation = new VignetteFilterTransformation(mContext, new PointF(0.5f, 0.5f), Glide.with(mContext)
new float[] { 0.0f, 0.0f, 0.0f }, 0f, 0.75f); .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; 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()); holder.title.setText(mDataSet.get(position).name());
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 484 KiB

View File

@ -1,5 +1,5 @@
VERSION_NAME=1.1.0 VERSION_NAME=1.2.0
VERSION_CODE=9 VERSION_CODE=10
GROUP=jp.wasabeef GROUP=jp.wasabeef
ARTIFACT_ID=glide-transformations ARTIFACT_ID=glide-transformations

View File

@ -16,7 +16,7 @@ android {
dependencies { dependencies {
compile "com.github.bumptech.glide:glide:${GLIDE_VERSION}" 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 -> android.libraryVariants.all { variant ->
@ -39,4 +39,4 @@ android.libraryVariants.all { variant ->
apply from: 'android-artifacts.gradle' apply from: 'android-artifacts.gradle'
apply from: 'central-publish.gradle' apply from: 'central-publish.gradle'
apply from: 'bintray-publish.gradle' apply from: 'bintray-publish.gradle'

View File

@ -24,70 +24,55 @@ import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode; import android.graphics.PorterDuffXfermode;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Build;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Transformation; import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.Resource; import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapResource; import com.bumptech.glide.load.resource.bitmap.BitmapResource;
import jp.wasabeef.glide.transformations.internal.Util;
public class MaskTransformation implements Transformation<Bitmap> { public class MaskTransformation implements Transformation<Bitmap> {
private Context mContext; private Context mContext;
private BitmapPool mBitmapPool; private BitmapPool mBitmapPool;
private int mMaskId; private int mMaskId;
public MaskTransformation(Context context, int maskId) { public MaskTransformation(Context context, int maskId) {
mBitmapPool = Glide.get(context).getBitmapPool(); mBitmapPool = Glide.get(context).getBitmapPool();
mContext = context; mContext = context;
mMaskId = maskId; mMaskId = maskId;
}
@Override
public Resource<Bitmap> transform(Resource<Bitmap> 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 Paint paint = new Paint();
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) { paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
Bitmap source = resource.get();
boolean maskFromBitmapPool = true; canvas = new Canvas(bitmap);
Bitmap mask = mBitmapPool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); canvas.drawBitmap(mask, new Rect(0, 0, width, height), new Rect(0, 0, width, height), null);
if (mask == null) { canvas.drawBitmap(source, 0, 0, paint);
maskFromBitmapPool = false;
mask = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
}
Drawable drawable; return BitmapResource.obtain(bitmap, mBitmapPool);
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 + ")";
}
@Override public String getId() {
return "MaskTransformation(maskId=" + mMaskId + ")";
}
} }

View File

@ -24,79 +24,59 @@ import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode; import android.graphics.PorterDuffXfermode;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Build;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Transformation; import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.Resource; import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapResource; import com.bumptech.glide.load.resource.bitmap.BitmapResource;
import com.bumptech.glide.request.target.Target; import com.bumptech.glide.request.target.Target;
import jp.wasabeef.glide.transformations.internal.Util;
public class NinePatchMaskTransformation implements Transformation<Bitmap> { public class NinePatchMaskTransformation implements Transformation<Bitmap> {
private Context mContext; private Context mContext;
private BitmapPool mBitmapPool; private BitmapPool mBitmapPool;
private int mMaskId; private int mMaskId;
private int mWidth;
private int mHeight;
public NinePatchMaskTransformation(Context context, int maskId, int width, int height) { public NinePatchMaskTransformation(Context context, int maskId) {
mBitmapPool = Glide.get(context).getBitmapPool(); mBitmapPool = Glide.get(context).getBitmapPool();
mContext = context; mContext = context;
mMaskId = maskId; mMaskId = maskId;
mWidth = width; }
mHeight = height;
@Override
public Resource<Bitmap> transform(Resource<Bitmap> 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 Paint paint = new Paint();
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) { paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
Bitmap source = resource.get();
int width = outWidth == Target.SIZE_ORIGINAL ? source.getWidth() : outWidth; canvas = new Canvas(bitmap);
int height = outHeight == Target.SIZE_ORIGINAL ? source.getHeight() : outHeight; 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; return BitmapResource.obtain(bitmap, mBitmapPool);
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 + ")";
}
@Override public String getId() {
return "NinePatchMaskTransformation(mMaskId=" + mMaskId + ")";
}
} }

View File

@ -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;
}
}