I am trying to flash different colours onto the screen at regular intervals (a few times per second).
To change the colours, I use Drawable.setColorFilter(int color, Mode mode)
on the background of my main view:
myView.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC);
For debugging purposes, I added another view that I change using View.setBackgroundColor(int color)
.
The problem is that the setColorFilter()
calls are working on Lollipop, but are broken on previous versions (specifically Nexus 7 v4.4.4, Galaxy Nexus v4.2.1).
I call the colour changing code inside a Runnable
that is triggered at regular intervals by a Handler
.
The handler is being called on all platforms (I can see the background changes due to the debugging setBackgroundColor()
calls).
Below is the colour cycling code:
Handler mHandler;
RunnableOnTick thisRunnable;
View vDebug;
View vBroken;
class RunnableOnTick implements Runnable
{
int backgroundColor;
@Override
public void run()
{
color = random.nextInt(2);
switch (color)
{
case 0:
{
backgroundColor = Color.RED;
break;
}
case 1:
{
backgroundColor = Color.GREEN;
break;
}
}
// this works on all platforms
vDebug.setBackgroundColor(backgroundColor);
// this works only on Lollipop
vBroken.getBackground().setColorFilter(backgroundColor, PorterDuff.Mode.SRC);
vBroken.invalidate();
mHandler.postDelayed(thisRunnable, 100);
}
}
I have tried different PorterDuff.Mode
values - still can't get it working on Android 4.
What is different between Android v4 and v5 that would change the way setColorFilter()
works?
Drawable
you're using exactly? It might depend on particular implementation.BitmapDrawable
. Well, if you don't have the app anymore, then it's hard to find the problem now. I can certainly say that color filter was working in KitKat (you can simply try to usetint
attribute ofImageView
- it is backed by ColorFilter)