Android:带硬件加速的WebView只显示白屏

时间:2012-07-10 10:25:27

标签: android android-webview hardware-acceleration

我注意到在WebView上加载包含JQuery等巨大javascript内容的页面导致Android 3.x中的性能非常低。我搜索过并发现将android:hardwareAccelerated设置为true会更好。

但是,执行此操作后,webview只显示白屏,没有。 以下是我用来测试的内容:

public class TestWebViewWithHAActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        WebView webView = (WebView) findViewById(R.id.web);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setRenderPriority(RenderPriority.HIGH);
        webView.getSettings().setPluginState(PluginState.ON_DEMAND);
        webView.loadUrl("http://www.google.com");        
}

并在清单

中的应用程序级别设置android:hardwareAccelerated="true"

部分日志显示渲染错误:

07-10 17:30:44.847 D/libEGL  (2417): loaded /system/lib/egl/libGLES_android.so
07-10 17:30:44.857 D/libEGL  (2417): loaded /system/lib/egl/libEGL_VIVANTE.so
07-10 17:30:44.877 D/libEGL  (2417): loaded /system/lib/egl/libGLESv1_CM_VIVANTE.so
07-10 17:30:44.907 D/libEGL  (2417): loaded /system/lib/egl/libGLESv2_VIVANTE.so
07-10 17:30:44.947 I/        (2417): visual ID: 1
07-10 17:30:44.947 D/OpenGLRenderer(2417): Creating OpenGL renderer caches
07-10 17:30:44.947 D/OpenGLRenderer(2417): Enabling debug mode 0
07-10 17:30:44.947 D/OpenGLRenderer(2417): Layers will be composited as regions
07-10 17:30:45.067 I/ActivityManager(121): Displayed com.example.testwebviewwithha/.TestWebViewWithHAActivity: +705ms
07-10 17:30:45.567 I/dalvikvm-heap(202): Clamp target GC heap from 48.338MB to 48.000MB
07-10 17:30:45.587 D/dalvikvm(202): GC_EXPLICIT freed 219K, 4% free 47342K/48839K, paused 3ms+5ms
07-10 17:30:46.447 D/ShaderProgram(2417): could not compile shader 35632:
07-10 17:30:46.447 D/ShaderProgram(2417): (1:0) : warning : Extension : GL_OES_EGL_image_external is not provided by this compiler.
07-10 17:30:46.447 D/ShaderProgram(2417): (2:0) : error : Extension GL_OES_EGL_image_external do not support 'require'.
07-10 17:30:46.447 D/ShaderProgram(2417): (2:0) : error : Error in parsing.
07-10 17:30:46.447 D/ShaderProgram(2417): (8:0) : error : syntax error
07-10 17:30:46.447 D/ShaderProgram(2417): 
07-10 17:30:46.447 D/ShaderProgram(2417): couldn't load the pixel shader!
07-10 17:30:46.457 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.467 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.487 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.517 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.527 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.537 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.557 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.577 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.607 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.617 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.647 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.657 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.677 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.687 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.707 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.737 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.747 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.767 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.777 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.807 D/GLUtils (2417): GL ERROR - after glBindTexture() glError (0x501)
07-10 17:30:46.817 D/OpenGLRenderer(2417): GL error from OpenGLRenderer: 0x501
07-10 17:30:46.837 D/GLUtils (2417): GL ERROR - after glBindTexture() glError (0x501)
07-10 17:30:46.837 D/GLUtils (2417): GL ERROR - after drawQuad() glError (0x501)

造成这种情况的原因是什么?

当然,如果我不使用硬件加速,它可以正常工作。但这与我的目的相矛盾。

我还在Android 3.0设备的模拟器上进行了测试。我工作正常,但我怀疑android:hardwareAccelerated="true"是否真的生效了。 难道只是我的设备无法支持?我正在使用Ainol Novo 7 Basic平板电脑(Android 3.2)。

1 个答案:

答案 0 :(得分:0)

在您的活动中添加如下支票:

if (Build.VERSION.SDK_INT >= 19) {
    // chromium, enable hardware acceleration
    websetting.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
    // older android version, disable hardware acceleration
    websetting.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}