mirror of
https://github.com/wasabeef/glide-transformations.git
synced 2025-06-08 07:24:05 +08:00
Merge pull request #21 from start141/master
Merged NinePatchMaskTransformation to MaskTransformation
This commit is contained in:
commit
f1246ff9ee
@ -19,7 +19,6 @@ import jp.wasabeef.glide.transformations.CropSquareTransformation;
|
|||||||
import jp.wasabeef.glide.transformations.CropTransformation;
|
import jp.wasabeef.glide.transformations.CropTransformation;
|
||||||
import jp.wasabeef.glide.transformations.GrayscaleTransformation;
|
import jp.wasabeef.glide.transformations.GrayscaleTransformation;
|
||||||
import jp.wasabeef.glide.transformations.MaskTransformation;
|
import jp.wasabeef.glide.transformations.MaskTransformation;
|
||||||
import jp.wasabeef.glide.transformations.NinePatchMaskTransformation;
|
|
||||||
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
|
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
|
||||||
import jp.wasabeef.glide.transformations.gpu.BrightnessFilterTransformation;
|
import jp.wasabeef.glide.transformations.gpu.BrightnessFilterTransformation;
|
||||||
import jp.wasabeef.glide.transformations.gpu.ContrastFilterTransformation;
|
import jp.wasabeef.glide.transformations.gpu.ContrastFilterTransformation;
|
||||||
@ -77,19 +76,23 @@ 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) {
|
||||||
switch (mDataSet.get(position)) {
|
switch (mDataSet.get(position)) {
|
||||||
case Mask:
|
case Mask:
|
||||||
|
int width = Utils.dip2px(mContext, 133.33f);
|
||||||
|
int height = Utils.dip2px(mContext, 126.33f);
|
||||||
Glide.with(mContext)
|
Glide.with(mContext)
|
||||||
.load(R.drawable.demo)
|
.load(R.drawable.check)
|
||||||
.override(210, 210)
|
.override(width, height)
|
||||||
.bitmapTransform(new CenterCrop(mContext),
|
.bitmapTransform(new CenterCrop(mContext),
|
||||||
new MaskTransformation(mContext, R.drawable.mask210))
|
new MaskTransformation(mContext, R.drawable.mask_starfish))
|
||||||
.into(holder.image);
|
.into(holder.image);
|
||||||
break;
|
break;
|
||||||
case NinePatchMask:
|
case NinePatchMask:
|
||||||
|
width = Utils.dip2px(mContext, 150.0f);
|
||||||
|
height = Utils.dip2px(mContext, 100.0f);
|
||||||
Glide.with(mContext)
|
Glide.with(mContext)
|
||||||
.load(R.drawable.demo)
|
.load(R.drawable.check)
|
||||||
.override(300, 300)
|
.override(width, height)
|
||||||
.bitmapTransform(new CenterCrop(mContext),
|
.bitmapTransform(new CenterCrop(mContext),
|
||||||
new NinePatchMaskTransformation(mContext, R.drawable.chat_me_mask))
|
new MaskTransformation(mContext, R.drawable.mask_chat_right))
|
||||||
.into(holder.image);
|
.into(holder.image);
|
||||||
break;
|
break;
|
||||||
case CropTop:
|
case CropTop:
|
||||||
@ -191,7 +194,6 @@ public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
|
|||||||
.bitmapTransform(
|
.bitmapTransform(
|
||||||
new SwirlFilterTransformation(mContext, 0.5f, 1.0f, new PointF(0.5f, 0.5f)))
|
new SwirlFilterTransformation(mContext, 0.5f, 1.0f, new PointF(0.5f, 0.5f)))
|
||||||
.into(holder.image);
|
.into(holder.image);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case Brightness:
|
case Brightness:
|
||||||
Glide.with(mContext)
|
Glide.with(mContext)
|
||||||
|
27
example/src/main/java/jp/wasabeef/example/glide/Utils.java
Normal file
27
example/src/main/java/jp/wasabeef/example/glide/Utils.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package jp.wasabeef.example.glide;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
public class Utils {
|
||||||
|
|
||||||
|
public static int dip2px(Context context, float dp) {
|
||||||
|
float scale = context.getResources().getDisplayMetrics().density;
|
||||||
|
return (int) (dp * scale + 0.5f);
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
Before Width: | Height: | Size: 19 KiB |
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 1.9 KiB |
BIN
example/src/main/res/drawable-xxhdpi/mask_starfish.png
Normal file
BIN
example/src/main/res/drawable-xxhdpi/mask_starfish.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 7.5 KiB |
@ -22,21 +22,30 @@ import android.graphics.Canvas;
|
|||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
import android.graphics.PorterDuff;
|
import android.graphics.PorterDuff;
|
||||||
import android.graphics.PorterDuffXfermode;
|
import android.graphics.PorterDuffXfermode;
|
||||||
import android.graphics.Rect;
|
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
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;
|
import jp.wasabeef.glide.transformations.internal.Utils;
|
||||||
|
|
||||||
public class MaskTransformation implements Transformation<Bitmap> {
|
public class MaskTransformation implements Transformation<Bitmap> {
|
||||||
|
|
||||||
|
private static Paint mMaskingPaint = new Paint();
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private BitmapPool mBitmapPool;
|
private BitmapPool mBitmapPool;
|
||||||
private int mMaskId;
|
private int mMaskId;
|
||||||
|
|
||||||
|
static {
|
||||||
|
mMaskingPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param maskId If you change the mask file, please also rename the mask file, or Glide will get
|
||||||
|
* the cache with the old mask. Because getId() return the same values if using the
|
||||||
|
* same make file name. If you have a good idea please tell us, thanks.
|
||||||
|
*/
|
||||||
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;
|
||||||
@ -46,33 +55,27 @@ public class MaskTransformation implements Transformation<Bitmap> {
|
|||||||
@Override
|
@Override
|
||||||
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
|
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
|
||||||
Bitmap source = resource.get();
|
Bitmap source = resource.get();
|
||||||
|
|
||||||
int width = source.getWidth();
|
int width = source.getWidth();
|
||||||
int height = source.getHeight();
|
int height = source.getHeight();
|
||||||
|
|
||||||
Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
Bitmap result = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
|
||||||
|
if (result == null) {
|
||||||
Drawable drawable = Util.getMaskDrawable(mContext, mMaskId);
|
result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Paint paint = new Paint();
|
Drawable mask = Utils.getMaskDrawable(mContext, mMaskId);
|
||||||
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
|
|
||||||
|
|
||||||
canvas = new Canvas(bitmap);
|
Canvas canvas = new Canvas(result);
|
||||||
canvas.drawBitmap(mask, new Rect(0, 0, width, height), new Rect(0, 0, width, height), null);
|
mask.setBounds(0, 0, width, height);
|
||||||
canvas.drawBitmap(source, 0, 0, paint);
|
mask.draw(canvas);
|
||||||
|
canvas.drawBitmap(source, 0, 0, mMaskingPaint);
|
||||||
|
|
||||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
return BitmapResource.obtain(result, mBitmapPool);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public String getId() {
|
@Override
|
||||||
return "MaskTransformation(maskId=" + mMaskId + ")";
|
public String getId() {
|
||||||
|
return "MaskTransformation(maskId="
|
||||||
|
+ mContext.getResources().getResourceEntryName(mMaskId) + ")";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,82 +0,0 @@
|
|||||||
package jp.wasabeef.glide.transformations;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.graphics.Bitmap;
|
|
||||||
import android.graphics.Canvas;
|
|
||||||
import android.graphics.Paint;
|
|
||||||
import android.graphics.PorterDuff;
|
|
||||||
import android.graphics.PorterDuffXfermode;
|
|
||||||
import android.graphics.Rect;
|
|
||||||
import android.graphics.drawable.Drawable;
|
|
||||||
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<Bitmap> {
|
|
||||||
|
|
||||||
private Context mContext;
|
|
||||||
private BitmapPool mBitmapPool;
|
|
||||||
private int mMaskId;
|
|
||||||
|
|
||||||
public NinePatchMaskTransformation(Context context, int maskId) {
|
|
||||||
mBitmapPool = Glide.get(context).getBitmapPool();
|
|
||||||
mContext = context;
|
|
||||||
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();
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
Paint paint = new Paint();
|
|
||||||
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
return BitmapResource.obtain(bitmap, mBitmapPool);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public String getId() {
|
|
||||||
return "NinePatchMaskTransformation(mMaskId=" + mMaskId + ")";
|
|
||||||
}
|
|
||||||
}
|
|
@ -20,9 +20,9 @@ import android.os.Build;
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public final class Util {
|
public final class Utils {
|
||||||
|
|
||||||
private Util() {
|
private Utils() {
|
||||||
// Utility class.
|
// Utility class.
|
||||||
}
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user