package com.xw.common.utils;
|
|
import android.content.Context;
|
import android.widget.ImageView;
|
|
import androidx.annotation.IdRes;
|
import androidx.annotation.NonNull;
|
|
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
|
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
|
import com.luck.picture.lib.engine.ImageEngine;
|
import com.luck.picture.lib.utils.ActivityCompatHelper;
|
import com.xw.common.R;
|
|
public class GlideEngine implements ImageEngine {
|
|
/**
|
* 加载图片
|
*
|
* @param context 上下文
|
* @param url 资源url
|
* @param imageView 图片承载控件
|
*/
|
@Override
|
public void loadImage(Context context, String url, ImageView imageView) {
|
if (!ActivityCompatHelper.assertValidRequest(context)) {
|
return;
|
}
|
Glide.with(context)
|
.load(url)
|
.placeholder(R.mipmap.ic_loading)
|
.error(R.mipmap.ic_load_fail).into(imageView);
|
}
|
|
@Override
|
public void loadImage(Context context, ImageView imageView, String url, int maxWidth, int maxHeight) {
|
if (!ActivityCompatHelper.assertValidRequest(context)) {
|
return;
|
}
|
Glide.with(context)
|
.load(url)
|
.override(maxWidth, maxHeight)
|
.placeholder(R.mipmap.ic_loading)
|
.error(R.mipmap.ic_load_fail).into(imageView);
|
}
|
|
/**
|
* 加载相册目录封面
|
*
|
* @param context 上下文
|
* @param url 图片路径
|
* @param imageView 承载图片ImageView
|
*/
|
@Override
|
public void loadAlbumCover(Context context, String url, ImageView imageView) {
|
if (!ActivityCompatHelper.assertValidRequest(context)) {
|
return;
|
}
|
Glide.with(context)
|
.asBitmap()
|
.load(url)
|
.override(180, 180)
|
.sizeMultiplier(0.5f)
|
.transform(new CenterCrop(), new RoundedCorners(8))
|
.placeholder(R.mipmap.ic_loading)
|
.error(R.mipmap.ic_load_fail).into(imageView);
|
}
|
|
|
/**
|
* 加载图片列表图片
|
*
|
* @param context 上下文
|
* @param url 图片路径
|
* @param imageView 承载图片ImageView
|
*/
|
@Override
|
public void loadGridImage(Context context, String url, ImageView imageView) {
|
if (!ActivityCompatHelper.assertValidRequest(context)) {
|
return;
|
}
|
Glide.with(context)
|
.load(url)
|
.override(200, 200)
|
.centerCrop()
|
.placeholder(R.mipmap.ic_loading)
|
.error(R.mipmap.ic_load_fail).into(imageView);
|
}
|
|
@Override
|
public void pauseRequests(Context context) {
|
if (!ActivityCompatHelper.assertValidRequest(context)) {
|
return;
|
}
|
Glide.with(context).pauseRequests();
|
}
|
|
@Override
|
public void resumeRequests(Context context) {
|
if (!ActivityCompatHelper.assertValidRequest(context)) {
|
return;
|
}
|
Glide.with(context).resumeRequests();
|
}
|
|
/**
|
* 加载图片
|
*
|
* @param imageView 图片控件
|
* @param url 链接
|
* @param error 加载异常占位图资源id
|
* @param placeHolder 占位图资源id
|
* @param radius 圆角 单位dp
|
*/
|
public void load(@NonNull ImageView imageView,
|
String url,
|
@IdRes int error, @IdRes int placeHolder,
|
float radius) {
|
float value = radius * imageView.getContext().getResources().getDisplayMetrics().density;
|
Glide.with(imageView)
|
.load(url)
|
.transform(new CenterCrop(), new RoundedCorners((int) value))
|
.placeholder(placeHolder)
|
.error(error)
|
.into(imageView);
|
}
|
|
private GlideEngine() {
|
}
|
|
private static final class InstanceHolder {
|
static final GlideEngine instance = new GlideEngine();
|
}
|
|
public static GlideEngine createGlideEngine() {
|
return InstanceHolder.instance;
|
}
|
}
|