Glide实现圆角图片,以及圆形图片
程序员文章站
2022-08-29 14:08:25
Glide很常用,为了避免经常迁移项目导致重写工具类,记录下实现圆角图片的功能以及圆形图片的功能,圆角图片如下:package com.yyb.mvvmlibrary.widgetimport android.content.Contextimport android.content.res.Resourcesimport android.graphics.*import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPoolimpo...
Glide很常用,为了避免经常迁移项目导致重写工具类,记录下实现圆角图片的功能以及圆形图片的功能分为kotlin和java版本,
圆角图片如下kotlin版本如下:
package com.yyb.mvvmlibrary.widget
import android.content.Context
import android.content.res.Resources
import android.graphics.*
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import java.security.MessageDigest
/**
* author : 闫裕波
* e-mail : yyb@zlhopesun.com
* time : 2020/8/7
* desc : 圆角图标
*/
class GlideRoundTransform @JvmOverloads constructor(
context: Context?,
dp: Int = 4
) : BitmapTransformation() {
override fun transform(
pool: BitmapPool,
toTransform: Bitmap,
outWidth: Int,
outHeight: Int
): Bitmap {
return roundCrop(pool, toTransform)!!
}
override fun updateDiskCacheKey(messageDigest: MessageDigest) {}
companion object {
private var radius = 0f
private fun roundCrop(pool: BitmapPool, source: Bitmap?): Bitmap? {
if (source == null) return null
val result =
pool[source.width, source.height, Bitmap.Config.ARGB_8888]
val canvas = Canvas(result)
val paint = Paint()
paint.shader = BitmapShader(
source,
Shader.TileMode.CLAMP,
Shader.TileMode.CLAMP
)
paint.isAntiAlias = true
val rectF = RectF(
0f, 0f, source.width.toFloat(), source.height
.toFloat()
)
canvas.drawRoundRect(
rectF,
radius,
radius,
paint
)
return result
}
}
/**
* 构造函数
*
* @param context Context
* @param dp 圆角半径
*/
/**
* 构造函数 默认圆角半径 4dp
*
* @param context Context
*/
init {
radius =
Resources.getSystem().displayMetrics.density * dp
}
}
工具类如下:
//圆形图片
fun circleImageUrl(view: ImageView, url: String) {
Glide.with(view.context)
.load(url)
.apply(RequestOptions.bitmapTransform(CircleCrop()))
.transition(DrawableTransitionOptions.withCrossFade(500))
.into(view)
}
//圆角图片
fun roundedCornerImagUrl(view:ImageView,url:String,radiis:Int){
Glide.with(view.context)
.load(url)
.transform(CenterCrop(),GlideRoundTransform(view.context,radiis))
.transition(DrawableTransitionOptions.withCrossFade(500))
.into(view)
}
java版本
package com.zlhs.wms_android.common.widget;
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.RectF;
import androidx.annotation.NonNull;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import java.security.MessageDigest;
/**
* author : 闫裕波
* e-mail : yyb@zlhopesun.com
* time : 2020/8/7
* desc : 圆角图标
*/
public class GlideRoundTransform extends BitmapTransformation {
private static float radius = 0f;
/**
* 构造函数 默认圆角半径 4dp
*
* @param context Context
*/
public GlideRoundTransform(Context context) {
this(context, 4);
}
/**
* 构造函数
*
* @param context Context
* @param dp 圆角半径
*/
public GlideRoundTransform(Context context, int dp) {
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);
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 void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
}
}
java版本Utilis
/**
- 方法描述:加载圆角图片
- @author 闫裕波
- ==========================
*/
public static void loadRoundLocalImage(Context context, int url, final ImageView imageView,int roundingRadius) {
Glide.with(context)
.load(url)
.placeholder(placeholder)
.error(placeholder)
//.centerCrop() 千万不要加,加了就没有圆角效果了
.transform(new CenterCrop(), new GlideRoundTransform(context,roundingRadius))
.into(imageView);
}
本文地址:https://blog.csdn.net/m0_37777069/article/details/108821057