drawablecompat.settint不可靠?

lndjwyie  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(306)

我有两个 ImageView (向上,向下)我想根据某些动作更新颜色。有三种不同的情况: up red, down black up black, down red up green, down green 我有三个函数来相应地更改颜色,它们在正确的位置执行(我有一个文本视图,打印出当前调用的内容,结果与预期一样),但是我发现它们通常不能正常工作-因为它们只对一个imageview着色。正如我在上面所写的,这是三种可能的情况-但是有时一个imageview是绿色的,另一个是红色的,或者两个imageview都是红色的,或者只有一个imageview是绿色的,尽管处于 up green, down green 案例-这不应该发生。功能如下:

/**17170452 = green
 * 17170444 = black
 * 17170455 = red
 */
    @SuppressLint("ResourceType")
    override fun colorTuned() {
        DrawableCompat.setTint(down.drawable, ContextCompat.getColor(applicationContext, 17170452))
        DrawableCompat.setTint(up.drawable, ContextCompat.getColor(applicationContext, 17170452))
    }
    @SuppressLint("ResourceType")
    override fun colorDown() {
        DrawableCompat.setTint(down.drawable, ContextCompat.getColor(applicationContext, 17170455))
        DrawableCompat.setTint(up.drawable, ContextCompat.getColor(applicationContext, 17170444))
    }

    @SuppressLint("ResourceType")
    override fun colorUp() {
        DrawableCompat.setTint(down.drawable, ContextCompat.getColor(applicationContext, 17170444))
        DrawableCompat.setTint(up.drawable, ContextCompat.getColor(applicationContext, 17170455))
    }

这些错案怎么会在这里发生?

ruoxqz4g

ruoxqz4g1#

val drawableUp = DrawableCompat.wrap(ContextCompat.getDrawable(this, R.drawable.ic_play_arrow)!!)
up.setImageDrawable(drawableUp)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
    DrawableCompat.setTint(drawableUp, ContextCompat.getColor(this, colorTop))

} else {
    drawableUp.mutate().colorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(colorTop, BlendModeCompat.SRC_ATOP)
}

我的案子就是这样

相关问题