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 5826253..8220cc8 100644 --- a/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java +++ b/example/src/main/java/jp/wasabeef/example/glide/MainAdapter.java @@ -19,7 +19,6 @@ import jp.wasabeef.glide.transformations.CropSquareTransformation; import jp.wasabeef.glide.transformations.CropTransformation; import jp.wasabeef.glide.transformations.GrayscaleTransformation; import jp.wasabeef.glide.transformations.MaskTransformation; -import jp.wasabeef.glide.transformations.NinePatchMaskTransformation; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; import jp.wasabeef.glide.transformations.gpu.BrightnessFilterTransformation; import jp.wasabeef.glide.transformations.gpu.ContrastFilterTransformation; @@ -77,19 +76,23 @@ public class MainAdapter extends RecyclerView.Adapter { @Override public void onBindViewHolder(MainAdapter.ViewHolder holder, int position) { switch (mDataSet.get(position)) { case Mask: + int width = Utils.dip2px(mContext, 133.33f); + int height = Utils.dip2px(mContext, 126.33f); Glide.with(mContext) - .load(R.drawable.demo) - .override(210, 210) + .load(R.drawable.check) + .override(width, height) .bitmapTransform(new CenterCrop(mContext), - new MaskTransformation(mContext, R.drawable.mask210)) + new MaskTransformation(mContext, R.drawable.mask_starfish)) .into(holder.image); break; case NinePatchMask: + width = Utils.dip2px(mContext, 150.0f); + height = Utils.dip2px(mContext, 100.0f); Glide.with(mContext) - .load(R.drawable.demo) - .override(300, 300) + .load(R.drawable.check) + .override(width, height) .bitmapTransform(new CenterCrop(mContext), - new NinePatchMaskTransformation(mContext, R.drawable.chat_me_mask)) + new MaskTransformation(mContext, R.drawable.mask_chat_right)) .into(holder.image); break; case CropTop: diff --git a/example/src/main/java/jp/wasabeef/example/glide/Utils.java b/example/src/main/java/jp/wasabeef/example/glide/Utils.java new file mode 100644 index 0000000..6e0f24f --- /dev/null +++ b/example/src/main/java/jp/wasabeef/example/glide/Utils.java @@ -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); + } +} diff --git a/example/src/main/res/drawable-xxhdpi/mask210.png b/example/src/main/res/drawable-xxhdpi/mask210.png deleted file mode 100644 index f8d0757..0000000 Binary files a/example/src/main/res/drawable-xxhdpi/mask210.png and /dev/null differ diff --git a/example/src/main/res/drawable-xxhdpi/chat_me_mask.9.png b/example/src/main/res/drawable-xxhdpi/mask_chat_right.9.png similarity index 100% rename from example/src/main/res/drawable-xxhdpi/chat_me_mask.9.png rename to example/src/main/res/drawable-xxhdpi/mask_chat_right.9.png diff --git a/example/src/main/res/drawable-xxhdpi/mask_starfish.png b/example/src/main/res/drawable-xxhdpi/mask_starfish.png new file mode 100644 index 0000000..264ed87 Binary files /dev/null and b/example/src/main/res/drawable-xxhdpi/mask_starfish.png differ 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 2eb859e..fff0b76 100644 --- a/transformations/src/main/java/jp/wasabeef/glide/transformations/MaskTransformation.java +++ b/transformations/src/main/java/jp/wasabeef/glide/transformations/MaskTransformation.java @@ -23,14 +23,12 @@ import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; 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 jp.wasabeef.glide.transformations.internal.Util; +import jp.wasabeef.glide.transformations.internal.Utils; public class MaskTransformation implements Transformation { @@ -44,9 +42,9 @@ public class MaskTransformation implements Transformation { } /** - * @param maskId If you change the mask file, please rename the mask file, or Glide will get the - * cache with the old mask. Because getId() will return the same values if using the - * same make file name. If you have a good idea please tell we, thanks. + * @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) { mBitmapPool = Glide.get(context).getBitmapPool(); @@ -65,7 +63,7 @@ public class MaskTransformation implements Transformation { result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); } - Drawable mask = Util.getMaskDrawable(mContext, mMaskId); + Drawable mask = Utils.getMaskDrawable(mContext, mMaskId); Canvas canvas = new Canvas(result); mask.setBounds(0, 0, width, height); @@ -75,7 +73,8 @@ public class MaskTransformation implements Transformation { return BitmapResource.obtain(result, mBitmapPool); } - @Override public String getId() { + @Override + public String getId() { return "MaskTransformation(maskId=" + mContext.getResources().getResourceEntryName(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 deleted file mode 100644 index 280fbeb..0000000 --- a/transformations/src/main/java/jp/wasabeef/glide/transformations/NinePatchMaskTransformation.java +++ /dev/null @@ -1,83 +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.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 { - - private static Paint mMaskingPaint = new Paint(); - private Context mContext; - private BitmapPool mBitmapPool; - private int mMaskId; - - static { - mMaskingPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); - } - - /** - * @param maskId If you change the mask file, please rename the mask file, or Glide will get the - * cache with the old mask. Because getId() will return the same values if using the - * same make file name. If you have a good idea please tell we, thanks. - */ - 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 = outWidth == Target.SIZE_ORIGINAL ? source.getWidth() : outWidth; - int height = outHeight == Target.SIZE_ORIGINAL ? source.getHeight() : outHeight; - - Bitmap result = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888); - if (result == null) { - result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); - } - - Drawable mask = Util.getMaskDrawable(mContext, mMaskId); - - Canvas canvas = new Canvas(result); - mask.setBounds(0, 0, width, height); - mask.draw(canvas); - canvas.drawBitmap(source, 0, 0, mMaskingPaint); - - return BitmapResource.obtain(result, mBitmapPool); - } - - @Override public String getId() { - return "NinePatchMaskTransformation(mMaskId=" - + mContext.getResources().getResourceEntryName(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/Utils.java similarity index 96% rename from transformations/src/main/java/jp/wasabeef/glide/transformations/internal/Util.java rename to transformations/src/main/java/jp/wasabeef/glide/transformations/internal/Utils.java index 075e494..2d34f2b 100644 --- a/transformations/src/main/java/jp/wasabeef/glide/transformations/internal/Util.java +++ b/transformations/src/main/java/jp/wasabeef/glide/transformations/internal/Utils.java @@ -20,9 +20,9 @@ import android.os.Build; * limitations under the License. */ -public final class Util { +public final class Utils { - private Util() { + private Utils() { // Utility class. }