First, set the placeholder map:
RequestOptions options = new RequestOptions()
.placeholder(R.drawable.img_default)// Display the image before the image is loaded
.fallback( R.drawable.img_blank) // The image to display when the URL is empty
.error(drawable.img_load_failure);// The image is displayed after the image fails to load
Glide.with(this)
.load(URL) // Image address
.apply(options)
.into(ImagView);
Copy the code
Other relevant knowledge:
Glide4 sets the image rounded corners
2.1. The first method:
RequestOptions options = new RequestOptions(). error(R.drawable.img_load_failure). bitmapTransform(new RoundedCorners(30)); With (this).load(URL) // Image address.apply(options).into(ImagView);Copy the code
2.2. The second way:
RequestOptions requestOptions = new RequestOptions(); requestOptions.placeholder(R.drawable.ic_launcher_background); requestOptions.circleCropTransform(); requestOptions.transforms( new RoundedCorners(30)); Glide. With (this).load(URL) // Image address.apply(options).into(ImagView);Copy the code
2.3. The third way:
RequestOptions options = new RequestOptions() .centerCrop() .transform(new RoundTransform(this,30)); Glide. With (this).load(URL) // Image address.apply(options).into(ImagView);Copy the code
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) {
}
}
Copy the code