在设备上动画 View 时 Android WebView 闪烁错误

标签 android android-emulator android-webview android-animation

我正在尝试实现滑入式导航(类似于 Facebook、Path 或 Google+ Android 应用程序)。重现此错误的完整源代码 can be found here .

然而,当 WebView 动画离开屏幕时,在设备上会发生闪烁,就好像设备在将它关闭之前正在咬我的 WebView屏幕。

WebView slide nav menu IN (device)

模拟器上没有出现神器!动画进行得很顺利。

WebView slide nav menu IN (emulator)

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

TextView slide nav menu IN (device)

WebView Activity 的布局:

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

对于 TextView Activity :

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

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

 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);
    }

然后将其滑出:

    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);
    }

我把代码上传到BitBucket作为公共(public)存储库,因此您可以像我一样尝试这个项目。

任何关于为什么会发生这种情况或如何解决它的帮助或想法将不胜感激!所需要的是平滑地将 WebView 动画移出图片并返回。谢谢!

最佳答案

一般来说,问题似乎与在 3.0+ 设备上启用硬件加速的 WebViews 时的错误有关。我也尝试使用 sliding menu带有 WebView 的库,但遇到了同样的闪烁问题。环顾四周后,我在这里找到了解决方法:

WebView “flashing” with white background if hardware acceleration is enabled (Android 3.0+)

变通方法使用以下代码将 WebView 的图层类型设置为软件渲染:

webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

这段代码以较低的性能为我解决了闪烁问题。

关于在设备上动画 View 时 Android WebView 闪烁错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12806094/

相关文章:

未调用 Android MotionLayout 转换监听器

安卓 SDK。无法启动 AVD

android - 使 Android 模拟器适用于 1600x1200

android - 模拟器 : incompatible HAX module version 3 requirs minimum version 4

android - Google Maps Android API V2 不在模拟器上显示 map

android - 在将网页加载到 WebView 之前尝试解析网页的 JSON

android - WebView : how to avoid security alert from Google Play upon implementation of onReceivedSslError

Android WebView 内容在继续放大/缩小时中断(呈现问题)

Android Studio - Android SDK 工具 25.2.4 更新失败

java - 静态计数(Android)