一,设置占位图:
RequestOptions options = new RequestOptions()
.placeholder(R.drawable.img_default)//图片加载出来前,显示的图片
.fallback( R.drawable.img_blank) //url为空的时候,显示的图片
.error(drawable.img_load_failure);//图片加载失败后,显示的图片
Glide.with(this)
.load(URL) //图片地址
.apply(options)
.into(ImagView);
其他相关知识:
二,Glide4设置图片圆角
2.1,第一种方式:
RequestOptions options = new RequestOptions().
error(R.drawable.img_load_failure).
bitmapTransform(new RoundedCorners(30));//图片圆角为30
Glide.with(this)
.load(URL) //图片地址
.apply(options)
.into(ImagView);
2.2,第二种方式:
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.ic_launcher_background);
requestOptions.circleCropTransform();
requestOptions.transforms( new RoundedCorners(30));
Glide.with(this)
.load(URL) //图片地址
.apply(options)
.into(ImagView);
2.3,第三种方式:
RequestOptions options = new RequestOptions()
.centerCrop()
.transform(new RoundTransform(this,30));
Glide.with(this)
.load(URL) //图片地址
.apply(options)
.into(ImagView);
public class RoundTransform extends BitmapTransformation {
private static float radius = 0f;
public RoundTransform(Context context) {
this(context, 4);
}
public RoundTransform(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) {
Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight);
return roundCrop(pool, bitmap);
}
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;
}
public String getId() {
return getClass().getName() + Math.round(radius);
}
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
}
}