画布不显示在自定义视图全部取材部位?画布、自定义、视图、部位

2023-09-03 22:01:46 作者:不爽你去喝爽歪歪阿

我工作的一个自定义视图作为一个Android应用程序,类似于模拟仪表样品code可从Mind机器人。

I'm working on a custom view for an android application, similar to the Analog Gauge sample code available from Mind the Robot.

从上市现场运行code,我得到这个看我的屏幕上:

Running the code from listed site, I get see this on my screen:

(摩托罗拉Droid,2.2.3),(模拟器,4.0.3)

(Motorola Droid, 2.2.3), (Emulator, 4.0.3)

(Xoom的,4.0.3)(对方电话,4.0.3)

(Xoom, 4.0.3)(Other phone, 4.0.3)

手缺少!

目前正在绘图电话(我可以看到他们在logcat中),但画布元素调用画是看不见的。

The drawing calls are being made (I can see them in logcat), but the canvas elements the calls draw are invisible.

这是不API级别依赖,虽然;如果我将其导入正确的方式进入一个项目,将手会出现在我的Xoom的运行。

It's not API level dependent, though; if I import it the right way into a project, it will hand will show up when I run it on the Xoom.

但是,当我将文件移动到不同的项目文件夹(相同的源$ C ​​$ C,同样的布局),它可以追溯到缺少拨盘。

But, when I move the files to a different project folder (same source code, same layouts) it goes back to missing the dial.

这是怎么回事?怎么可以在同一code是在不同设备上生产这种不同的结果?

What's going on? How could the same code be producing such different outcomes on different devices?

推荐答案

所以,在我的奥秘的关键线索似乎是,它的工作在模拟器上,而不是在硬件设备。

So, the key clue in my mystery seemed to be that it worked on the emulator, but not on the hardware devices.

我没有细读对Android开发者网站上的硬件渲染页面,但显然不够紧密。

I did peruse the hardware rendering page on the Android Developer's website, but apparently not closely enough.

http://developer.android.com/guide/topics/graphics/hardware-accel.html

虽然未提及该API的可开始11版,它不说,硬件渲染处于打开状态默认情况下所有的应用程序,从API等级14(ICS)

While it does mention that the API's are available beginning version 11, it does not say that Hardware Rendering is turned on for all applications by default, starting with API Level 14 (ICS).

这是什么意思为我们呢?

What does this mean for us?

几乎所有的东西比较快;除了不工作了几件事情。

Almost everything is faster; except for the few things that don't work.

我设法破坏其中的两个,没有意识到这一点:

I managed to violate two of these, without realizing it:

Canvas.DrawTextOnPath() Paint.setShadowLayer()

这不是在API参考中提到(或任何其他地方我能找到的,肯定不是林特选中),但使用任何列出的操作都可以做奇怪的事情。

It's not mentioned in the API reference (or anywhere else I can find, and certainly not checked by Lint), but using any of the listed operations can do weird things.

在我的情况下,Canvas.DrawTextOnPath()似乎工作就好了。

In my case, Canvas.DrawTextOnPath() seemed to work just fine.

但是当Android的通知,说我用手中的油漆有阴影图层组,它忽略了。

从上面的文档链接:

有两种不同的方法来检查应用程序是否是硬件加速:

There are two different ways to check whether the application is hardware accelerated:    View.isHardwareAccelerated()如果View附属于硬件加速的窗口,返回true。    Canvas.isHardwareAccelerated()如果画布硬件加速返回true    View.isHardwareAccelerated() returns true if the View is attached to a hardware accelerated window. Canvas.isHardwareAccelerated() returns true if the Canvas is hardware accelerated

如果你必须做此检查图形中的code,使用Canvas.isHardwareAccelerated()来代替> View.isHardwareAccelerated()时可能。当一个视图被附连到一个硬件>加速窗口,它仍可以使用非硬件加速画布绘制。此>发生,例如,拉拔一个视图成位图用于高速缓存目的时

If you must do this check in your drawing code, use Canvas.isHardwareAccelerated() instead >of View.isHardwareAccelerated() when possible. When a view is attached to a hardware >accelerated window, it can still be drawn using a non-hardware accelerated Canvas. This >happens, for instance, when drawing a view into a bitmap for caching purposes.

在我的情况,相反似乎已经发生了。 自定义视图日志,这不是硬件加速;然而,在画布报告它是硬件加速。

In my case, the opposite appears to have occurred. The custom view logs that it is not Hardware-accelerated; however, the canvas reports that it is hardware-accelerated.

最简单的修补程序迫使自定义视图做软件渲染。每文档这可以通过以下方式实现:

The simplest fix is forcing the custom view to do software rendering. Per the documentation this can be accomplished by:

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

另外,你可以删除有问题的操作,并保持硬件渲染打开。

Alternatively, you could remove the offending operations, and keep hardware rendering turned on.

这是我的不幸了解。祝你好运,所有的。

Learn from my misfortune. Good luck, all.