硬件加速 Hardware Accelerated 繪制


官方文檔中對硬件加速的描述


Beginning in Android 3.0 (API level 11), the Android 2D rendering pipeline渲染引擎 supports hardware acceleration, meaning that all drawing operations操作 that are performed執行 on a View's canvas use the GPU. Because of the increased增加 resources required to enable hardware acceleration, your app will consume消耗 more RAM.

Hardware acceleration is enabled by default if your Target API level is >=14, but can also be explicitly明確的 enabled. If your application uses only standard views and Drawables, turning it on globally全局 should not cause any adverse不利的 drawing effects. However, because hardware acceleration is not supported for all of the 2D drawing operations, turning it on might affect some of your custom views自定義控件 or drawing calls. Problems usually manifest themselves as表現為 invisible elements, exceptions, or wrongly rendered pixels. To remedy修改 this, Android gives you the option to enable or disable hardware acceleration at multiple levels多種級別.

If your application performs custom drawing, test your application on actual hardware devices with hardware acceleration turned on to find any problems. The Unsupported drawing operations section章節 describes known issues已知的問題 with hardware acceleration and how to work around繞開、避免 them.

四個層次級別的硬件加速

1、整個應用 A pplication 層, 可以選擇打開或關閉
<application android:hardwareAccelerated="true">
2、Activity 層 可以選擇打開或關閉
<activity android:hardwareAccelerated="true">
3、Window 層,只能打開(必須在setContentView之前調用)
getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, //
	WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
4、View 層,只能關閉。也可在XML使用 android:layerType="software" 來關閉硬件加速
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
注意:在apk的AndroidManifest中,如果指定了minSDKVersion & targetSDKVersion=7,會使得應用無法使用硬件加速進行繪圖。

如何判斷一個View是否啟用了硬件加速

有兩種 方式
  • 使用View的isHardwareAccelerated()
    • 如果View掛在一個開啟了硬件加速的Window之下,返回true。也就是說,它在繪制的時候不一定使用了硬件加速。
    • returns true if the View is attached to a hardware accelerated window.
  • 使用Canvas的isHardwareAccelerated()
    • 如果canvas在繪制的時候啟用了硬件加速,返回true
    • 建議在draw的代碼塊中使用此方法來判斷是否開啟了硬件加速,因為如果一個View被attach到一個硬件加速的Window上,即使沒有硬件加速的Canvas,它也是可以被繪制的。比如:將一個View以bitmap的形式進行緩存
    • returns true if the Canvas is hardware accelerated.

Android中的硬件加速

硬件加速能使用GPU來加速2D圖像的渲染速度,但是硬件加速並不能完全支持所有的渲染操作, 針對自定義的View,硬件加速可能導致渲染出現錯誤。 如果有自定義的View,需要在硬件加速的設備上進行測試,如果出現渲染的問題,需要關閉硬件加速。

不過Android可以保證內置的組件和應用支持硬件加速,因此,如果應用中只使用了標准UI組件,可以放心開啟硬件加速。

隨着Android的版本升級,相信一段時間之后,硬件加速可以得到完美的支持。

開啟硬件加速之后可能的異常反應:
  • 某些UI元素沒有顯示或者沒有更新:可能是沒有調用invalidate
  • 繪制不正確或者拋出異常:可能使用了不支持硬件加速的操作, 需要關閉硬件加速或者繞過該操作





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM