DrawableCompat.setTint不靠谱?
DrawableCompat.setTint not reliable?
我有两个 ImageView
(向上,向下),我想根据某些操作更新颜色。共有三种不同的情况:
up red, down black
up black, down red
up green, down green
我有三个函数可以相应地更改颜色并在正确的位置执行(我有一个 TextView 打印出当前调用的内容,结果符合预期),但我发现它们通常不会无法正常工作——因为它们只为一个 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))
}
这里怎么会发生那些错误的案例?
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)
}
这在我的案例中做到了
我有两个 ImageView
(向上,向下),我想根据某些操作更新颜色。共有三种不同的情况:
up red, down black
up black, down red
up green, down green
我有三个函数可以相应地更改颜色并在正确的位置执行(我有一个 TextView 打印出当前调用的内容,结果符合预期),但我发现它们通常不会无法正常工作——因为它们只为一个 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))
}
这里怎么会发生那些错误的案例?
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)
}
这在我的案例中做到了