Android开发_控制硬加速hardwareAccelerated
控制硬加速 hardwareAccelerated 也是一种优化的手段
从Android3.0 (API level11)开始,Android的2D显示管道被被设计得更加支持硬加速了.硬加速使用GPU承担了所有在View的canvas上执行的绘制操作.
启用硬加速最简单的的方法是对整个应用启用硬件速.如果你的应用只使用标准的view和Drawable,全局启用硬加速将不会带来任何负面影响.然而,因为硬加速不是被所有的2D绘制所支持,所以启用它时可能对你的自定义绘制产生影响.出现的问题经常是不可见的,也可能是异常,或错误地显示了像素.为了避免这些问题,Android提供了在以下各级别上启用或禁止硬加速的能力:
Application
Activity
Window
View
如果你的应用使用了自定义绘制,需要在真实的设备上启用硬加速进行测试来找出问题."不支持的绘制操作"一节描述了已知的不能被硬加速的绘制操作以及如何避开它们.
控制硬加速
你可以在以下级别上分别控制硬加速:
Application
Activity
Window
View
Application级
在你的manifest文件中,把以下属性添加到<application>标签来对你的整个应用启用硬加速:
<applicationandroid:hardwareAccelerated="true" ...>
Activity级
如果你的应用在全局启用硬加速时行为不正确,你可以对个别activities单独启用硬加速.欲在actvity级别启用或禁止硬加速,你可以对<activity>元素使用android:hardwareAccelerated属性.下面的例子在整个应用中启用了硬加速但对一个activity禁止了硬加速:
<applicationandroid:hardwareAccelerated="true">
<activity... />
<activityandroid:hardwareAccelerated="false" />
</application>
Window级
如果你需要更高颗粒度的控制,你可以使用以下代码为一个window启用硬加速:
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
注:现在你还不能在window级别禁止硬加速.
Viewlevel
你可以在运行时使用以下代码禁止个别的View的硬加速:
myView.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
注:当前你不能在View级别启用硬加速.View层有除禁止硬加速之外的其它功能.
判定一个View是否能被硬加速
有时一个应用了解是否启用了硬件速是很有用的,对那些自定义View之类的东西尤其重要.在你的应用做了一些不被最新的管线所支持的自定义绘制时这更加重要.
有两种方法可以检查应用是否被硬加速:
View.isHardwareAccelerated():如果View附加到一个硬加速的window上就返回true.
Canvas.isHardwareAccelerated():如果Canvas被硬加速了就返回true.
如果你必须在你的绘制代码中做这个,应使用Canvas.isHardwareAccelerated()而不是View.isHardwareAccelerated().当一个view附加到一个硬加速的window上,它仍可以使用非硬件速的Canvas进行绘制操作.比如当为了高速缓存而把一个view画到一个bitmap中.
You can control hardware acceleration at the following levels:
In your Android manifest file, add the following attribute to the <application>
tag to enable hardware acceleration for your entire application:
<application android:hardwareAccelerated="true" ...>
If your application does not behave properly with hardware acceleration turned on globally, you can control it for individual activities as well. To enable or disable hardware acceleration at the activity level, you can use theandroid:hardwareAccelerated
attribute for the <activity>
element. The following example enables hardware acceleration for the entire application but disables it for one activity:
<application android:hardwareAccelerated="true"> <activity ... /> <activity android:hardwareAccelerated="false" /></application>
If you need even more fine-grained control, you can enable hardware acceleration for a given window with the following code:
getWindow().setFlags( WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
Note: You currently cannot disable hardware acceleration at the window level.
You can disable hardware acceleration for an individual view at runtime with the following code:
myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Note: You currently cannot enable hardware acceleration at the view level. View layers have other functions besides disabling hardware acceleration. See View layers for more information about their uses.