我有一个 Ionic 项目,我想在其中显示视频,并在其之上显示 cordova 的内容 WebView
.拥有透明WebView
在视频 View 之上,在我使用的插件中:
webView.getView().setBackgroundColor(0x00000000);
((ViewGroup) webView.getView()).bringToFront();
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) {}
});
}
我已经在 Android 4.4.2 上测试过了,效果很好。然后我在 Android 5.1.1 和 WebView
上测试了它播放视频时不可见。但是如果我使用 Chrome 检查它并将鼠标悬停在 <body>
上例如(然后屏幕显示覆盖蓝色),然后出现 html 内容中的元素。另外,如果我触摸屏幕,内容就会突然出现。因此,当与 WebView
没有交互时,这似乎是一个渲染问题。 ,但一旦 at 收到触摸或某种触发,它就会出现。
我也尝试过使用 Crosswalk,它在 4.2.2 上仍然运行良好,但仅在 5.1.1 上启动应用程序时有效,然后在第一次交互后仅在触摸屏幕时有效
我还设置了<preference name="BackgroundColor" value="0xff000000"/>
在config.xml
,但这不会改变行为。
编辑 1:
我在其中添加了一个带有一些透明文本的 div,并在其上放置了一个简单的无限动画(只是将不透明度从 .9
更改为 1
),瞧 WebView
中的元素现在播放时出现在视频的顶部。它并不像有时 WebView
那样完美内容会“闪烁”,但它确实有效。仍然不知道为什么,尽管
最佳答案
用下面的代码替换。
webView.setBackgroundColor(Color.TRANSPARENT);
如果你想在视频 View 之上添加 webview, 使用应用程序内容布局并在其中添加 webview,如下所示。
FramlayoutLayout layout = (FramlayoutLayout )cordova.getActivity() findViewById(android.R.id.content);
layout.addview(your webview)
关于Android 透明 WebView 在 VideoView 之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42777413/