Android7.0 自定義view開啓硬件加速報錯

Android7.0 開啓硬件加速後部分應用運行出錯。

canvas繪製從網絡獲取圖片報錯

出現異常:

5-10 02:07:53.362 1675-1758/system_process W/WindowAnimator: Failed to dispatch window animation state change.
                                                              android.os.DeadObjectException
                                                                  at android.os.BinderProxy.transactNative(Native Method)
                                                                  at android.os.BinderProxy.transact(Binder.java:503)
                                                                  at android.view.IWindow$Stub$Proxy.onAnimationStopped(IWindow.java:534)
                                                                  at com.android.server.wm.WindowAnimator.updateWindowsLocked(WindowAnimator.java:286)
                                                                  at com.android.server.wm.WindowAnimator.animateLocked(WindowAnimator.java:678)
                                                                  at com.android.server.wm.WindowAnimator.-wrap0(WindowAnimator.java)


關閉硬件加速則運行正常。


原因在這裏

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

Hardware Acceleration


Beginning in Android 3.0 (API level 11), the Android 2D rendering pipeline is designed to better support hardware acceleration. 

從Android 3.0(API Level 11)開始,Android的2D渲染管線可以更好的支持硬件加速。硬件加速使用GPU進行View上的繪製操作。

... ...


Unsupported Drawing Operations

不支持的繪圖方法:

我的應用中,正好用到第一種 clipPath.所以運行出錯。

問題找到,

解決方法:

有4種控件硬件加速的方法。


1 Application level

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

在應用程序AndroidManifest.xml文件中,爲application標籤添加如下的屬性即可爲整個應用程序true開啓、false關閉硬件加速

<application android:hardwareAccelerated="false" ...> 


2  Activity level

在應用程序AndroidManifest.xml文件中,只需在activity元素中添加android:hardwareAccelerated屬性即可。
例:在application級別開啓硬件加速,但在activity上關閉硬件加速。
<application android:hardwareAccelerated="true">
    <activity ... />
    <activity android:hardwareAccelerated="false" />
</application>

3 Window level

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.


4  View level

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

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);


測試中第1, 2 中都無效,第3種只能開啓硬件加速,而不能關閉硬件加速。

只有第4種適合。只對當前View關閉硬件加速。
優點:View中使用到上述硬件加速不支持的方法時,強制關閉硬件加速。其它地方,由系統決定是否硬件加速。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章