質問

I need to enable hardware acceleration on one of my WebViews. So far, I've found out that if I build my project with a target API of 11 or 13 (Android 3.0 and 3.2, respectively), hardware acceleration gets enabled and everything's fine. But the weird part is that when I build my project with API 17 or 18, all my efforts to turn on hardware acceleration get ignored for some reason.

So far I've tried:

1) Setting android:hardwareAccelerated="true" in my tag in the manifest

2) Same, but for the activity containing the WebView

3) The following code in my Activity's onCreate (before setting content view):

getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

4) The same, but using getWindow().addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

In all cases, webView.isHardwareAccelerated() returns false, even when it's working.

Please help, it would be pretty lame to be stuck on API 13 because of this...

役に立ちましたか?

解決

Tested on API 18:

Perform the check by posting a Runnable to the WebView:

WebView webView = (WebView) findViewById(R.id.webview_id);

// Shows: "Is hardware accelerated? false"
// Toast.makeText(YourActivity.this, "Is hardware accelerated? " + 
                                             // webView.isHardwareAccelerated(),
                                                 // Toast.LENGTH_LONG).show();

webView.post(new Runnable() {

    @Override
    public void run() {
        Shows: "Is hardware accelerated? true"
        Toast.makeText(YourActivity.this, "Is hardware accelerated? " + 
                                              webView.isHardwareAccelerated(),
                                                  Toast.LENGTH_LONG).show();

        if (webView.isHardwareAccelerated()) {
            // isAccelerated();
        } else {
            // isNotAccelerated();
        }
    }
});

From resource page on Hardware Acceleration:

Hardware acceleration is enabled by default if your Target API level is >=14, but can also be explicitly enabled.

If the image you are displaying is still pixelated, try setting:

webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

and see if pixelation of the image gets better.

Consider reading the resource page on Hardware Acceleration.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top