zoukankan      html  css  js  c++  java
  • 硬件加速 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、整个应用 Application 层,可以选择打开或关闭
    <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
    • 绘制不正确或者抛出异常:可能使用了不支持硬件加速的操作, 需要关闭硬件加速或者绕过该操作




  • 相关阅读:
    【转】c++ http下载文件
    unity事件
    【Unity3D自学记录】判断物体是否在镜头内
    Unity3D研究院之获取摄像机的视口区域
    Unity3D特效-场景淡入淡出
    Unity3D深入浅出
    胶囊碰撞体(CapsuleCollider)
    unity Dotween插件的简单介绍及示例代码
    使用Animation实现摄像机动画
    Unity3D研究院之使用Animation编辑器编辑动画
  • 原文地址:https://www.cnblogs.com/baiqiantao/p/3e64ac2dcabc216b2cbb91d5d4e46d53.html
Copyright © 2011-2022 走看看