Android 4.1.1上的ComposeShader问题 [英] Issue with ComposeShader on Android 4.1.1

查看:376
本文介绍了Android 4.1.1上的ComposeShader问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是试图为我的Android应用程序实现一个颜色选择器,并遇到一个奇怪的问题在Android 4.1.1。以下代码不会在Android 4.1.1上创建预期的渐变,但它在2.3.7上创建:

I'm just trying to implement a color picker for my android application, and ran into a strange issue on Android 4.1.1. The following code does not create the expected gradients on Android 4.1.1, but it does on 2.3.7:

Shader fadeInRight = new LinearGradient(0, 0, pWidth, 0, 0x00000000, 0xFF000000, Shader.TileMode.CLAMP);
Shader blackToWhite = new LinearGradient(0, 0, 0, pHeight, 0xFF000000, 0xFFFFFFFF, Shader.TileMode.CLAMP);
Shader whiteMask = new ComposeShader(blackToWhite, fadeInRight, PorterDuff.Mode.DST_IN);
Shader blackToColor = new LinearGradient(0, 0, pWidth, 0, 0xFF000000, hue, Shader.TileMode.CLAMP);
Shader shader = new ComposeShader(blackToColor, whiteMask, PorterDuff.Mode.SCREEN);
paint.setShader(shader);
...
canvas.drawRect(new Rect(0, 0, pWidth, pHeight), paint);

这里有问题:

(忽略Android上的红色框4.1.1。我只是谈论上面的渐变)

任何想法出了什么问题?我的代码,但我不知道,什么。

Any Idea what's wrong? I think there is something missing in my code, but I don't have any idea, what.

编辑#1:
如果我只是使用 whiteMask setShader ,我也得到不同的结果两个系统:在2.3.7我可以看到配置文本菜单(在弹出窗口的后面)照亮渐变矩形。此外,从左上边缘到右下边缘(黑到白)有一个梯度,但是4.1.1上的梯度从左到右水平。 所以,这似乎是 ComposeShader

EDIT #1: If I just use whiteMask for setShader, I also get different results for both systems: On the 2.3.7 I can see the text of the configuration menu (which is behind the popup) shining through the gradient rectangle. Furthermore there is a gradient from the upper left edge to the lower right edge (black to white), but the gradient on the 4.1.1 goes horizontally from left to right. So it seems to be a problem with ComposeShader

EDIT#2 :
我发现一个更简单的例子来描述这个问题:

EDIT #2: I found a more simpler example to describe the problem:

Shader shader1 = new LinearGradient(0, 0, 0, pHeight, hue, 0xffffffff, Shader.TileMode.CLAMP);
Shader shader2 = new LinearGradient(0, 0, pWidth, 0, 0xff000000, 0xffffffff, Shader.TileMode.CLAMP);
Shader shader = new ComposeShader(shader1, shader2, PorterDuff.Mode.MULTIPLY);

所以,我们只有一个从任何颜色到黑色的垂直线性渐变,到白色。如果我们乘这两个层,我们应该得到正确的矩形(如上面的左截图)。但在Android 4.1.1我只看到shader2的渐变。我还在Gimp中尝试了这两层,结果与Android 2.3.7相同。

So, we have just a vertical linear gradient from any color to black and a horizontal linear gradient from black to white. If we multiply these two layers, we should get the correct rectangle (as in the left screenshot above). But on Android 4.1.1 I see only the gradient of shader2. I also tried this in Gimp with two layers and the result was the same as on Android 2.3.7.

推荐答案

发现问题:看来它与硬件加速有关。如此处所述 ComposeShader 只能包含不同类型的着色器(例如,BitmapShader和LinearGradient,但不包含BitmapShader的两个实例)'。但是,如果您的应用程序受到任何这些缺失的功能或限制的影响,您可以通过调用 setLayerType(View.LAYER_TYPE_SOFTWARE,null)关闭应用程序的受影响部分的硬件加速,

Found the problem: Seems that it has to do with the hardware acceleration. As described here 'ComposeShader can only contain shaders of different types (a BitmapShader and a LinearGradient for instance, but not two instances of BitmapShader)'. But, 'If your application is affected by any of these missing features or limitations, you can turn off hardware acceleration for just the affected portion of your application by calling setLayerType(View.LAYER_TYPE_SOFTWARE, null).'

我看到,此方法自SDK 11以来可用。我的应用程序支持从SDK 7开始的所有版本,所以我必须检查方法是否可用:

I saw, that this method is available since SDK 11. My App supports all versions starting with SDK 7, so I have to check if the method is available:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

现在一切正常了。

这篇关于Android 4.1.1上的ComposeShader问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆