控制硬加速 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中.

 

 

 

Controlling Hardware Acceleration


You can control hardware acceleration at the following levels:

  • Application
  • Activity
  • Window
  • View
Application level

In your Android manifest file, add the following attribute to the <application> tag to enable hardware acceleration for your entire application:


<span class="tag" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 136);"><application</span><span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="atn" style="list-style: none; margin: 0px; padding: 0px; color: rgb(136, 34, 136);">android:hardwareAccelerated</span><span class="pun" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="atv" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"true"</span><span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> ...</span><span class="tag" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">></span>


Activity level

android:hardwareAccelerated attribute for the <activity> element. The following example enables hardware acceleration for the entire application but disables it for one activity:

<span class="tag" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 136);"><application</span><span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="atn" style="list-style: none; margin: 0px; padding: 0px; color: rgb(136, 34, 136);">android:hardwareAccelerated</span><span class="pun" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="atv" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"true"</span><span class="tag" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">></span><span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">     </span><span class="tag" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 136);"><activity</span><span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> ... </span><span class="tag" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">/></span><span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">     </span><span class="tag" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 136);"><activity</span><span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="atn" style="list-style: none; margin: 0px; padding: 0px; color: rgb(136, 34, 136);">android:hardwareAccelerated</span><span class="pun" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="atv" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"false"</span><span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="tag" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">/></span><span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="tag" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 136);"></application></span>


Window level

If you need even more fine-grained control, you can enable hardware acceleration for a given window with the following code:


<span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">getWindow</span><span class="pun" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">().</span><span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">setFlags</span><span class="pun" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">     </span><span class="typ" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">WindowManager</span><span class="pun" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="typ" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">LayoutParams</span><span class="pun" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">FLAG_HARDWARE_ACCELERATED</span><span class="pun" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">     </span><span class="typ" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">WindowManager</span><span class="pun" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="typ" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">LayoutParams</span><span class="pun" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">FLAG_HARDWARE_ACCELERATED</span><span class="pun" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span>


Note: You currently cannot disable hardware acceleration at the window level.

View level

You can disable hardware acceleration for an individual view at runtime with the following code:


<span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">myView</span><span class="pun" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">setLayerType</span><span class="pun" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="typ" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 0, 102);">View</span><span class="pun" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);">LAYER_TYPE_SOFTWARE</span><span class="pun" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="list-style: none; margin: 0px; padding: 0px; color: rgb(0, 0, 136);">null</span><span class="pun" style="list-style: none; margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span>