Glide加载图片与设置图片圆角
2017-12-15 15:24
357 查看
compile 'com.github.bumptech.glide:glide:3.8.0' 图形加载库
ImageView imgView=(ImageView) findViewById(R.id.circleImage);
//普通图片
Glide.with(context).load(url).into(img);
//圆形图片
Glide.with(context).load("http://oo6pz0u05.bkt.clouddn.com/17-12-13/69427561.jpg").transform(new GlideCircleImageView(context)).into(imgView);
//圆角图片
Glide.with(context).load("http://oo6pz0u05.bkt.clouddn.com/17-12-13/69427561.jpg").transform(new GlideRoundImageView(context,15)).into(imgView);
public class GlideCircleImageView extends BitmapTransformation {
public GlideCircleImageView(Context context) {
super(context);
}
@Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
// TODO this could be acquired from the pool too
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
return result;
}
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);
}
float radius = Resources.getSystem().getDisplayMetrics().density * 15f;
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();
}
}
public class GlideRoundImageView extends BitmapTransformation {
private static float radius = 0f;
public GlideRoundImageView(Context context) {
this(context, 4);
}
public GlideRoundImageView(Context context, int dp) {
super(context);
this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
}
@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;
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;
}
@Override public String getId() {
return getClass().getName() + Math.round(radius);
}
}
ImageView imgView=(ImageView) findViewById(R.id.circleImage);
//普通图片
Glide.with(context).load(url).into(img);
//圆形图片
Glide.with(context).load("http://oo6pz0u05.bkt.clouddn.com/17-12-13/69427561.jpg").transform(new GlideCircleImageView(context)).into(imgView);
//圆角图片
Glide.with(context).load("http://oo6pz0u05.bkt.clouddn.com/17-12-13/69427561.jpg").transform(new GlideRoundImageView(context,15)).into(imgView);
public class GlideCircleImageView extends BitmapTransformation {
public GlideCircleImageView(Context context) {
super(context);
}
@Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
// TODO this could be acquired from the pool too
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
return result;
}
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);
}
float radius = Resources.getSystem().getDisplayMetrics().density * 15f;
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();
}
}
public class GlideRoundImageView extends BitmapTransformation {
private static float radius = 0f;
public GlideRoundImageView(Context context) {
this(context, 4);
}
public GlideRoundImageView(Context context, int dp) {
super(context);
this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
}
@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;
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;
}
@Override public String getId() {
return getClass().getName() + Math.round(radius);
}
}
相关文章推荐
- Android Glide加载图片时转换为圆形、圆角、毛玻璃等图片效果
- Android开发:使用Glide动态加载圆形图片和圆角图片--图片开发框架
- Glide加载圆形图片和圆角图片的解决方式。
- 关于 Glide 加载图片圆角问题
- 解决Glide加载网络圆角图片先显示完整图片的问题
- Android Glide 加载圆形图片和圆角图片的两种方法
- Glide加载圆形,圆角图片
- 解决Glide加载网络圆角图片先显示完整图片的问题
- 解决Glide加载网络圆角图片先显示完整图片的问题
- Glide 加载圆角、圆形图片
- Glide设置圆角图片后设置ImageVIew的scanType="centerCrop"无效解决办法
- Android开发:使用Glide动态加载圆形图片和圆角图片
- Glide实现圆角图片加载
- Glide 加载圆角图片大小不一的问题
- 使用Glide动态加载圆形图片和圆角图片
- Glide加载圆形、圆角等图片的用法
- Android Glide加载图片时转换为圆形、圆角、毛玻璃等图片效果
- Android中Glide加载圆形图片和圆角图片实例代码
- Android Glide加载图片时转换为圆形、圆角、毛玻璃等图片效果
- Android Glide加载图片时转换为圆形、圆角、毛玻璃等图片效果