Glide使用
程序员文章站
2022-06-05 12:37:09
...
首先建立一个GlideUtil的工具类,建立
GlideWithLoad的图片Builder,用
GlideWithLoad
替换原来的Glide加载:Glide
.with(context)
.load(imageUrl)
.into(imageView);
改为如下,有正常的图片加载,圆角图片加载,圆形图加载,高斯模糊加载,
public class GlideUtil { private static final String DRAWABLE = "drawable://"; private static final String FILE = "file://"; public static DrawableTypeRequest GlideWithLoad(String path) { RequestManager requestManager = Glide.with(MyApplication.sContext); DrawableTypeRequest request; if (path.startsWith(DRAWABLE)) { int resourceId = Integer.parseInt(path.substring(DRAWABLE.length())); request = requestManager.load(resourceId); } else if (path.startsWith(FILE)) { String file = path.substring(FILE.length()); request = requestManager.load(file); } else { request = requestManager.load(path); } return request; }
}
/** * 显示圆角的图片 * * @param imageView 显示图片的View * @param cornerRadiusPixels 圆角半径像素 * @param imageUrl 图片的地址 */ public static void display(ImageView imageView, int cornerRadiusPixels, String imageUrl) { GlideWithLoad(imageUrl) .placeholder(R.drawable.bg_loading) .error(R.drawable.bg_loading) .crossFade(500) .bitmapTransform(new CenterCrop(MyApplication.sContext), new RoundedCornersTransformation(MyApplication.sContext, cornerRadiusPixels, 1)) .into(imageView); } /** * 显示圆形的图片 * * @param imageView 显示图片的View * @param imageUrl 图片的地址 */ public static void displayCircle(ImageView imageView, String imageUrl) { GlideWithLoad(imageUrl) .placeholder(R.drawable.bg_circle_loading) .error(R.drawable.bg_circle_loading) .bitmapTransform(new CropCircleTransformation(MyApplication.sContext)) .crossFade(500) .into(imageView); } /** * 加载高斯模糊图 * * @param blur 设置模糊度(在0.0到25.0之间)默认”25" * @param zoom 图片缩放比例,默认“1”。 */ public static void display(ImageView imageView, String imageUrl, int blur, int zoom) { GlideWithLoad(imageUrl) .placeholder(R.drawable.bg_circle_loading) .error(R.drawable.bg_circle_loading) .crossFade(500) .bitmapTransform(new BlurTransformation(MyApplication.sContext, blur, zoom)) .into(imageView); }
//加载本地图片,第三个本地路径图片,第二参数是圆角弧度,第一个是图片控件对象