import android.content.res.Resources
import android.support.v4.graphics.drawable.DrawableCompat
import android.widget.ImageView
class StaticUtiil {
companion object {
fun changeImageViewColor(imageView: ImageView, color: Int, resources: Resources) {
DrawableCompat.setTint(imageView.drawable, resources.getColor(color))
imageView.setImageDrawable(imageView.drawable)
imageView.setImageDrawable(imageView.drawable)
}
}
}
import android.app.Activity
import android.os.Bundle
import android.widget.ImageView
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
iv_one.setOnClickListener {
if (iv_one.isSelected) {
iv_one.isSelected = false
StaticUtiil.changeImageViewColor(it as ImageView, R.color.black, resources)
} else {
iv_one.isSelected = true
StaticUtiil.changeImageViewColor(it as ImageView, R.color.colorPrimary, resources)
}
}
}
}
网友评论