Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sudox.design.circleImageView
- import android.content.Context
- import android.graphics.*
- import android.util.AttributeSet
- import kotlin.math.min
- class CircleImageView : androidx.appcompat.widget.AppCompatImageView {
- private val emptyPaint = Paint()
- private val circlePoint = Paint().apply {
- color = Color.TRANSPARENT
- xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)
- isAntiAlias = true
- }
- private val fromBToAPaint = Paint().apply {
- xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_OUT)
- }
- constructor(context: Context) : this(context, null)
- constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
- constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
- override fun onDraw(canvas: Canvas?) {
- canvas ?: return
- // Create a new layer and draw the image
- val a = canvas.saveLayer(0f, 0f, width.toFloat(), height.toFloat(), emptyPaint)
- super.onDraw(canvas)
- // Create a new layer to crop a layer
- val b = canvas.saveLayer(0f,0f, width.toFloat(), height.toFloat(), fromBToAPaint)
- // Fill and cut circle
- canvas.drawColor(Color.GREEN)
- canvas.drawCircle(width / 2f, height / 2f, min(width, height) / 2f, circlePoint)
- // Delete from a the b layer
- canvas.restoreToCount(b)
- // Paint result
- canvas.restoreToCount(a)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement