画布没有在自定义视图中显示所有绘制的部分? [英] Canvas not displaying all drawn parts in Custom View?

查看:32
本文介绍了画布没有在自定义视图中显示所有绘制的部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Android 应用程序开发一个自定义视图,类似于

虽然它确实提到 API 从版本 11 开始可用,但并没有说 从 API 级别 14 (ICS) 开始默认为所有应用程序启用硬件渲染..p>

这对我们意味着什么?

几乎一切都更快;除了一些不起作用的东西.

我在不知不觉中违反了其中两个:

  • Canvas.DrawTextOnPath()
  • Paint.setShadowLayer()

API 参考中没有提到(或者我能找到的任何其他地方,当然也没有被 Lint 检查过),但是使用任何列出的操作都会做一些奇怪的事情.

就我而言,Canvas.DrawTextOnPath() 似乎工作得很好.

但当 Android 注意到我在手上使用的油漆设置了阴影层时,它会默默地忽略它.

我如何知道我的 View 是否经过硬件加速?

来自上面的文档链接:

<块引用>

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

  • View.isHardwareAccelerated() 如果视图附加到硬件加速窗口,则返回 true.
  • Canvas.isHardwareAccelerated() 如果 Canvas 是硬件加速的,则返回 true

如果您必须在绘图代码中执行此检查,请尽可能使用 Canvas.isHardwareAccelerated() 而不是 View.isHardwareAccelerated().当视图被附加到硬件>加速窗口时,它仍然可以使用非硬件加速的 Canvas 进行绘制.>例如,当将视图绘制到位图中以进行缓存时,就会发生这种情况.

在我的情况下,似乎发生了相反的情况.自定义视图记录它不是硬件加速的;但是,画布报告它是硬件加速的.

解决方法和修复

最简单的解决方法是强制自定义视图进行软件渲染.根据文档,这可以通过以下方式完成:

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

或者,您可以删除有问题的操作,并保持打开硬件渲染.

从我的不幸中吸取教训.祝大家好运.

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

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

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

(Xoom, 4.0.3)(Other phone, 4.0.3)

The hand is missing!

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

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.

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

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.

Hardware Rendering

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

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()

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.

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

But when Android notice that the paint that I used on the hand had shadow layer set, it silently ignored it.

How do I know if my View is hardware accelerated?

From the documentation link above:

There are two different ways to check whether the application is hardware accelerated:

  • View.isHardwareAccelerated() returns true if the View is attached to a hardware accelerated window.
  • Canvas.isHardwareAccelerated() returns true if the Canvas is hardware accelerated

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.

Work Arounds and Fixings

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.

这篇关于画布没有在自定义视图中显示所有绘制的部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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