-
-
Save johnny3young/d125a7df3b8baaee93ecd184caaaa7e7 to your computer and use it in GitHub Desktop.
Picasso circle transformation by kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.* | |
import com.squareup.picasso.Transformation | |
// https://gist.github.com/codezjx/b8a99374385a0210edb9192bced516a3 | |
class CircleTransformation: Transformation { | |
companion object { | |
private val KEY = "circleImageTransformation" | |
} | |
override fun transform(source: Bitmap): Bitmap { | |
val minEdge = Math.min(source.width, source.height) | |
val dx = (source.width - minEdge) / 2 | |
val dy = (source.height - minEdge) / 2 | |
// Init shader | |
val shader = BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) | |
val matrix = Matrix() | |
matrix.setTranslate((-dx).toFloat(), (-dy).toFloat()) // Move the target area to center of the source bitmap | |
shader.setLocalMatrix(matrix) | |
// Init paint | |
val paint = Paint(Paint.ANTI_ALIAS_FLAG) | |
paint.shader = shader | |
// Create and draw circle bitmap | |
val output = Bitmap.createBitmap(minEdge, minEdge, source.config) | |
val canvas = Canvas(output) | |
canvas.drawOval(RectF(0f, 0f, minEdge.toFloat(), minEdge.toFloat()), paint) | |
source.recycle() | |
return output | |
} | |
override fun key(): String = KEY | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment