-1

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...

npace
  • 4,218
  • 1
  • 25
  • 35
  • Whoever downvoted, could you please explain why? I have no idea what I've done wrong... – npace Jan 25 '16 at 12:38

1 Answers1

4

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.

Vikram
  • 51,313
  • 11
  • 93
  • 122
  • Thanks for the answer. Interestingly enough, if I log webView.isHardwareAccelerated() before posting the runnable, it returns false. Inside the runnable, it returns true. That doesn't help though, because hw acceleration is still clearly off - the image I'm displaying is pixelated. (Note: when building with target API 11 or 13, the image is not pixelated.) I tried setting the window's hw acceleration flags and then invalidating the webView in the runnable, but it still didn't work. – npace Sep 02 '13 at 07:53
  • 1
    @npace `because hw acceleration is still clearly off` Actually, no. 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.` It's a long shot, but try setting `webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);` and see if pixelation of the image gets better. – Vikram Sep 02 '13 at 08:05
  • Wow. That actually worked... I constantly get amazed by Android's little quirks like that, although they make polishing your product a complete pain. Please edit your answer to include that bit about the software layer type so I can accept it, etc. By the way, do you have any idea why setting the layer type to "software" worked? – npace Sep 02 '13 at 08:16
  • `I constantly get amazed by Android's little quirks like that...` Man, tell me about it. Anyway, I updated my answer. `By the way, do you have any idea why setting the layer type to "software" worked?` I included a link to the resource page on Hardware Acceleration. Take a look at it. One of the sections is `Unsupported Drawing Operations`. I tried looking through android.webkit.webview's source code, but could not find an example that explains why the `long shot` suggestion worked. Sorry about that. – Vikram Sep 02 '13 at 09:33
  • I can confirm, setting target API to 14 makes the trick, also, isHardwareAccelerated() has to be called in runnable posted to webView. – MartinC Feb 27 '14 at 13:56