| | |
| | | import android.graphics.Paint; |
| | | import android.graphics.RectF; |
| | | import android.support.annotation.NonNull; |
| | | import android.util.Log; |
| | | |
| | | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; |
| | | import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; |
| | | import com.bumptech.glide.load.resource.bitmap.TransformationUtils; |
| | | import com.bumptech.glide.util.Util; |
| | | |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.nio.ByteBuffer; |
| | | import java.security.MessageDigest; |
| | | |
| | | /** |
| | |
| | | */ |
| | | public class GlideRoundTransform extends BitmapTransformation { |
| | | private static final String ID = GlideRoundTransform.class.getClass().getName(); |
| | | private static final String TAG = "GlideRoundTransform"; |
| | | private static byte[] ID_BYTES = null; |
| | | private static float radius = 0f; |
| | | private float radius = 0f; |
| | | |
| | | public GlideRoundTransform(Context context) { |
| | | this(context, 12); |
| | |
| | | @Override |
| | | protected Bitmap transform(BitmapPool pool, Bitmap toTransform, |
| | | int outWidth, int outHeight) { |
| | | return roundCrop(pool, toTransform); |
| | | //处理centercrop |
| | | Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight); |
| | | return roundCrop(pool, bitmap == null ? toTransform : bitmap); |
| | | } |
| | | |
| | | private static Bitmap roundCrop(BitmapPool pool, Bitmap source) { |
| | | private Bitmap roundCrop(BitmapPool pool, Bitmap source) { |
| | | if (source == null) |
| | | return null; |
| | | |
| | | Log.i(TAG, "源图片大小:" + source.getWidth() + "-" + source.getHeight()); |
| | | |
| | | Bitmap result = pool.get(source.getWidth(), source.getHeight(), |
| | | Bitmap.Config.ARGB_8888); |
| | |
| | | |
| | | @Override |
| | | public int hashCode() { |
| | | return ID.hashCode(); |
| | | return Util.hashCode(ID.hashCode(), |
| | | Util.hashCode(radius)); |
| | | } |
| | | |
| | | @Override |
| | | public boolean equals(Object o) { |
| | | if (o instanceof GlideRoundTransform) { |
| | | GlideRoundTransform other = (GlideRoundTransform) o; |
| | | return radius == other.radius; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) { |
| | | messageDigest.update(ID_BYTES); |
| | | byte[] radiusData = ByteBuffer.allocate(4).putInt((int) radius).array(); |
| | | messageDigest.update(radiusData); |
| | | } |
| | | } |