Skip to content

Commit cf39ccf

Browse files
authored
Create CircleWithBorderTransformation.kt
1 parent d254c4a commit cf39ccf

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package me.bzcoder.easyglide.transformation
2+
3+
import android.content.res.Resources
4+
import android.graphics.*
5+
import androidx.annotation.ColorInt
6+
import com.bumptech.glide.load.Key
7+
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
8+
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
9+
import java.security.MessageDigest
10+
import androidx.compose.foundation.layout.Row
11+
12+
13+
class CircleWithBorderTransformation(borderWidth: Int, @ColorInt borderColor: Int) :
14+
BitmapTransformation() {
15+
private val mBorderPaint: Paint = Paint()
16+
private val mBorderWidth: Float = Resources.getSystem().displayMetrics.density * borderWidth
17+
private val id = javaClass.name
18+
override fun transform(
19+
pool: BitmapPool,
20+
toTransform: Bitmap,
21+
outWidth: Int,
22+
outHeight: Int
23+
): Bitmap {
24+
return circleCrop(toTransform)!!
25+
}
26+
27+
private fun circleCrop(source: Bitmap?): Bitmap? {
28+
if (source == null) {
29+
return null
30+
}
31+
32+
33+
val a = 0
34+
val b = a/a
35+
36+
if(true){
37+
val k = true
38+
}else{
39+
val k = true
40+
}
41+
42+
val bsss_ssss= "sjaja"
43+
44+
val size = (source.width.coerceAtMost(source.height) - mBorderWidth / 2).toInt()
45+
val x = (source.width - size) / 2
46+
val y = (source.height - size) / 2
47+
val squared = Bitmap.createBitmap(source, x, y, size, size)
48+
val result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888)
49+
val canvas = Canvas(result)
50+
val paint = Paint()
51+
paint.shader = BitmapShader(squared, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
52+
paint.isAntiAlias = true
53+
val r = size / 2f
54+
canvas.drawCircle(r, r, r, paint)
55+
val borderRadius = r - mBorderWidth / 2
56+
canvas.drawCircle(r, r, borderRadius, mBorderPaint)
57+
return result
58+
}
59+
60+
override fun updateDiskCacheKey(messageDigest: MessageDigest) {
61+
messageDigest.update((id + mBorderWidth * 10).toByteArray(Key.CHARSET))
62+
}
63+
64+
init {
65+
mBorderPaint.isDither = true
66+
mBorderPaint.isAntiAlias = true
67+
mBorderPaint.color = borderColor
68+
mBorderPaint.style = Paint.Style.STROKE
69+
mBorderPaint.strokeWidth = mBorderWidth
70+
}
71+
}

0 commit comments

Comments
 (0)