在设备上制作动画视图时,Android WebView会出现闪烁错误

Rob*_*arl 5 android android-emulator android-animation android-webview

我正在尝试实现幻灯片导航(Facebook,Path或Google+ Android应用程序).可以在此处找到重现此错误的完整源代码.

WebView然而,当在屏幕上设置动画时,在设备上会发生闪烁,就好像设备WebView在将其从屏幕上移开之前咬了一口.

WebView幻灯片导航菜单IN(设备)

模拟器上不会发生工件!动画进展顺利.

WebView幻灯片导航菜单IN(模拟器)

有趣的是,这种闪烁似乎只发生在动画视图是一个WebView!这是使用TextView作为主要内容视图的相同项目.

TextView幻灯片导航菜单IN(设备)

Web视图活动的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue" >

    <WebView
        android:id="@+id/web_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

对于文本视图活动:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue" >

    <TextView
        android:id="@+id/web_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello there. no flicker!"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
         />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

调用代码以滑动导航菜单:

 private void slideIn() {
        LinearLayout actionBarFrame = (LinearLayout) findViewById(android.R.id.content).getParent();

        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        menuView = inflater.inflate(R.layout.menu, null);
        menuView.setLayoutParams(new FrameLayout.LayoutParams(SIZE, LayoutParams.MATCH_PARENT, Gravity.LEFT));
        FrameLayout decorView = (FrameLayout) getWindow().getDecorView();
        decorView.addView(menuView);
        decorView.bringChildToFront(actionBarFrame);

        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) actionBarFrame.getLayoutParams();
        params.setMargins(SIZE, 0, -SIZE, 0);
        actionBarFrame.setLayoutParams(params);

        TranslateAnimation ta = new TranslateAnimation(-SIZE, 0, 0, 0);
        ta.setDuration(DURATION); 
        actionBarFrame.startAnimation(ta);
    }
Run Code Online (Sandbox Code Playgroud)

并将其滑回:

    private void slideOut() {
        LinearLayout actionBarFrame = (LinearLayout) findViewById(android.R.id.content).getParent();

        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) actionBarFrame.getLayoutParams();
        params.setMargins(0, 0, 0, 0);
        actionBarFrame.setLayoutParams(params);

        TranslateAnimation ta = new TranslateAnimation(SIZE, 0, 0, 0);
        ta.setDuration(DURATION);
        ta.setAnimationListener(new  AnimationListener() {
            @Override 
            public void onAnimationEnd(Animation arg0) {
                ((FrameLayout) getWindow().getDecorView()).removeView(menuView);
                menuView = null;
            }
            @Override public void onAnimationRepeat(Animation arg0) { }
            @Override public void onAnimationStart(Animation arg0) { }
        });
        actionBarFrame.startAnimation(ta);
    }
Run Code Online (Sandbox Code Playgroud)

我将代码作为公共仓库上传到BitBucket,所以你可以像我一样尝试这个项目.

任何帮助或想法为什么会发生这种情况或如何解决它将不胜感激!所需要的是平滑地动画WebView出画面并重新进入.谢谢!

Ste*_*son 5

一般来说,在使用WebViews3.0+设备上启用硬件加速时,问题似乎与错误有关.我也试过使用滑动菜单WebView但遇到了同样的闪烁问题.环顾四周后,我找到了一个解决方法:

如果启用了硬件加速,WebView将以"白色背景闪烁"(Android 3.0+)

解决方法使用以下代码来设置WebView软件渲染的图层类型:

webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Run Code Online (Sandbox Code Playgroud)

这段代码解决了我的闪烁,具有性能较低的缺点.