加载圆角图片(上下文,图片,控件)GlideImage.setGlideImageView(context,company.getLogo(),viewHolder1.iv_img);
加载圆圈轮廓GlideImage.setGlideImageView(mContext,company.getLogo(),logo);
public class GlideCircleTransform extends BitmapTransformation {
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
}
public GlideCircleTransform(Context 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;
}
}
public class GlideRoundNewTransform extends BitmapTransformation {
private static float radius = 0f;
public GlideRoundNewTransform(Context context) {
this(context, 4);
}
public GlideRoundNewTransform(Context context, int dp) {
super();
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 bitmap = changeBitmapSize(source);
Bitmap result = pool.get(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_4444);
if (result == null) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
result = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_4444);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
RectF rectF = new RectF(0f, 0f, bitmap.getWidth(), bitmap.getHeight());
canvas.drawRoundRect(rectF, radius, radius, paint);
return result;
}
public static Bitmap changeBitmapSize(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
//设置想要的大小
int newWidth=72;
int newHeight=72;
//计算压缩的比率
float scaleWidth=((float)newWidth)/width;
float scaleHeight=((float)newHeight)/height;
//获取想要缩放的matrix
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth,scaleHeight);
//获取新的bitmap
bitmap=Bitmap.createBitmap(bitmap,0,0,width,height,matrix,true);
bitmap.getWidth();
bitmap.getHeight();
Log.e("newWidth","newWidth"+bitmap.getWidth());
Log.e("newHeight","newHeight"+bitmap.getHeight());
return bitmap;
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
}
}
public class GlideRoundTransform extends BitmapTransformation {
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
}
private static float radius = 0f;
/**
* 构造函数 默认圆角半径 4dp
* @param context Context
*/
public GlideRoundTransform(Context context) {
this(context, 6);
radius = Resources.getSystem().getDisplayMetrics().density * 6;
}
/**
* 构造函数
* @param context Context
* @param dp 圆角半径
*/
public GlideRoundTransform(Context context, int dp) {
super();
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 class GlideImage {
// 普通加载图片
public static void setImg(Context context, String url, ImageView imageView){
RequestOptions options = new RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.placeholder(R.mipmap.main_hot_summary_default_img)
.error(R.mipmap.main_hot_summary_default_img);
Glide.with(context)
.load(url)
.apply(options)
.into(imageView);
}
// 加载方角图片
public static void setRoundImg(Context context, String url, ImageView imageView,int num){
RequestOptions options = new RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.dontAnimate()
.transforms( new CenterCrop(),new GlideRoundTransform(context,num));
//Glide 加载图片简单用法
Glide.with(context)
.load(url)
.apply(options)
.into(imageView);
}
// 图片
public static void setGlideImageView(Context context, String url, ImageView imageView){
int width = (int) ScreenUtils.dp2px(context, 60);
int height = (int) ScreenUtils.dp2px(context, 50);
RequestOptions options = new RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.override(width,height)
.placeholder(R.mipmap.company_default)
.error(R.mipmap.company_default)
.centerInside()
.dontAnimate();
// .transforms(new CenterCrop(), new GlideCircleTransform(context));
//Glide 加载图片简单用法
Glide.with(context)
.load(url)
.apply(options)
.into(imageView);
}
// 加载圆角图片
public static void setCircleImg(Context context, String url, ImageView imageView){
RequestOptions options = new RequestOptions()
.placeholder(R.mipmap.main_hot_summary_default_img)
.error(R.mipmap.main_hot_summary_default_img)
.transform(new CenterCrop(), new GlideCircleTransform(context));
//Glide 加载图片简单用法
Glide.with(context)
.load(url)
.apply(options)
.into(imageView);
}
// 加载方角图片
public static void setRoundBitmap(Context context, String url, ImageView imageView, int num){
RequestOptions options = new RequestOptions()
.placeholder(R.mipmap.main_hot_summary_default_img)
.error(R.mipmap.main_hot_summary_default_img)
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.dontAnimate()
.transform( new CenterCrop(),new GlideRoundTransform(context,num));
//Glide 加载图片简单用法
Glide.with(context)
.load(url)
.apply(options)
.into(imageView);
}
}
网友评论