-
Notifications
You must be signed in to change notification settings - Fork 365
/
Copy pathRoundedRectangle.kt
40 lines (33 loc) · 1.19 KB
/
RoundedRectangle.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.takusemba.spotlight.shape
import android.animation.TimeInterpolator
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.PointF
import android.graphics.RectF
import android.view.animation.DecelerateInterpolator
import java.util.concurrent.TimeUnit
/**
* [Shape] of RoundedRectangle with customizable height, width, and radius.
*/
class RoundedRectangle @JvmOverloads constructor(
private val height: Float,
private val width: Float,
private val radius: Float,
override val duration: Long = DEFAULT_DURATION,
override val interpolator: TimeInterpolator = DEFAULT_INTERPOLATOR
) : Shape {
override fun draw(canvas: Canvas, point: PointF, value: Float, paint: Paint) {
val halfWidth = width / 2 * value
val halfHeight = height / 2 * value
val left = point.x - halfWidth
val top = point.y - halfHeight
val right = point.x + halfWidth
val bottom = point.y + halfHeight
val rect = RectF(left, top, right, bottom)
canvas.drawRoundRect(rect, radius, radius, paint)
}
companion object {
val DEFAULT_DURATION = TimeUnit.MILLISECONDS.toMillis(500)
val DEFAULT_INTERPOLATOR = DecelerateInterpolator(2f)
}
}