Skip to content

Instantly share code, notes, and snippets.

@vorobeij
Last active August 25, 2018 21:00
Show Gist options
  • Save vorobeij/83fcbbe52f039c9eb853a9a113c8c2cc to your computer and use it in GitHub Desktop.
Save vorobeij/83fcbbe52f039c9eb853a9a113c8c2cc to your computer and use it in GitHub Desktop.
State animations
class CompoundButton : BaseCompoundView {
override fun loadAttrs(attrs: AttributeSet) {
val styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.CompoundButton, 0, 0)
val iconStyleableId = R.styleable.CompoundButton_xxx
imageView.setImageDrawable(StateListDrawable().apply {
setFadeDuration(FADE_DURATION)
addState(STATE_ACTIVE, getDrawable(styledAttrs, iconStyleableId, colorActive))
addState(STATE_PRESSED, getDrawable(styledAttrs, iconStyleableId, colorLeftIconBacgroundPressed))
})
styledAttrs.recycle()
}
...
companion object {
private val STATE_ACTIVE = intArrayOf(-android.R.attr.state_pressed)
private val STATE_PRESSED = intArrayOf(android.R.attr.state_pressed)
private const val FADE_DURATION = 80
}
}
fun Drawable.setColor(color: Int) = this.mutate().setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment