Android4.0 开启硬件加速后部分应用运行出错。


出现异常:


12-20 15:18:19.543: E/AndroidRuntime(26301): FATAL EXCEPTION: main
12-20 15:18:19.543: E/AndroidRuntime(26301): java.lang.UnsupportedOperationException
12-20 15:18:19.543: E/AndroidRuntime(26301): at android.view.GLES20Canvas.clipPath(GLES20Canvas.java:429)
12-20 15:18:19.543: E/AndroidRuntime(26301): at cn.hpc.ui.MyView.drawArea(MyView.java:66)


关闭硬件加速则运行正常。


原因在这里

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

不支持的绘图方法:

  • Canvas
    • clipPath()
    • clipRegion()
    • drawPicture()
    • drawTextOnPath()
    • drawVertices()
  • Paint
    • setLinearText()
    • setMaskFilter()
    • setRasterizer()
  • Xfermodes
    • AvoidXfermode
    • PixelXorXfermode
我的应用中,正好用到第一种 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中使用到上述硬件加速不支持的方法时,强制关闭硬件加速。其它地方,由系统决定是否硬件加速。




更多相关文章

  1. androidSDK无法更新的解决方法之一
  2. TextView中ellipsize属性 + 走马灯效果
  3. android全屏去掉title栏的多种实现方法
  4. View类的XML属性、相关方法及说明
  5. android 中隐藏EditText的下划线方法
  6. android:configChanges属性
  7. ImageView的scaletype属性
  8. Android调用系统默认浏览器访问的方法

随机推荐

  1. Linux用户和组的操作(一) 用户文件/etc/pas
  2. Linux命令应用大词典-第21章 LVM和RAID管
  3. Linux网络状态工具ss命令使用详解
  4. [置顶] Linux C编程--string.h函
  5. Ubuntu12挂载扩充/home
  6. Linux文件映射的反思
  7. [转帖]linux文件描述符文件/etc/security
  8. 重装linux,从ubuntu到centos
  9. 双插槽与单插槽内存模型?
  10. Linux socket通信——并发服务器(fork)