【问题标题】:Find out if "Force GPU-Rendering" is enabled查看是否启用了“强制 GPU 渲染”
【发布时间】:2012-03-31 02:07:12
【问题描述】:

由于 Ice-Cream-Sandwich 有一个开发者选项,称为“强制 GPU 渲染”。如果启用,它会拒绝显示一些大的 Drawable。因此,我想知道,如果启用了此选项,则通知用户他必须将其关闭,如果他想查看该可绘制对象。

【问题讨论】:

    标签: android rendering gpu android-4.0-ice-cream-sandwich


    【解决方案1】:

    找到一个你知道不应该加速的视图,如果你添加它应该是任何视图

    android:hardwareAccelerated="false"
    

    到您的 在 Android Manifest 中,然后在您的代码中调用

    view.isHardwareAccelerated();
    

    如果它返回 true,则该选项设置为 on。这已被证实适用于我的 Galaxy Nexus。

    【讨论】:

      【解决方案2】:

      在 Kai 的帮助下,我在 android-developers 上找到了这个 Hardware Acceleration 主题。不幸的是,我们希望与 2.1 保持兼容,所以我为遇到类似问题的任何人添加了我的解决方案。所以在一个Activity里面:

      public View contentView
      
      public void onCreate(Bundle savedInstanceState){
      
          contentView = findViewById(R.id.someId);
          //initialize Views ...
          setContentView(contentView);
      
          //use a handler as easiest method to post a Runnable Delayed.
          //we cannot check hardware-acceleration directly as it will return reasonable results after attached to Window.
          Handler handler = new Handler();
          handler.postDelayed(HardwareAccelerationRunnable(), 500);
      }
      
      public class HardwareAccelerationRunnable implements Runnable{
           public void run(){
          //now lets check for HardwareAcceleration since it is only avaliable since ICS.
          // 14 = ICS_VERSION_CODE
          if (android.os.Build.VERSION.SDK_INT >= 14){
              try{
                  //use reflection to get that Method
                  Method isHardwareAccelerated = contentView.getClass().getMethod("isHardwareAccelerated", null);
                  Object o = isHardwareAccelerated.invoke(contentView, null);
                  if (null != o && o instanceof Boolean && (Boolean)o){
                      //ok we're shure that HardwareAcceleration is on.
                      //Now Try to switch it off:
                      Method setLayerType = contentView.getClass().getMethod("setLayerType", int.class, android.graphics.Paint.class);
                      setLayerType.invoke(contentView, 1, null);
                  }
              } catch(Exception e){
                  e.printStackTrace();
              }
      
          }
           }  
      }
      
      
      
      }
      

      【讨论】:

      • 我的应用程序的 minSdk 版本是 7,与 3.0 以下的版本相比,我的应用程序在带有 ICS 的 nexus 上运行流畅,因为 min sdk 是 7,所以我想以编程方式打开 gpu 渲染选项,如果它已经没有启用。如果 ICS 使用相同的 apk,我该怎么做?
      • 请记住,我想在应用程序级别启用渲染,而不是在视图级别。
      【解决方案3】:

      我不认为你可以通过添加来关闭它

      android:hardwareAccelerated="false"
      

      如果将代码跟踪到 Window.setWindowManager() 中,可以看到以下内容

      public void setWindowManager(...) {
      ...
      mHardwareAccelerated = hardwareAccelerated
                      || SystemProperties.getBoolean(PROPERTY_HARDWARE_UI, false); 
      ...
      }
      

      在哪里,

      hardwareAccelerated:来自android:hardwareAccelerated

      PROPERTY_HARDWARE_UI 属性由“强制 GPU 渲染”选项设置。

      您可以看到,如果用户手动选中“强制 GPU 渲染”选项,无论 android:hardwareAccelerated 是什么,都会为 mHardwareAccelerated 变量分配 TRUE 值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 2019-08-17
        • 2021-02-12
        • 2014-04-10
        • 1970-01-01
        • 2015-05-21
        • 1970-01-01
        相关资源
        最近更新 更多