Created
June 14, 2020 08:39
-
-
Save macieknajbar/ddda4eacea8d044c04e6c25284ecf64e to your computer and use it in GitHub Desktop.
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
class FillDrawable(private val color: Int, layers: Array<out Drawable>) : LayerDrawable(layers) { | |
private val radius: Float = 20f | |
private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { | |
color = [email protected] | |
} | |
private var r = copyBounds() | |
private var activeState = false | |
override fun isStateful(): Boolean = true | |
override fun onBoundsChange(bounds: Rect?) { | |
super.onBoundsChange(bounds) | |
val b = requireNotNull(bounds) | |
r = Rect(b.left, b.top, b.left + (b.right * 0.1).toInt(), b.bottom) | |
} | |
override fun draw(canvas: Canvas) { | |
super.draw(canvas) | |
canvas.drawRoundRect(r.toRectF(), radius, radius, paint) | |
} | |
override fun onStateChange(state: IntArray?): Boolean { | |
val stateSet = state?.toSet() ?: emptySet() | |
var enabled = false | |
var pressed = false | |
for (s in stateSet) { | |
when (s) { | |
android.R.attr.state_enabled -> enabled = true | |
android.R.attr.state_pressed -> pressed = true | |
} | |
} | |
setActive(enabled && pressed) | |
return super.onStateChange(state) | |
} | |
private fun setActive(active: Boolean) { | |
if (activeState != active) { | |
activeState = active | |
if (active) { | |
extendsWidth() | |
} else { | |
anim.cancel() | |
} | |
} | |
} | |
private fun setWidth(factor: Float) { | |
r = Rect( | |
bounds.left, | |
bounds.top, | |
(bounds.right * factor).toInt(), | |
bounds.bottom | |
) | |
invalidateSelf() | |
} | |
private val anim = ObjectAnimator.ofFloat(this, "width", 0.1f, 1f) | |
private fun extendsWidth() { | |
anim.startDelay = 100 | |
anim.duration = 300 | |
anim.interpolator = LinearInterpolator() | |
anim.start() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment