Guest User

Untitled

a guest
Jan 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. class CircleCropTransformation : Transformation {
  2. override fun transform(source: Bitmap?): Bitmap {
  3. val size: Int = Math.min(source!!.width, source.height)
  4. val x: Int = (source.width - size) / 2
  5. val y: Int = (source.height - size) / 2
  6.  
  7. val squaredBitmap: Bitmap = Bitmap.createBitmap(source, x, y, size, size)
  8. if (squaredBitmap != source) {
  9. source.recycle()
  10. }
  11.  
  12. val bitmap: Bitmap = Bitmap.createBitmap(size, size, source.config)
  13. val canvas = Canvas(bitmap)
  14. val paint = Paint()
  15.  
  16. val shader = BitmapShader(
  17. squaredBitmap,
  18. Shader.TileMode.CLAMP,
  19. Shader.TileMode.CLAMP
  20. )
  21.  
  22. paint.shader = shader
  23. paint.isAntiAlias = true
  24.  
  25. val r = size / 2f
  26. canvas.drawCircle(r, r, r, paint)
  27. squaredBitmap.recycle()
  28. return bitmap
  29.  
  30. }
  31.  
  32. override fun key(): String {
  33. return "circle"
  34. }
  35. }
Add Comment
Please, Sign In to add comment