美文网首页
Android 单色图片变色 setTint实现

Android 单色图片变色 setTint实现

作者: ElonYanJ | 来源:发表于2018-11-19 17:47 被阅读14次
    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)
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Android 单色图片变色 setTint实现

          本文链接:https://www.haomeiwen.com/subject/qnmafqtx.html