在VideoView上安装android透明WebView

Gui*_*ume 8 android transparent webview android-videoview

我有一个Ionic项目,我想要显示一个视频,并在其上面显示cordova的内容WebView.要WebView在视频视图的顶部透明,我在插件中使用:

webView.getView().setBackgroundColor(0x00000000);
((ViewGroup) webView.getView()).bringToFront();
Run Code Online (Sandbox Code Playgroud)

VideoView被初始化,并添加在FrameLayout这样的:

FrameLayout containerView = (FrameLayout) cordova.getActivity().findViewById(1);

if (containerView == null) {
  containerView = new FrameLayout(cordova.getActivity().getApplicationContext());
  containerView.setId(1);
  containerView.setBackgroundColor(Color.BLACK);

  FrameLayout.LayoutParams containerLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
  cordova.getActivity().addContentView(containerView, containerLayoutParams);

  videoView = new VideoView(cordova.getActivity());
  videoView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
  containerView.addView(videoView);

  MediaPlayer player = new MediaPlayer();
  player.setOnPreparedListener(this);
  player.setOnCompletionListener(this);
  player.setOnErrorListener(this);

  final SurfaceHolder holder = videoView.getHolder();
  holder.setKeepScreenOn(true);
  holder.addCallback(new SurfaceHolder.Callback() {
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
      player.setDisplay(holder);
      try {
        player.prepare();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
      player.release();
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
  });
}
Run Code Online (Sandbox Code Playgroud)

我已经在Android 4.4.2上测试了这个并且效果很好.我已经在Android 5.1.1上测试了它,并且WebView在视频播放时不可见.但是如果我使用Chrome来检查它并将其悬停在<body>例如(然后屏幕显示覆盖蓝色),则会显示html内容中的元素.此外,如果我触摸屏幕,则内容突然出现.因此,当没有与之交互时,它似乎是一个渲染问题WebView,但是一旦接到触摸或某种触发,它就会出现.

我也尝试过使用Crosswalk,它在4.2.2上仍能完美运行,但只能在5.1.1启动应用程序时使用,然后在第一次交互后才能在触摸屏幕时起作用

我也设置<preference name="BackgroundColor" value="0xff000000"/>config.xml,但这并没有改变行为.

编辑1:我添加了一个div,其中包含一些透明文本,并在其上放置一个简单的无限动画(刚刚将不透明度更改.91),并且WebView当播放时,现在显示在视频顶部的元素.它并不完美,因为有时WebView内容会"眨眼",但确实有效.尽管如此,仍然不知道为什么

Key*_*mar -1

将代码替换为以下代码。

webView.setBackgroundColor(Color.TRANSPARENT);  
Run Code Online (Sandbox Code Playgroud)

如果您想在视频视图之上添加 webview,请使用应用程序内容布局并在其中添加 webview,如下所示。

FramlayoutLayout layout = (FramlayoutLayout )cordova.getActivity() findViewById(android.R.id.content);
layout.addview(your webview)
Run Code Online (Sandbox Code Playgroud)