setColorFilter()上破的Android 4.0,工作在Android上5
我想不同的颜色闪烁在屏幕上每隔一定时间(每秒几次)。
I am trying to flash different colours onto the screen at regular intervals (a few times per second).
要更改我的主要观点的背景颜色,我用 Drawable.setColorFilter(INT颜色,模式模式)
:
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);
有关调试的目的,我又说,我换用另一种观点认为 View.setBackgroundColor(INT颜色)
。
For debugging purposes, I added another view that I change using View.setBackgroundColor(int color)
.
在问题是, setColorFilter()
电话是工作棒棒堂,但都是破在previous版本(具体的Nexus 7 v4.4.4,Galaxy Nexus的V4.2.1)。
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).
我所说的色彩变化在code进行的Runnable
由一个触发定期处理程序
。
I call the colour changing code inside a Runnable
that is triggered at regular intervals by a Handler
.
的处理程序被调用所有平台上(我可以看到背景的变化,由于调试 setBackgroundColor()
电话)。
The handler is being called on all platforms (I can see the background changes due to the debugging setBackgroundColor()
calls).
下面是颜色循环code:
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);
}
}
我已经尝试了不同的 PorterDuff.Mode
值 - 仍无法得到它的工作在Android 4
I have tried different PorterDuff.Mode
values - still can't get it working on Android 4.
什么是Android的V4和V5,将改变方式之间的不同 setColorFilter()
和
What is different between Android v4 and v5 that would change the way setColorFilter()
works?
最后,好像问题是,奇巧不支持使用ColorFilter(或隐含阿尔法)上绘制对象将依次在那StateListDrawable。我的解决方案是使用相同的,以code,构建复杂的绘制对象,然后渲染成一个简单的BitMapDrawable:
Ultimately, it seems like the problem is that KitKat doesn't support using a ColorFilter (or implicitly an alpha) on a Drawable that will in turn be in a StateListDrawable. My solution was to use the same to code to construct the complex Drawable and then render that into a simple BitMapDrawable:
static Drawable createDrawable(Context context, int color, boolean disabled) {
OvalShape oShape = new OvalShape();
ShapeDrawable background = new ShapeDrawable(oShape);
background.getPaint().setColor(color);
ShapeDrawable shader = new ShapeDrawable(oShape);
shader.setShaderFactory(new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
return new LinearGradient(0, 0, 0, height,
new int[]{
Color.WHITE,
Color.GRAY,
Color.DKGRAY,
Color.BLACK
}, null, Shader.TileMode.REPEAT);
}
});
Drawable icon = ContextCompat.getDrawable(context, R.drawable.ic_chat_button).mutate();
icon.setColorFilter(context.getResources().getColor(R.color.control_tint_color), PorterDuff.Mode.SRC_IN);
Drawable layer = new LayerDrawable(new Drawable[]{ shader, background, icon });
layer.setAlpha(disabled ? 128 : 255);
// Note that on KitKat, setting a ColorFilter on a Drawable contained in a StateListDrawable
// apparently doesn't work, although it does on later versions, so we have to render the colored
// bitmap into a BitmapDrawable and then put that into the StateListDrawable
Bitmap bitmap = Bitmap.createBitmap(icon.getIntrinsicWidth(), icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
layer.setBounds(0, 0, layer.getIntrinsicWidth(), layer.getIntrinsicHeight());
layer.draw(canvas);
return new BitmapDrawable(context.getResources(), bitmap);
}