Glide使用 加载网络图片 圆形图片 圆角图片

时间:2021-08-05 09:24:05

在项目中总结的Glide用法(工具类)。

public class Glides {

public static Glides instance = new Glides();

public Glides() {
}

public static Glides getInstance() {
return instance;
}

// 加载网络图片
public void load(Context context, String url, ImageView imageView, GlideDrawableImageViewTarget target) {

Glide.with(context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.color.abc_tab_text_normal)
.error(R.color.abc_tab_text_normal)
.crossFade()
.into(target);
}

// 加载网络图片
public void load(Context context, Uri uri, ImageView imageView, GlideDrawableImageViewTarget target) {

Glide.with(context)
.load(uri)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.color.abc_tab_text_normal)
.error(R.color.abc_tab_text_normal)
.crossFade()
.into(target);
}

// 加载网络图片
public void load(Context context, String url, ImageView imageView) {
Glide.with(context)
.load(url)
.placeholder(R.color.abc_tab_text_normal)
.error(R.color.abc_tab_text_normal)
.crossFade()
.into(imageView);
}

//加载网络或者本地图片
public void load(Context context, Uri uri, ImageView imageView, int defaultImg) {
Glide.with(context)
.load(uri)
.placeholder(defaultImg)
.error(defaultImg)
.crossFade()
.centerCrop()
.into(imageView);
}

// 加载网络图片
public void load(Context context, String url, ImageView imageView, int defaultImg) {
Glide.with(context)
.load(url)
.placeholder(defaultImg)
.error(defaultImg)
.crossFade()
.centerCrop()
.into(imageView);
}

// 加载网络图片
public void loadFitCenter(Context context, String url, ImageView imageView, int defaultImg, int width, int height) {
Glide.with(context)
.load(url)
.thumbnail(0.8f)
.override(width, height)
.dontAnimate()
.placeholder(defaultImg)
.error(defaultImg)
.fitCenter()
.into(imageView);
}

// 加载网络图片
public void loadShade(Context context, String url, ImageView imageView, int defaultImg) {
Glide.with(context)
.load(url)
.placeholder(defaultImg)
.error(defaultImg)
.crossFade()
.bitmapTransform(new ShadeTransformation(context))
.into(imageView);
}

// 加载本地引导图片
public void loadGuide(Context context, int resId, ImageView imageView, int defaultImg) {
Glide.with(context)
.load(resId)
.dontAnimate()
.bitmapTransform(new CropTopTransformation(context))
.into(imageView);
}

// 加载本地引导图片
public void loadGuide(Context context, int resId, ImageView imageView, Transformation transformation) {
Glide.with(context)
.load(resId)
.dontAnimate()
.bitmapTransform(transformation)
.into(imageView);
}

// 加载圆型网络图片
public void loadCircle(Context context, String url, ImageView imageView, int defaultImg) {
Glide.with(context)
.load(url)
.placeholder(defaultImg)
.error(defaultImg)
.crossFade()
.bitmapTransform(new CropCircleTransformation(context))
.into(imageView);
}

public void loadCircleDefault(Context context, ImageView imageView, int defaultImg) {
Glide.with(context)
.load(defaultImg)
.placeholder(defaultImg)
.error(defaultImg)
.crossFade()
.bitmapTransform(new CropCircleTransformation(context))
.into(imageView);
}

public void loadCircle(Context context, Uri uri, ImageView imageView, int defaultImg) {
Glide.with(context)
.load(uri)
.placeholder(defaultImg)
.error(defaultImg)
.bitmapTransform(new CropCircleTransformation(context))
.crossFade()
.into(imageView);
}

/**
* 圆角图片
*
* @param context
* @param url
* @param imageView
* @param defaultImg
*/

public void loadRound(Context context, String url, ImageView imageView, int defaultImg) {
Glide.with(context)
.load(url)
.placeholder(defaultImg)
.error(defaultImg)
.transform(new CenterCrop(context), new GlideRoundTransform(context, 15))
.crossFade()
.into(imageView);
}

// 加载圆角网络图片
public void loadRoundCorner(Context context, String url, ImageView imageView, int radius) {
Glide.with(context)
.load(url)
.transform(new CenterCrop(context), new GlideRoundTransform(context, radius))
.placeholder(R.color.common_image_default)
.error(R.color.common_image_default)
// .crossFade()
.into(imageView);
}

// 加载圆角网络图片
public void loadRound(Context context, String url, ImageView imageView, int radius, int defaultImg) {
Glide.with(context)
.load(url)
.placeholder(defaultImg)
.error(defaultImg)
.transform(new CenterCrop(context), new GlideRoundTransform(context, radius))
.crossFade()
.into(imageView);
}

// 加载圆角本地图片
public void loadRoundCorner(Context context, int resId, ImageView imageView, int radius) {
Glide.with(context)
.load(resId)
.transform(new CenterCrop(context), new GlideRoundTransform(context, radius))
.into(imageView);
}


// 加载本地图片
public void load(Context context, int resid, ImageView imageView) {
Glide.with(context)
.load(resid)
.placeholder(R.color.abc_tab_text_normal)
.error(R.color.abc_tab_text_normal)
.crossFade()
.into(imageView);
}

// 加载本地图片
public void loadDecor(Context context, String url, ImageView imageView, int errresid, int width, int height) {
Glide.with(context)
.load(url)
.asBitmap()
.format(DecodeFormat.PREFER_RGB_565)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.priority(Priority.IMMEDIATE)
.dontAnimate()
.thumbnail(0.1f)
.override(width, height)
.into(imageView);
}

// 加载天空网络图片,不用设高
public void loadDecor(Context context, String url, ImageView imageView, int errresid) {
Glide.with(context)
.load(url)
.asBitmap()
.format(DecodeFormat.PREFER_RGB_565)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.priority(Priority.IMMEDIATE)
.dontAnimate()
.into(imageView);
}


// 加载本地图片,错误或占位用透明
public void loadDecor(Context context, int resid, ImageView imageView, int errresid, int width, int height) {
Glide.with(context)
.load(resid)
.asBitmap()
.format(DecodeFormat.PREFER_RGB_565)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.priority(Priority.IMMEDIATE)
.dontAnimate()
.thumbnail(0.1f)
.override(width, height)
.into(imageView);
}


// 加载天空图片,错误或占位用透明
public void loadDecor(Context context, int resid, ImageView imageView, int errresid) {
Glide.with(context)
.load(resid)
.asBitmap()
.format(DecodeFormat.PREFER_RGB_565)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.priority(Priority.IMMEDIATE)
.dontAnimate()
.into(imageView);
}

public void loadNoAnim(Context context, String uri, ImageView imageView, int placeholder) {

Glide.with(context)
.load(Uri.parse(uri))
.placeholder(placeholder)
.error(placeholder)
.dontAnimate()//color filter error
.into(imageView);
}

// 加载圆型本地图片
public void loadCircle(Context context, int resId, ImageView imageView) {
Glide.with(context)
.load(resId)
.placeholder(R.color.trans)
.error(R.color.trans)
.transform(new GlideCircleTransform(context))
.crossFade()
.into(imageView);
}

// 加载圆型网络图片
public void loadCircle(Context context, String url, ImageView imageView) {
Glide.with(context)
.load(url)
.placeholder(R.color.trans)
.error(R.color.trans)
.transform(new GlideCircleTransform(context))
.crossFade()
.into(imageView);
}

// 加载网络图片动画
public void loadAnima(Context context, String url, Animation animation, ImageView imageView) {
Glide.with(context)
.load(url)
.placeholder(R.color.abc_tab_text_normal)
.error(R.color.abc_tab_text_normal)
.animate(animation)
.crossFade()
.into(imageView);
}

// 加载网络图片动画
public void loadAnima(Context context, String url, int animationId, ImageView imageView) {
Glide.with(context)
.load(url)
.placeholder(R.color.abc_tab_text_normal)
.error(R.color.abc_tab_text_normal)
.animate(animationId)
.crossFade()
.into(imageView);
}

// 加载本地图片动画
public void loadAnima(Context context, int resId, Animation animation, ImageView imageView) {
Glide.with(context)
.load(resId)
.placeholder(R.color.abc_tab_text_normal)
.error(R.color.abc_tab_text_normal)
.animate(animation)
.crossFade()
.into(imageView);
}


// 加载drawable图片
public void loadAnima(Context context, int resId, int animationId, ImageView imageView) {
Glide.with(context)
.load(resId)
.placeholder(R.color.abc_tab_text_normal)
.error(R.color.abc_tab_text_normal)
.animate(animationId)
.crossFade()
.into(imageView);
}


}
public class GlideRoundTransform extends BitmapTransformation {

private static float radius = 0f;

public GlideRoundTransform(Context context) {
this(context, 6);
}

public GlideRoundTransform(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) {
return roundCrop(pool, toTransform);
}

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;
}

@Override public String getId() {
return getClass().getName() + Math.round(radius);
}
}
public class CropTopTransformation implements Transformation<Bitmap> {

private Context mContext;
private BitmapPool mBitmapPool;
private int mWidth;
private int mHeight;

private CropTransformation.CropType mCropType = CropTransformation.CropType.CENTER;

public CropTopTransformation(Context context) {
this(Glide.get(context).getBitmapPool());
mContext = context;
}

public CropTopTransformation(BitmapPool pool) {
this.mBitmapPool = pool;
}

@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {


Bitmap source = resource.get();
float sWidth = (float) source.getWidth();
float sHeight = (float) source.getHeight();

float wScale = outWidth / sWidth;

float calculateHeight = wScale * sHeight + 0.5f;

Bitmap fitBitmap = TransformationUtils.fitCenter(source, mBitmapPool, outWidth, (int) calculateHeight);

int fWidth = fitBitmap.getWidth();
int fHeight = fitBitmap.getHeight();

Bitmap bitmap = mBitmapPool.get(outWidth, outHeight, Bitmap.Config.ARGB_8888);
if (bitmap == null) {
bitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
}

Matrix matrix = new Matrix();
if (fHeight > outHeight) {
matrix.setTranslate(0, fHeight - outHeight);
} else {
matrix.setTranslate(0, outHeight - fHeight);
}

Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();

paint.setAntiAlias(true);
paint.setDither(true);

canvas.drawBitmap(fitBitmap, matrix, paint);

return BitmapResource.obtain(bitmap, mBitmapPool);
}

@Override
public String getId() {
return "CropCircleTransformation()";
}

private float getTop(float scaledHeight) {
switch (mCropType) {
case TOP:
return 0;
case CENTER:
return (mHeight - scaledHeight) / 2;
case BOTTOM:
return mHeight - scaledHeight;
default:
return 0;
}
}
}
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;
}

@Override
public String getId() {
return getClass().getName();
}
}
public class ShadeTransformation implements Transformation<Bitmap> {

private Context mContext;
private BitmapPool mBitmapPool;
private int mWidth;
private int mHeight;

private CropTransformation.CropType mCropType = CropTransformation.CropType.CENTER;

public ShadeTransformation(Context context) {
this(Glide.get(context).getBitmapPool());
mContext = context;
}

public ShadeTransformation(BitmapPool pool) {
this.mBitmapPool = pool;
}

@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {


Bitmap source = resource.get();

Bitmap bitmap = mBitmapPool.get(outWidth, outHeight, Bitmap.Config.ARGB_8888);
if (bitmap == null) {
bitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);
}

Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();

paint.setAntiAlias(true);
paint.setDither(true);

canvas.drawBitmap(TransformationUtils.centerCrop(bitmap, source, outWidth, outHeight), null, new Rect(0, 0, outWidth, outHeight), paint);

GradientDrawable drawable =
new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{Color.parseColor("#80000000"), Color.parseColor("#00000000")});

drawable.setBounds(0, 0, outWidth, outHeight);

drawable.draw(canvas);

return BitmapResource.obtain(bitmap, mBitmapPool);
}

@Override
public String getId() {
return "CropCircleTransformation()";
}

private float getTop(float scaledHeight) {
switch (mCropType) {
case TOP:
return 0;
case CENTER:
return (mHeight - scaledHeight) / 2;
case BOTTOM:
return mHeight - scaledHeight;
default:
return 0;
}
}
}