Created
February 23, 2019 23:39
-
-
Save yfujiki/bbfe74d4841189e39e610bd3cc3e5ba9 to your computer and use it in GitHub Desktop.
Implementation of PathDrawable
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
// x and y are normalized value which are in 0~1. | |
// It doesn't necessarily need to be like this, but I valued the merit that | |
// I don't need to consider the actual size of the path depending on the layout. | |
data class NormalizedPoint(val x: Float, val y: Float) | |
open class PathDrawable(val points: MutableList<NormalizedPoint>, val context: Context) : Drawable() { | |
protected val path: Path = { | |
val p = Path() | |
p | |
}() | |
protected val paint: Paint = { | |
val p = Paint() | |
p.color = ContextCompat.getColor(context, android.R.color.holo_blue_dark) | |
p.strokeWidth = 20.0f | |
p.style = Paint.Style.STROKE | |
p.strokeCap = Paint.Cap.ROUND | |
p | |
}() | |
init { | |
} | |
override fun setAlpha(alpha: Int) { | |
} | |
override fun getOpacity(): Int { | |
return PixelFormat.TRANSLUCENT | |
} | |
override fun setColorFilter(colorFilter: ColorFilter?) { | |
} | |
// Main method of the Drawable. It is called on every rendering cycle. | |
override fun draw(canvas: Canvas) { | |
configurePath() | |
canvas.drawPath(path, paint) | |
} | |
// The main logic of PathDrawable. Configure a path based on the specified points. | |
protected fun configurePath() { | |
path.reset() | |
val b = bounds | |
points.forEachIndexed { index, point -> | |
val x = point.x | |
val y = point.y | |
if (index == 0) { | |
path.moveTo(x * b.width(), y * b.height()) | |
} else { | |
path.lineTo(x * b.width(), y * b.height()) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment