Make Canvas easier to use in Kotlin
Kanvas
WORK IN PROGRESS
Make canvas easier to use in Kotlin blush
Use shapes & write less code to do more magic
val canvasAnimator = CanvasAnimator(this)
val background = RectShape {
this.color = Color.parseColor("#6fbf73")
this.cornerRadius = 16.dpToPx(context)
}.initWhenViewHasSize(this){ view ->
left = 100f
width = view.width / 2f
top = 100f
height = view.height / 3f
}
fun animate(){
canvasAnimator
.play(background.animate().right.to(this.width.toFloat()))
.start()
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
canvas.draw(background)
}
Examples
Available shapes
Shapes | link |
---|---|
Rect | |
Circle | |
Text | |
Arc | |
Line | |
Triangle | |
Drawable | |
Path |
Animation
Follow the example of SwitchView
Shape animations are executed by an instance of ShapeAnimator
attached to your view
val canvasAnimator = CanvasAnimator(this)
All animated methods of shapes are wrapped into the method .animate()
For example, for a CircleShape
, you can animate his position (centerX) using
myCircleShape.animate().centerX.to(15);
Then use your ShapeAnimator
to execute this animation
shapeAnimator.play(myCircleShape.animate().centerX.to(15);)
.setDuration(500)
.start()
Added support methods to Canvas
drawArc(centerX, centerY, circleRadius, startAngle, sweepAngle, paint)
drawArc(center: PointF, circleRadius, startAngle, sweepAngle, paint)
drawArc(left, top, right, bottom, startAngle, sweepAngle, useCenter, paint)
drawOval(left, top, right, bottom, startAngle, sweepAngle, useCenter, paint)
drawLine(start: PointF, end: PointF, paint)
drawRoundRect(left, top, right, bottom, rx, ry, paint)
Added support methods to Path
addRoundRect(left, top, right, bottom, rx, ry, dir: Path.Direction)
addRoundRect(left, top, right, bottom, radiiArray, dir: Path.Direction)
arcTo(centerX, centerY, circleRadius, startAngle, sweepAngle, forceMoveTo)
arcTo(center: PointF, circleRadius, startAngle, sweepAngle, forceMoveTo)
arcTo(left, top, right, bottom, startAngle, sweepAngle, forceMoveTo)