| | |
| | | import android.graphics.Canvas; |
| | | import android.graphics.Paint; |
| | | import android.graphics.RectF; |
| | | import android.support.annotation.NonNull; |
| | | |
| | | import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; |
| | | import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; |
| | | |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.security.MessageDigest; |
| | | |
| | | /** |
| | | * glide 圆角图片加载 |
| | | * |
| | | * |
| | | * @author weikou2015 |
| | | * |
| | | */ |
| | | public class GlideRoundTransform extends BitmapTransformation { |
| | | private static float radius = 0f; |
| | | private static final String ID = GlideRoundTransform.class.getClass().getName(); |
| | | private static byte[] ID_BYTES = null; |
| | | private static float radius = 0f; |
| | | |
| | | public GlideRoundTransform(Context context) { |
| | | this(context, 12); |
| | | } |
| | | public GlideRoundTransform(Context context) { |
| | | this(context, 12); |
| | | try { |
| | | ID_BYTES = ID.getBytes(STRING_CHARSET_NAME); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | public GlideRoundTransform(Context context, int dp) { |
| | | super(context); |
| | | this.radius = Resources.getSystem().getDisplayMetrics().density * dp; |
| | | } |
| | | public GlideRoundTransform(Context context, int dp) { |
| | | this.radius = Resources.getSystem().getDisplayMetrics().density * dp; |
| | | try { |
| | | ID_BYTES = ID.getBytes(STRING_CHARSET_NAME); |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | protected Bitmap transform(BitmapPool pool, Bitmap toTransform, |
| | | int outWidth, int outHeight) { |
| | | return roundCrop(pool, toTransform); |
| | | } |
| | | @Override |
| | | protected Bitmap transform(BitmapPool pool, Bitmap toTransform, |
| | | int outWidth, int outHeight) { |
| | | return roundCrop(pool, toTransform); |
| | | } |
| | | |
| | | private static Bitmap roundCrop(BitmapPool pool, Bitmap source) { |
| | | if (source == null) |
| | | return null; |
| | | private static Bitmap roundCrop(BitmapPool pool, Bitmap source) { |
| | | if (source == null) |
| | | return null; |
| | | |
| | | Bitmap result = pool.get(source.getWidth(), source.getHeight(), |
| | | Bitmap.Config.ARGB_8888); |
| | | if (result == null) { |
| | | result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), |
| | | Bitmap.Config.ARGB_8888); |
| | | } |
| | | Bitmap result = pool.get(source.getWidth(), source.getHeight(), |
| | | Bitmap.Config.ARGB_8888); |
| | | if (result == null) { |
| | | result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), |
| | | Bitmap.Config.ARGB_8888); |
| | | } |
| | | |
| | | Canvas canvas = new Canvas(result); |
| | | Paint paint = new Paint(); |
| | | paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, |
| | | BitmapShader.TileMode.CLAMP)); |
| | | paint.setAntiAlias(true); |
| | | RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight()); |
| | | canvas.drawRoundRect(rectF, radius, radius, paint); |
| | | return result; |
| | | } |
| | | Canvas canvas = new Canvas(result); |
| | | Paint paint = new Paint(); |
| | | paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, |
| | | BitmapShader.TileMode.CLAMP)); |
| | | paint.setAntiAlias(true); |
| | | RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight()); |
| | | canvas.drawRoundRect(rectF, radius, radius, paint); |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public String getId() { |
| | | return getClass().getName() + Math.round(radius); |
| | | } |
| | | @Override |
| | | public int hashCode() { |
| | | return ID.hashCode(); |
| | | } |
| | | |
| | | @Override |
| | | public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) { |
| | | messageDigest.update(ID_BYTES); |
| | | } |
| | | } |