Last active
June 13, 2019 18:26
-
-
Save titoaesj/9ff47c051d15aee736ece7276d426548 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class CircleTransform : Transformation { | |
override fun transform(source: Bitmap): Bitmap { | |
val size = Math.min(source.width, source.height) | |
val x = (source.width - size) / 2 | |
val y = (source.height - size) / 2 | |
val squaredBitmap = Bitmap.createBitmap(source, x, y, size, size) | |
if (squaredBitmap != source) { | |
source.recycle() | |
} | |
val bitmap = Bitmap.createBitmap(size, size, source.config) | |
val canvas = Canvas(bitmap) | |
val paint = Paint() | |
val shader = BitmapShader( | |
squaredBitmap, | |
Shader.TileMode.CLAMP, Shader.TileMode.CLAMP | |
) | |
paint.shader = shader | |
paint.isAntiAlias = true | |
val r = size / 2f | |
canvas.drawCircle(r, r, r, paint) | |
squaredBitmap.recycle() | |
return bitmap | |
} | |
override fun key(): String = "circle" | |
} | |
// Como usar | |
Picasso.with(itemView.context).load(seu_resource).transform(CircleTransform()).into(seu_component_image_view) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment