1

我注意到WebView在 Android 3.x 中加载包含大量 javascript 内容(如 JQuery)的页面会导致性能非常缓慢。我搜索并发现设置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)。

4

1 回答 1

2

在您的活动中添加这样的检查:

if (Build.VERSION.SDK_INT >= 19) {
    // chromium, enable hardware acceleration
    webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
    // older android version, disable hardware acceleration
    webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
于 2016-07-06T08:43:02.783 回答