There are a lot of articles and codes about custom Glide Transformation. Originally, I also found a partial rounded corner picture of Glide loading on the Internet and used it directly in the project. However, I encountered a Bug in the process of using it, so I checked the official documents. It turns out that the custom Transformation forgot the most important part, overriding equals()/hashCode(). It works in normal scenarios, but in scenarios like RecyclerView that load a lot of images, Custom transform without overwriting equals()/hashCode() causes bugs.

(EMMM, what is the specific Bug I forget, probably slide stuck? Because it is a long time ago encountered the problem, now fill the article)

Let’s look atThe official documentation:The official article notes that the equals()/hashCode()/updateDiskCacheKey() methods must be overridden to make disk and memory caches work correctly. The equals()/hashCode() disk cache may not work properly without overriding it, and while it can be compiled, it can cause problems like memory leaks when used.

Finally, attached is the modified Round Corner Form, which can be used normally at present:

//Koltin version class RoundedCornersTransform(context: context? , var radius: Float, var leftTop: Boolean = true, var rightTop: Boolean = true, var leftBottom: Boolean = true, var rightBottom: Boolean = true ) : Transformation<Bitmap? > { private val mBitmapPool: BitmapPool = Glide.get(context!!) .bitmapPool private val id = javaClass.name private val idBytes = id.toByteArray(Charsets.UTF_8); override fun transform( context: Context, resource: Resource<Bitmap? >, outWidth: Int, outHeight: Int ): Resource<Bitmap? > {val source: Bitmap = resources.get () var finalWidth: Int var finalHeight: Int Float if (outWidth > outHeight) {scale = outheight.tofloat ()/outwidth.tofloat () finalWidth = outHeight FinalHeight = (source.width. ToFloat () * scale).toint () if (finalHeight > source.height) {finalHeight = (source.width. ToFloat () * scale).toint () if (finalHeight > source.height) { Scale = outwidth.tofloat ()/outheight.tofloat () finalHeight = source.height // Fix the original height, find the final width ToInt ()}} else if (outWidth < outHeight) {finalWidth = (source.height.tofloat () * scale).toint ()}} else if (outWidth < outHeight) = outwidth.tofloat ()/outHeight. ToFloat () finalHeight = source.height // fix the original height, find the finalWidth finalWidth = (source.height.tofloat () * scale).toint () if (finalWidth > source.width) {// If (finalWidth > source.width outHeight.toFloat() / outWidth.toFloat() finalWidth = source.width finalHeight = (source.width.toFloat() * Scale).toint ()}} else {// If output width = output height finalHeight = source.height finalWidth = finalHeight} // Correct rounded radius *= finalHeight.toFloat() / outHeight.toFloat() val outBitmap: Bitmap = mBitmapPool[finalWidth, finalHeight, Bitmap.config.argb_8888] val canvas = canvas (outBitmap) val paint = paint () // Associate the original drawing with the brush Bitmap val shader = BitmapShader(Source, shader.tilemode.CLAMP, shader.tilemode.CLAMP) Int = (source.width - finalWidth) / 2 val height: Int = (source.height - finalHeight) / 2 if (width ! = 0 || height ! = 0) { val matrix = Matrix() matrix.setTranslate((-width).toFloat(), (-height).toFloat()) shader.setLocalMatrix(matrix) } paint.shader = shader paint.isAntiAlias = true val rectF = RectF(0.0f, 0.0f, canvas.width.tofloat (), canvas.height.tofloat ()) Radius, paint) // top left corner rounded if (! LeftTop) {canvas.drawRect(0f, 0f, radius, radius, paint)} RightTop) {canvas.drawRect(Canvas.width-radius, 0f, canvas.width.tofloat (), radius, paint)} LeftBottom) {canvas.drawrect (0f, canvas.height-radius, radius, canvas.height.tofloat (), paint)} rightBottom) { canvas.drawRect( canvas.width - radius, canvas.height - radius, canvas.width.toFloat(), canvas.height.toFloat(), paint ) } return BitmapResource.obtain(outBitmap, mBitmapPool)!! } /** must override */ override fun equals(other: Any?) : Boolean { return other is RoundedCornersTransform } /** must override */ override fun hashCode(): Int { return id.hashCode() } /** must override */ override fun updateDiskCacheKey(messageDigest: MessageDigest) { messageDigest.update(idBytes) } }Copy the code

RoundedCornersTransform use:

// Trim four corners by default, no need to set corners, Glide.with(context).load(item.coverURL).apply(RequestOptions().transform(CenterCrop(), RoundedCornersTransform( context, 8f, leftBottom = false, rightBottom = false ) ) ) .into(holder.getView(R.id.img_icon))Copy the code