工具类之 ImageLoader(基于 Glide)
/**
类描述:图片处理工具类的封装(基于 Glide)
*/
public class ImageLoader {
private Context mContext;
public static final String ANDROID_RESOURCE = "android.resource://";
public static final String FOREWARD_SLASH = "/";
public ImageLoader(Context context) {
this.mContext = context;
}
// 将资源 ID 转为 Uri
public Uri resourceIdToUri(int resourceId) {
return Uri.parse(ANDROID_RESOURCE + mContext.getPackageName() + FOREWARD_SLASH + resourceId);
}
// 加载访问地址的图片
public void loadUrlImage(String url, int loadResId, ImageView imageView) {
with(mContext)
.load(url)
.placeholder(loadResId)
.error(loadResId)
.crossFade()
.into(imageView);
}
// 加载 drawable 图片
public void loadResImage(int resId, int loadResId, ImageView imageView) {
with(mContext)
.load(resourceIdToUri(resId))
.placeholder(loadResId)
.error(loadResId)
.crossFade()
.into(imageView);
}
// 加载本地图片
public void loadLocalImage(String path, int loadResId, ImageView imageView) {
with(mContext)
.load("file://" + path)
.placeholde
r(loadResId)
.error(loadResId)
.crossFade()
.into(imageView);
}
/****************************
圆形图片
***************************************/
// 加载有访问地址的圆型图片
public void loadCircleImage(String url, int loadResId, ImageView imageView) {
with(mContext)
.load(url)
.placeholder(loadResId)
.error(loadResId)
.crossFade()
.transform(new GlideCircleTransform(mContext))
.into(imageView);
}
// 加载 drawable 圆型图片
public void loadCircleResImage(int resId, int loadResId, ImageView imageView) {
with(mContext)
.load(resourceIdToUri(resId))
.placeholder(loadResId)
.error(loadResId)
.crossFade()
.transform(new GlideCircleTransform(mContext))
.into(imageView);
}
// 加载本地圆型图片
public void loadCircleLocalImage(String path, int loadResId, ImageView imageView) {
with(mContext)
.load("file://" + path)
.placeholder(loadResId)
.error(loadResId)
.crossFade()
.transform(new GlideCircleTransform(mContext))
.into(imageView);
}
/****************************
圆角型图片
***************************************/
// 加载有访问地址的圆角型图片
public void loadRoundImage(String url, int loadResId, ImageView imageView) {
with(mContext)
.load(url)
.placeholder(loadResId)
.error(loadResId)
.crossFade()
.transform(new GlideRoundTransform(mContext,
DisplayUtil.dip2px(mContext, 2.0f)))
.into(imageView);
}
// 加载 drawable 圆角型图片
public void loadRoundResImage(int resId, int loadResId, ImageView imageView) {
with(mContext)
.load(resourceIdToUri(resId))
.placeholder(loadResId)
.error(loadResId)
.crossFade()
.transform(new GlideRoundTransform(mContext,
DisplayUtil.dip2px(mContext, 2.0f)))
.into(imageView);
}
// 加载本地圆角型图片
public void loadRoundLocalImage(String path, int loadResId, ImageView imageView) {
with(mContext)
.load("file://" + path)
.placeholder(loadResId)
.error(loadResId)
.crossFade()
.transform(new GlideRoundTransform(mContext, DisplayUtil.dip2px(mContext, 2.0f)))
.into(imageView);
}
}
3、GlideCircleTransform
======================
/**
类描述:基于 Glide 图片圆型变换类
*/
public class GlideCircleTransform extends BitmapTransformation {
public GlideCircleTransform(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;
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;
}
评论