Skip to content

Instantly share code, notes, and snippets.

@theapache64
Created April 12, 2019 05:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theapache64/31f4dad152bfee2e053806e0fe514e84 to your computer and use it in GitHub Desktop.
Save theapache64/31f4dad152bfee2e053806e0fe514e84 to your computer and use it in GitHub Desktop.
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