import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.Display;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.excellent.downloadutil.BuildConfig;
import com.excellent.downloadutil.R;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.MessageDigest;
public class GlideUtils {
public static RequestListener mListener;
// private static RequestOptions mOptions;
public static final int placeholderSoWhite = R.mipmap.ic_launcher;
public static final int errorSoWhite = R.mipmap.ic_launcher;
public static RequestBuilder settingError(RequestBuilder builder) {
return settingError(builder, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
}
public static RequestBuilder settingError(RequestBuilder builder, @DrawableRes int img) {
return settingError(builder, img, R.mipmap.ic_launcher);
}
public static RequestBuilder settingError(RequestBuilder builder, @DrawableRes int img, @DrawableRes int holder) {
if (BuildConfig.DEBUG) {
if (mListener == null) { //防止创建多个,造成内存泄漏 ,捕获图片加载异常
mListener = new RequestListener() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
if (e != null) {
e.printStackTrace();
}
return false;
}
@Override
public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
return false;
}
};
}
builder.listener(mListener);
}
// if (mOptions == null) {
// mOptions = new RequestOptions();
// mOptions.diskCacheStrategy(DiskCacheStrategy.DATA) //默认缓存处理过的图片,看产品设计,如果同一张图片,有不同的尺寸显示,打开该方法
//// .placeholder(R.drawable.ic_product_default)
// .error(img); //图片拉伸
//// .error(R.drawable.ic_product_default).dontAnimate(); //图片不拉伸
// }
return builder.apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.DATA).error(errorSoWhite).placeholder(errorSoWhite).disallowHardwareConfig());
}
/**
* Glide只播放一次Gif以及监听播放完成的实现
*
* @param context
* @param model
* @param imageView
* @param gifListener
*/
public static void loadOneTimeGif(Context context, Object model, final ImageView imageView, final GifListener gifListener) {
Glide.with(context).asGif().load(model).listener(new RequestListener<GifDrawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
try {
Field gifStateField = GifDrawable.class.getDeclaredField("state");
gifStateField.setAccessible(true);
Class gifStateClass = Class.forName("com.bumptech.glide.load.resource.gif.GifDrawable$GifState");
Field gifFrameLoaderField = gifStateClass.getDeclaredField("frameLoader");
gifFrameLoaderField.setAccessible(true);
Class gifFrameLoaderClass = Class.forName("com.bumptech.glide.load.resource.gif.GifFrameLoader");
Field gifDecoderField = gifFrameLoaderClass.getDeclaredField("gifDecoder");
gifDecoderField.setAccessible(true);
Class gifDecoderClass = Class.forName("com.bumptech.glide.gifdecoder.GifDecoder");
Object gifDecoder = gifDecoderField.get(gifFrameLoaderField.get(gifStateField.get(resource)));
Method getDelayMethod = gifDecoderClass.getDeclaredMethod("getDelay", int.class);
getDelayMethod.setAccessible(true);
//设置只播放一次
resource.setLoopCount(1);
//获得总帧数
int count = resource.getFrameCount();
int delay = 0;
for (int i = 0; i < count; i++) {
//计算每一帧所需要的时间进行累加
delay += (int) getDelayMethod.invoke(gifDecoder, i);
}
imageView.postDelayed(new Runnable() {
@Override
public void run() {
if (gifListener != null) {
gifListener.gifPlayComplete();
}
}
}, delay);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return false;
}
}).into(imageView);
}
/**
* Gif播放完毕回调
*/
public interface GifListener {
void gifPlayComplete();
}
/*
*加载图片(默认)
*/
public static void loadImage(Context context, String url, ImageView imageView) {
RequestOptions options = new RequestOptions().centerCrop().placeholder(placeholderSoWhite) //占位图
.error(errorSoWhite) //错误图
// .priority(Priority.HIGH)
.diskCacheStrategy(DiskCacheStrategy.ALL);
Glide.with(context).load(url).apply(options).into(imageView);
}
public static void loadImageFitXY(Context context, String url, ImageView imageView, int defaultPic) {
RequestOptions options = new RequestOptions()
// .centerCrop()
.placeholder(defaultPic) //占位图
.error(defaultPic) //错误图
// .priority(Priority.HIGH)
.diskCacheStrategy(DiskCacheStrategy.ALL);
Glide.with(context).load(url).apply(options).into(imageView);
}
/**
* 自适应宽度加载图片。保持图片的长宽比例不变,通过修改imageView的高度来完全显示图片。
*/
public static void loadIntoUseFitWidth(Context context, final String url, final ImageView imageView) {
Glide.with(context).asBitmap().load(url).placeholder(placeholderSoWhite) //占位图
.error(placeholderSoWhite) //错误图
.into(new PhotoViewImageViewTarget(imageView, getScreenWidth(context)));
}
public static void loadIntoUseFitWidth(Context context, final String url, final ImageView imageView, int screenWidth) {
Glide.with(context).asBitmap().load(url).placeholder(placeholderSoWhite) //占位图
.error(placeholderSoWhite) //错误图
.into(new PhotoViewImageViewTarget(imageView, screenWidth));
}
/**
* 预加载图片
*
* @param context
* @param imageUrl
*/
public static void preLoad(Context context, final String imageUrl) {
if (TextUtils.isEmpty(imageUrl)) {
return;
}
try {
Glide.with(context).load(imageUrl).preload();
} catch (IllegalArgumentException e) {
LogUtil.e(e.getMessage());
}
}
/*
*加载图片(默认)
*/
public static void loadImage(Context context, String url, ImageView imageView, int placeholder) {
RequestOptions options = new RequestOptions().centerCrop().placeholder(placeholder) //占位图
.error(placeholder) //错误图
// .priority(Priority.HIGH)
.diskCacheStrategy(DiskCacheStrategy.ALL);
Glide.with(context).load(url).apply(options).into(imageView);
}
/*
*加载图片(默认)
*/
public static void loadImage(Context context, int rid, ImageView imageView) {
RequestOptions options = new RequestOptions().centerCrop().placeholder(placeholderSoWhite) //占位图
.error(errorSoWhite) //错误图
// .priority(Priority.HIGH)
.diskCacheStrategy(DiskCacheStrategy.ALL);
Glide.with(context).load(rid).apply(options).into(imageView);
}
/**
* 指定图片大小;使用override()方法指定了一个图片的尺寸。
* Glide现在只会将图片加载成width*height像素的尺寸,而不会管你的ImageView的大小是多少了。
* 如果你想加载一张图片的原始尺寸的话,可以使用Target.SIZE_ORIGINAL关键字----override(Target.SIZE_ORIGINAL)
*
* @param context
* @param url
* @param imageView
* @param width
* @param height
*/
public static void loadImageSize(Context context, String url, ImageView imageView, int width, int height) {
RequestOptions options = new RequestOptions().centerCrop().placeholder(placeholderSoWhite) //占位图
.error(errorSoWhite) //错误图
.override(width, height)
// .priority(Priority.HIGH)
.diskCacheStrategy(DiskCacheStrategy.ALL);
Glide.with(context).load(url).apply(options).into(imageView);
}
/**
* 禁用内存缓存功能
* diskCacheStrategy()方法基本上就是Glide硬盘缓存功能的一切,它可以接收五种参数:
* <p>
* DiskCacheStrategy.NONE: 表示不缓存任何内容。
* DiskCacheStrategy.DATA: 表示只缓存原始图片。
* DiskCacheStrategy.RESOURCE: 表示只缓存转换过后的图片。
* DiskCacheStrategy.ALL : 表示既缓存原始图片,也缓存转换过后的图片。
* DiskCacheStrategy.AUTOMATIC: 表示让Glide根据图片资源智能地选择使用哪一种缓存策略(默认选项)。
*/
public static void loadImageSizekipMemoryCache(Context context, String url, ImageView imageView) {
RequestOptions options = new RequestOptions().placeholder(placeholderSoWhite) //占位图
.error(errorSoWhite) //错误图S
.skipMemoryCache(true)//禁用掉Glide的内存缓存功能
.diskCacheStrategy(DiskCacheStrategy.ALL);
Glide.with(context).load(url).apply(options).into(imageView);
}
/**
* 加载圆形图片
*/
public static void loadCircleImage(Context context, String url, ImageView imageView) {
RequestOptions options = new RequestOptions().centerCrop().circleCrop()//设置圆形
.placeholder(placeholderSoWhite).error(errorSoWhite)
//.priority(Priority.HIGH)
.diskCacheStrategy(DiskCacheStrategy.ALL);
Glide.with(context).load(url).apply(options).into(imageView);
}
public static void loadCircleImage(Context context, String url, ImageView imageView, int defaultImg) {
RequestOptions options = new RequestOptions().centerCrop().circleCrop()//设置圆形
.placeholder(defaultImg).error(defaultImg)
//.priority(Priority.HIGH)
.diskCacheStrategy(DiskCacheStrategy.ALL);
Glide.with(context).load(url).apply(options).into(imageView);
}
/**
* 加载圆形图片
*/
public static void loadCircleImage(Context context, int url, ImageView imageView) {
RequestOptions options = new RequestOptions().centerCrop().circleCrop()//设置圆形
.placeholder(placeholderSoWhite).error(errorSoWhite)
//.priority(Priority.HIGH)
.diskCacheStrategy(DiskCacheStrategy.ALL);
Glide.with(context).load(url).apply(options).into(imageView);
}
/**
* 预先加载图片
* 在使用图片之前,预先把图片加载到缓存,调用了预加载之后,我们以后想再去加载这张图片就会非常快了,
* 因为Glide会直接从缓存当中去读取图片并显示出来
*/
public static void preloadImage(Context context, String url) {
Glide.with(context).load(url).preload();
}
/**
* Glide.with(this).asGif() //强制指定加载动态图片
* 如果加载的图片不是gif,则asGif()会报错, 当然,asGif()不写也是可以正常加载的。
* 加入了一个asBitmap()方法,这个方法的意思就是说这里只允许加载静态图片,不需要Glide去帮我们自动进行图片格式的判断了。
* 如果你传入的还是一张GIF图的话,Glide会展示这张GIF图的第一帧,而不会去播放它。
*
* @param context
* @param url 例如:https://image.niwoxuexi.com/blog/content/5c0d4b1972-loading.gif
* @param imageView
*/
public static void loadGif(Context context, String url, ImageView imageView) {
RequestOptions options = new RequestOptions().placeholder(placeholderSoWhite).error(errorSoWhite);
Glide.with(context).load(url).apply(options).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
}).into(imageView);
}
public static void loadGif(Context context, int urlID, ImageView imageView) {
RequestOptions options = new RequestOptions().placeholder(placeholderSoWhite).error(errorSoWhite);
Glide.with(context).load(urlID).apply(options).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
}).into(imageView);
}
public static int dip2px(Context context, float dp) {
float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
/**
* 加载圆角图片
*/
public static void loadRoundCircleImage(Context context, String url, ImageView imageView) {
RequestOptions options = new RequestOptions().centerCrop()
// .circleCrop()//设置圆形
.placeholder(placeholderSoWhite).error(errorSoWhite)
//.priority(Priority.HIGH)
.bitmapTransform(new RoundedCornersTransformation(15, 0, RoundedCornersTransformation.CornerType.ALL)).diskCacheStrategy(DiskCacheStrategy.ALL);
Glide.with(context).load(url).apply(options).into(imageView);
}
public static void loadRoundCircleImage(Context context, String url, ImageView imageView, int defalut) {
RequestOptions options = new RequestOptions().centerCrop()
// .circleCrop()//设置圆形
.placeholder(defalut).error(defalut)
//.priority(Priority.HIGH)
.bitmapTransform(new RoundedCornersTransformation(15, 0, RoundedCornersTransformation.CornerType.ALL)).diskCacheStrategy(DiskCacheStrategy.ALL);
Glide.with(context).load(url).apply(options).into(imageView);
}
/**
* 设置圆角的话,xml文件中设置centerCrop属性是不起作用的,要在代码中设置这个属性 GlideRoundTransform
*
* @param context
* @param url
* @param imageView
*/
public static void loadRoundCircleImage2(Context context, String url, ImageView imageView) {
Glide.with(context).load(url).dontAnimate().placeholder(placeholderSoWhite).error(errorSoWhite).transform(new CenterCrop(), new GlideRoundTransform(context, 10)).into(imageView);
}
/**
* 加载圆角图片
*/
public static void loadRoundCircleImage(Context context, int url, ImageView imageView) {
RequestOptions options = new RequestOptions().centerCrop().circleCrop()//设置圆形
.placeholder(placeholderSoWhite).error(errorSoWhite)
//.priority(Priority.HIGH)
.bitmapTransform(new RoundedCornersTransformation(15, 0, RoundedCornersTransformation.CornerType.ALL)).diskCacheStrategy(DiskCacheStrategy.ALL);
Glide.with(context).load(url).apply(options).into(imageView);
}
/**
* 加载圆角图片-指定任意部分圆角(图片上、下、左、右四个角度任意定义)
*
* @param context
* @param url
* @param imageView
* @param type
*/
public static void loadCustRoundCircleImage(Context context, String url, ImageView imageView, RoundedCornersTransformation.CornerType type) {
RequestOptions options = new RequestOptions().centerCrop().placeholder(placeholderSoWhite).error(errorSoWhite)
//.priority(Priority.HIGH)
.bitmapTransform(new RoundedCornersTransformation(45, 0, type)).diskCacheStrategy(DiskCacheStrategy.ALL);
Glide.with(context).load(url).apply(options).into(imageView);
}
public void downloadImage(final Context context, final String url) {
new Thread(new Runnable() {
@Override
public void run() {
try {
//String url = "http://www.guolin.tech/book.png";
FutureTarget<File> target = Glide.with(context).asFile().load(url).submit();
final File imageFile = target.get();
//LogUtil.d("logcat", "下载好的图片文件路径=" + imageFile.getPath());
// runOnUiThread(new Runnable() {
// @Override
// public void run() {
// Toast.makeText(context, imageFile.getPath(), Toast.LENGTH_LONG).show();
// }
// });
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
/**
* @date
* @auther gaoxiaoxiong
* @Descriptiion 根据传入的宽度,放大高度
**/
static class PhotoViewImageViewTarget extends CustomTarget<Bitmap> {
ImageView photoView;
int phoneWidth;
public PhotoViewImageViewTarget(ImageView photoView, int phoneWidth) {
this.photoView = photoView;
this.phoneWidth = phoneWidth;
}
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
if (resource != null) {
//获取原图的宽高
int resourceWidth = resource.getWidth();
int resourceHeight = resource.getHeight();
LogUtil.d("phoneWidth=" + phoneWidth + ";resourceWidth=" + resourceWidth + ";resourceHeight=" + resourceHeight);
ViewGroup.LayoutParams layoutParams = photoView.getLayoutParams();
float scaleWidth = 0.0f;
scaleWidth = (float) phoneWidth / (float) resourceWidth;
layoutParams.width = (int) (resourceWidth * scaleWidth);
layoutParams.height = (int) (resourceHeight * scaleWidth);
LogUtil.d("phoneWidth=" + phoneWidth + ";scaleWidth=" + scaleWidth + "; layoutParams.width=" + layoutParams.width + "; layoutParams.height=" + layoutParams.height);
// if (resourceWidth > 1000){
// layoutParams.width = (int) (resourceWidth * scaleWidth);
// layoutParams.height = (int) (resourceHeight * scaleWidth);
// }else {
// if (resourceWidth > phoneWidth){
// layoutParams.width = (int) (resourceWidth * scaleWidth);
// layoutParams.height = (int) (resourceHeight * scaleWidth);
// }else {
// layoutParams.width = resourceWidth;
// layoutParams.height = resourceHeight;
// }
// }
// int height = resource.getHeight();//原始高度
// layoutParams.height = height;
photoView.setLayoutParams(layoutParams);
photoView.setImageBitmap(resource);
photoView.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
}
/**
* 将图片转化为圆角
* 构造中第二个参数定义半径
*/
public static class GlideRoundTransform extends BitmapTransformation {
private static float radius = 0f;
public GlideRoundTransform(Context context) {
this(context, 10);
}
public GlideRoundTransform(Context context, int dp) {
// super(context);
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;
}
public String getId() {
return getClass().getName() + Math.round(radius);
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
}
}
public static int getScreenWidth(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getRealSize(size);
return size.x;
}
}
RoundedCornersTransformation
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import androidx.annotation.NonNull;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
public class RoundedCornersTransformation extends BitmapTransformation {
private static final String ID = "com.excellent.downloadutil.util.RoundedCornersTransformation";
private static final byte[] ID_BYTES = ID.getBytes(CHARSET);
private final int radius;
private final int margin;
private final CornerType cornerType;
public RoundedCornersTransformation(int radius, int margin, CornerType cornerType) {
this.radius = radius;
this.margin = margin;
this.cornerType = cornerType;
}
@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
int width = toTransform.getWidth();
int height = toTransform.getHeight();
Bitmap bitmap = pool.get(width, height, Bitmap.Config.ARGB_8888);
if (bitmap == null) {
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(new BitmapShader(toTransform, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
drawRoundRect(canvas, paint, width, height);
return bitmap;
}
private void drawRoundRect(Canvas canvas, Paint paint, float width, float height) {
float right = width - margin;
float bottom = height - margin;
Path path = new Path();
switch (cornerType) {
case ALL:
path.addRoundRect(new RectF(margin, margin, right, bottom), radius, radius, Path.Direction.CW);
break;
case TOP_LEFT:
addTopLeftRoundRect(path, margin, right, bottom, radius);
break;
case TOP_RIGHT:
addTopRightRoundRect(path, margin, right, bottom, radius);
break;
case BOTTOM_LEFT:
addBottomLeftRoundRect(path, margin, right, bottom, radius);
break;
case BOTTOM_RIGHT:
addBottomRightRoundRect(path, margin, right, bottom, radius);
break;
case TOP:
addTopRoundRect(path, margin, right, bottom, radius);
break;
case BOTTOM:
addBottomRoundRect(path, margin, right, bottom, radius);
break;
case LEFT:
addLeftRoundRect(path, margin, right, bottom, radius);
break;
case RIGHT:
addRightRoundRect(path, margin, right, bottom, radius);
break;
case OTHER_TOP_LEFT:
addOtherTopLeftRoundRect(path, margin, right, bottom, radius);
break;
case OTHER_TOP_RIGHT:
addOtherTopRightRoundRect(path, margin, right, bottom, radius);
break;
case OTHER_BOTTOM_LEFT:
addOtherBottomLeftRoundRect(path, margin, right, bottom, radius);
break;
case OTHER_BOTTOM_RIGHT:
addOtherBottomRightRoundRect(path, margin, right, bottom, radius);
break;
case DIAGONAL_FROM_TOP_LEFT:
addDiagonalFromTopLeftRoundRect(path, margin, right, bottom, radius);
break;
case DIAGONAL_FROM_TOP_RIGHT:
addDiagonalFromTopRightRoundRect(path, margin, right, bottom, radius);
break;
default:
path.addRoundRect(new RectF(margin, margin, right, bottom), radius, radius, Path.Direction.CW);
break;
}
canvas.drawPath(path, paint);
}
private void addTopLeftRoundRect(Path path, float left, float right, float bottom, float radius) {
path.moveTo(left + radius, bottom);
path.lineTo(right, bottom);
path.lineTo(right, left);
path.lineTo(left + radius, left);
path.quadTo(left, left, left, left + radius);
path.lineTo(left, bottom);
}
private void addTopRightRoundRect(Path path, float left, float right, float bottom, float radius) {
path.moveTo(left, bottom);
path.lineTo(right - radius, bottom);
path.lineTo(right, bottom - radius);
path.quadTo(right, bottom, right - radius, right);
path.lineTo(left, right);
path.lineTo(left, bottom);
}
private void addBottomLeftRoundRect(Path path, float left, float right, float bottom, float radius) {
path.moveTo(left, bottom - radius);
path.quadTo(left, bottom, left + radius, bottom);
path.lineTo(right, bottom);
path.lineTo(right, left);
path.lineTo(left, left);
path.lineTo(left, bottom - radius);
}
private void addBottomRightRoundRect(Path path, float left, float right, float bottom, float radius) {
path.moveTo(left, bottom);
path.lineTo(right - radius, bottom);
path.quadTo(right, bottom, right, bottom - radius);
path.lineTo(right, left);
path.lineTo(left, left);
path.lineTo(left, bottom);
}
private void addTopRoundRect(Path path, float left, float right, float bottom, float radius) {
path.moveTo(left + radius, bottom);
path.lineTo(right - radius, bottom);
path.lineTo(right, bottom - radius);
path.quadTo(right, bottom, right - radius, right);
path.lineTo(left + radius, right);
path.quadTo(left, right, left, bottom - radius);
path.lineTo(left, bottom);
}
private void addBottomRoundRect(Path path, float left, float right, float bottom, float radius) {
path.moveTo(left, bottom - radius);
path.quadTo(left, bottom, left + radius, bottom);
path.lineTo(right - radius, bottom);
path.quadTo(right, bottom, right, bottom - radius);
path.lineTo(right, left);
path.lineTo(left, left);
path.lineTo(left, bottom - radius);
}
private void addLeftRoundRect(Path path, float left, float right, float bottom, float radius) {
path.moveTo(left, bottom - radius);
path.quadTo(left, bottom, left + radius, bottom);
path.lineTo(right, bottom);
path.lineTo(right, left);
path.lineTo(left + radius, left);
path.quadTo(left, left, left, left + radius);
path.lineTo(left, bottom - radius);
}
private void addRightRoundRect(Path path, float left, float right, float bottom, float radius) {
path.moveTo(left, bottom);
path.lineTo(right - radius, bottom);
path.quadTo(right, bottom, right, bottom - radius);
path.lineTo(right, left + radius);
path.quadTo(right, left, right - radius, left);
path.lineTo(left, left);
path.lineTo(left, bottom);
}
private void addOtherTopLeftRoundRect(Path path, float left, float right, float bottom, float radius) {
path.moveTo(left, bottom - radius);
path.quadTo(left, bottom, left + radius, bottom);
path.lineTo(right, bottom);
path.lineTo(right, left + radius);
path.quadTo(right, left, right - radius, left);
path.lineTo(left + radius, left);
path.lineTo(left, left + radius);
}
private void addOtherTopRightRoundRect(Path path, float left, float right, float bottom, float radius) {
path.moveTo(left, bottom);
path.lineTo(right - radius, bottom);
path.quadTo(right, bottom, right, bottom - radius);
path.lineTo(right, left);
path.lineTo(left + radius, left);
path.quadTo(left, left, left, left + radius);
path.lineTo(left, bottom - radius);
}
private void addOtherBottomLeftRoundRect(Path path, float left, float right, float bottom, float radius) {
path.moveTo(left + radius, bottom);
path.lineTo(right, bottom);
path.lineTo(right, left);
path.lineTo(left, left);
path.lineTo(left, bottom - radius);
path.quadTo(left, bottom, left + radius, bottom);
}
private void addOtherBottomRightRoundRect(Path path, float left, float right, float bottom, float radius) {
path.moveTo(left, bottom);
path.lineTo(right - radius, bottom);
path.lineTo(right, bottom - radius);
path.lineTo(right, left);
path.lineTo(left, left);
path.lineTo(left, bottom - radius);
path.quadTo(left, bottom, left + radius, bottom);
}
private void addDiagonalFromTopLeftRoundRect(Path path, float left, float right, float bottom, float radius) {
path.moveTo(left, bottom - radius);
path.quadTo(left, bottom, left + radius, bottom);
path.lineTo(right, bottom);
path.lineTo(right, left);
path.lineTo(left + radius, left);
path.quadTo(left, left, left, left + radius);
}
private void addDiagonalFromTopRightRoundRect(Path path, float left, float right, float bottom, float radius) {
path.moveTo(left, bottom);
path.lineTo(right - radius, bottom);
path.quadTo(right, bottom, right, bottom - radius);
path.lineTo(right, left);
path.lineTo(left, left);
path.lineTo(left, bottom - radius);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RoundedCornersTransformation that = (RoundedCornersTransformation) o;
return radius == that.radius && margin == that.margin && cornerType == that.cornerType;
}
@Override
public int hashCode() {
int result = radius;
result = 31 * result + margin;
result = 31 * result + cornerType.hashCode();
return result;
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
messageDigest.update(ID_BYTES);
byte[] radiusData = ByteBuffer.allocate(4).putInt(radius).array();
messageDigest.update(radiusData);
byte[] marginData = ByteBuffer.allocate(4).putInt(margin).array();
messageDigest.update(marginData);
byte[] cornerTypeData = cornerType.name().getBytes(CHARSET);
messageDigest.update(cornerTypeData);
}
public enum CornerType {
ALL,
TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT,
TOP, BOTTOM, LEFT, RIGHT,
OTHER_TOP_LEFT, OTHER_TOP_RIGHT, OTHER_BOTTOM_LEFT, OTHER_BOTTOM_RIGHT,
DIAGONAL_FROM_TOP_LEFT, DIAGONAL_FROM_TOP_RIGHT
}
}