package cn.muchinfo.rma.view.autoWidget

import android.graphics.PorterDuff
import android.graphics.drawable.Drawable
import android.graphics.drawable.StateListDrawable


/**
 * 用于自动设置点击时的变色
 */
class PressedEffectStateListDrawable(drawable: Drawable, private val selectionColor: Int) : StateListDrawable() {

    init {
        addState(intArrayOf(android.R.attr.state_pressed), drawable)
        addState(intArrayOf(), drawable)
    }

    override fun onStateChange(states: IntArray): Boolean {
        val isStatePressedInArray = states.contains(android.R.attr.state_pressed)
        if (isStatePressedInArray) {
            super.setColorFilter(selectionColor, PorterDuff.Mode.MULTIPLY)
        } else {
            super.clearColorFilter()
        }
        return super.onStateChange(states)
    }

    override fun isStateful(): Boolean {
        return true
    }
}