Glide工具入口
public class ImageLoadUtils {
private volatile static ImageLoadUtils sInstance;
private ImageLoadUtils() {
}
public static ImageLoadUtils getInstance() {
if (null == sInstance) {
synchronized (ImageLoadUtils.class) {
if (null == sInstance) {
sInstance = new ImageLoadUtils();
}
}
}
return sInstance;
}
/**
* 加載圖片
* @param view
* @param url
*/
public void loadImage(ImageView view, String url){
loadImage(view, url, 0);
}
/**
* 加載圖片(帶默認圖)
* @param view
* @param url
* @param defaultRes
*/
public void loadImage(ImageView view, String url, @DrawableRes int defaultRes){
loadImage(view, url, defaultRes, 0);
}
/**
* 加載圖片(帶圓角)
* @param radius
* @param view
* @param url
*/
public void loadImage(int radius, ImageView view, String url){
loadImage(view, url, 0, radius);
}
/**
* 加載圖片(帶默認圖和圓角)
* @param view
* @param url
* @param defaultRes
* @param radius
*/
public void loadImage(ImageView view, String url, @DrawableRes int defaultRes, int radius){
RequestOptions options = new RequestOptions();
Transformation transformation = null;
if(radius > 0 ){
transformation = new MultiTransformation(
new CenterCrop(),
new RoundedCorners(radius)
);
}else{
transformation = new CenterCrop();
}
options.transform(transformation);
options.error(defaultRes);
options.placeholder(defaultRes);
Glide.with(view).load(url)
.apply(options)
.into(view);
}
/**
* 加載圓形圖片
* @param view
* @param url
*/
public void loadCircle(ImageView view, String url){
loadCircle(view, url, 0);
}
/**
* 加載圓形圖片(帶默認圖)
* @param view
* @param url
* @param defaultRes
*/
public void loadCircle(ImageView view, String url, @DrawableRes int defaultRes){
RequestOptions options = RequestOptions.centerCropTransform()
.placeholder(defaultRes)
.error(defaultRes)
.circleCrop();
Glide.with(view).load(url).apply(options).into(view);
}
/**
* 加載圓形圖片(帶邊框)
* @param view
* @param url
* @param borderWidth
* @param borderColor
*/
public void loadCircleWithBorder(ImageView view,String url,int borderWidth,int borderColor) {
loadCircleWithBorder(view, url, 0, borderWidth, borderColor);
}
/**
* 加載圓形圖片(帶默認圖和邊框)
* @param view
* @param url
* @param defaultRes
* @param borderWidth
* @param borderColor
*/
public void loadCircleWithBorder(ImageView view, String url, @DrawableRes int defaultRes, int borderWidth, int borderColor){
RequestOptions options = RequestOptions
.bitmapTransform(new GlideCircleWithBorder(borderWidth,borderColor))
.placeholder(defaultRes)
.error(defaultRes);
Glide.with(view).load(url).apply(options).into(view);
}
/**
* 加載圓角和高斯模糊
* @param view
* @param url
*/
public void loadBlurImage(ImageView view, String url){
loadBlurImage(view,url,15,1);
}
/**
* 加載圓角和高斯模糊
* @param view
* @param url
* @param radius
* @param sampling
*/
public void loadBlurImage(ImageView view, String url, int radius, int sampling){
loadBlurImage(view,url,0,radius,sampling);
}
/**
* 加載圓角和高斯模糊
* @param view
* @param url
* @param cornRadius
* @param sampling
*/
public void loadBlurImage(ImageView view, String url, int cornRadius, int radius, int sampling){
if(radius == 0){
radius = 15;
}
if(sampling == 0){
sampling = 1;
}
Transformation transformation = null;
if(cornRadius > 0 ){
transformation = new MultiTransformation(
new BlurTransformation(radius, sampling),
new RoundedCorners(cornRadius)
);
}else{
transformation = new BlurTransformation(radius,sampling);
}
RequestOptions options = RequestOptions.bitmapTransform(transformation);
Glide.with(view)
.load(url)
.apply(options)
.into(view);
}
/**
* 加載圓形高斯模糊
* @param view
* @param url
*/
public void loadBlurCircleImage(ImageView view, String url){
loadBlurCircleImage(view,url,15,1);
}
/**
* 加載圓形高斯模糊
* @param view
* @param url
* @param radius
* @param sampling
*/
public void loadBlurCircleImage(ImageView view, String url, int radius, int sampling){
if(radius == 0){
radius = 15;
}
if(sampling == 0){
sampling = 1;
}
MultiTransformation transformation = new MultiTransformation(
new BlurTransformation(radius, sampling),
new CircleCrop());
RequestOptions options = RequestOptions.bitmapTransform(transformation);
Glide.with(view)
.load(url)
.apply(options)
.into(view);
}
}
複製代碼
Glide圓形邊框類
public class GlideCircleWithBorder extends BitmapTransformation {
private Paint mBorderPaint;
private float mBorderWidth;
public GlideCircleWithBorder(int borderWidth, int borderColor) {
super();
mBorderWidth = Resources.getSystem().getDisplayMetrics().density * borderWidth;
mBorderPaint = new Paint();
mBorderPaint.setDither(true);
mBorderPaint.setAntiAlias(true);
mBorderPaint.setColor(borderColor);
mBorderPaint.setStyle(Paint.Style.STROKE);
mBorderPaint.setStrokeWidth(mBorderWidth);
}
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
private Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) {
return null;
}
int size = (int) (Math.min(source.getWidth(), source.getHeight()) - (mBorderWidth / 2));
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);
if (mBorderPaint != null) {
float borderRadius = r - mBorderWidth / 2;
canvas.drawCircle(r, r, borderRadius, mBorderPaint);
}
return result;
}
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
}
}
複製代碼
Glide 高斯模式
public class BlurTransformation extends BitmapTransformation {
private static final int VERSION = 1;
private static final String ID =
"com.xuetian.common.util.image.blur.BlurTransformation." + VERSION;
private static int MAX_RADIUS = 25;
private static int DEFAULT_DOWN_SAMPLING = 1;
private int radius;
private int sampling;
public BlurTransformation() {
this(MAX_RADIUS, DEFAULT_DOWN_SAMPLING);
}
public BlurTransformation(int radius) {
this(radius, DEFAULT_DOWN_SAMPLING);
}
public BlurTransformation(int radius, int sampling) {
this.radius = radius;
this.sampling = sampling;
}
@Override
protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
@NonNull Bitmap toTransform, int outWidth, int outHeight) {
int width = toTransform.getWidth();
int height = toTransform.getHeight();
int scaledWidth = width / sampling;
int scaledHeight = height / sampling;
Bitmap bitmap = pool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
setCanvasBitmapDensity(toTransform, bitmap);
Canvas canvas = new Canvas(bitmap);
canvas.scale(1 / (float) sampling, 1 / (float) sampling);
Paint paint = new Paint();
paint.setFlags(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(toTransform, 0, 0, paint);
bitmap = FastBlur.blur(bitmap, radius, true);
return bitmap;
}
@Override
public String toString() {
return "BlurTransformation(radius=" + radius + ", sampling=" + sampling + ")";
}
@Override
public boolean equals(Object o) {
return o instanceof BlurTransformation &&
((BlurTransformation) o).radius == radius &&
((BlurTransformation) o).sampling == sampling;
}
@Override
public int hashCode() {
return ID.hashCode() + radius * 1000 + sampling * 10;
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update((ID + radius + sampling).getBytes(CHARSET));
}
}
複製代碼