Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
class DonutView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
private val icingPathEffect = ComposePathEffect(
CornerPathEffect(50f),
DiscretePathEffect(80f, 50f)
)
private val basePaint = Paint().apply {
isAntiAlias = true
}
private val icingPaint = Paint().apply {
isAntiAlias = true
style = Paint.Style.FILL
color = Color.parseColor("#491F0E")
pathEffect = icingPathEffect
}
private val holePath = Path()
private var holeRad: Float = 0.0f
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
super.onLayout(changed, left, top, right, bottom)
holePath.reset()
val cx = (width / 2).toFloat()
val cy = (height / 2).toFloat()
this.holeRad = ((if (width > height) height else width) / 6).toFloat()
holePath.addCircle(cx, cy, holeRad, Path.Direction.CW)
}
private val rect = RectF()
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
canvas?.apply {
// Open hole
save()
val cx = (width / 2).toFloat()
val cy = (height / 2).toFloat()
scale(1f, 1f, cx, cy)
clipPath(holePath, Region.Op.DIFFERENCE)
// Drawing base
val padding = 100
val baseRad = ((if (width > height) height else width) / 2).toFloat() - padding
basePaint.color = Color.parseColor("#B34B00")
drawCircle(cx, cy, baseRad, basePaint)
// Drawing icing
val icingRad = baseRad - (baseRad / 5.5f)
drawCircle(cx, cy, icingRad, icingPaint)
// Close hole
restore()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.