Can Android WebView make hardware-accelerated translationX animations?

I read somewhere that hardware acceleration should work for all built-in Android views; however, when I try to make a slide animation using WebView, all web content becomes empty for the duration of the animation.

I will write a small application, so if you want to see it, you can try. The application will do slide animations every time a page loads in WebView. We will navigate between ImageView and WebView. ImageView will contain a screenshot of WebView itself. Paste the following code in onCreate for your activity:

final WebView myWebView = (WebView) findViewById(R.id.webview); myWebView.loadUrl("http://www.google.com"); final ImageView myImageView = (ImageView) findViewById(R.id.imageView); myWebView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { myWebView.setDrawingCacheEnabled(true); myWebView.buildDrawingCache(); myImageView.setImageBitmap(Bitmap.createBitmap(myWebView.getDrawingCache())); myWebView.setDrawingCacheEnabled(false); myImageView.setVisibility(View.VISIBLE); myWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null); ObjectAnimator webViewAnimator = ObjectAnimator.ofFloat(myWebView, "translationX", myWebView.getWidth(), 0); webViewAnimator.setDuration(SLIDE_DURATION); ObjectAnimator imageViewAnimator = ObjectAnimator.ofFloat(myImageView, "translationX", 0, -myWebView.getWidth()); imageViewAnimator.setDuration(SLIDE_DURATION); AnimatorSet set = new AnimatorSet(); set.addListener(new AnimatorListenerAdapter() { public void onAnimationEnd(Animator animation) { myWebView.setLayerType(View.LAYER_TYPE_NONE, null); myImageView.setVisibility(View.GONE); } }); set.playTogether(webViewAnimator, imageViewAnimator); set.start(); } } 

Make sure you have android: hardwareAccelerated = "true" in your Manifest.xml

Also in your main_layout.xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" /> <ImageView android:id="@+id/imageView" android:contentDescription="@string/image_view_description" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone" /> </RelativeLayout> 

Now every time you load a page, there is a slide transition between ImageView (containing a screenshot of WebView) and WebView itself.

The error cannot be reproduced in the JellyBean simulator, however this happens on all tested ICS / JellyBean devices ...

Any understanding of this problem would be greatly appreciated. Note that although I can simply turn off hardware acceleration in WebView by setting the Layer Type to LAYER_TYPE_SOFTWARE and this will eliminate the “blanking” problem, I would like to make hardware acceleration faster due to the benefits of rendering.

+4
source share

Source: https://habr.com/ru/post/1441062/


All Articles