我有一个自定义视图,绘制HUD:
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<VideoView
android:id="@+id/videoView1"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.widgets.HUD
android:id="@+id/hud"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.hud_fragment, container, false);
frameLayout = (FrameLayout) view.findViewById(R.id.frameLayout);
hudWidget = (HUD) view.findViewById(R.id.hudWidget);
videoView = (VideoView) view.findViewById(R.id.videoView1);
videoView.setVideoURI(Uri.parse("http://88.150.210.138:5001/spor"));
videoView.start();
frameLayout.removeView(hudWidget);
frameLayout.addView(hudWidget);
hudWidget.bringToFront();
return view;
}
一旦视频开始播放,我就会在我的VideoView上播放RTSP流,如下所示:
如何强制HUD在VideoView上绘制? HUD extends SurfaceView
我找到了你的问题的答案:VideoView on top of SurfaceView SurfaceView和VideoView都是表面视图,并且不支持在旧版本的Android中重叠这些,但是从2.0开始可用。
你需要做什么:
hudWidget.getHolder().setFormat(PixelFormat.TRANSPARENT);
hudWidget.setZOrderMediaOverlay(true);
有拉动请求,您可以尝试这些。 https://github.com/arthurbenemann/droidplanner/pull/247
带来解决方案的线程:https://groups.google.com/forum/?fromgroups#!msg/android-developers/nDNQcceRnYA/ps9wTBfXIyEJ
来自FrameLayout
的文档可在此处获取:Link
子视图以堆栈形式绘制,最近添加的子项位于顶部
将id
添加到布局文件中的FrameLayout
。在你的onCreate(Bundle)
中,你会有类似的东西:
// find your framelayout
frameLayout = (FrameLayout) findViewById(....);
videoView = (VideoView) findViewById(R.id.videoView1);
hudView = (com.widgets.HUD) findViewById(R.id.hud);
视频开始后,请执行以下操作:
frameLayout.removeView(hudView);
frameLayout.addView(hudView);
每50或100毫秒制作一个Handler
invalidate
你的hudView
像这样:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.hud_fragment, container, false);
frameLayout = (FrameLayout) view.findViewById(R.id.frameLayout);
hudWidget = (HUD) view.findViewById(R.id.hudWidget);
videoView = (VideoView) view.findViewById(R.id.videoView1);
videoView.setVideoURI(Uri.parse("http://88.150.210.138:5001/spor"));
videoView.start();
frameLayout.removeView(hudWidget);
frameLayout.addView(hudWidget);
hudWidget.bringToFront();
//The Handler which invalidate your (hudWidget) every 50 milliseconds
new Handler() {
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
hudWidget.invalidate();
sendEmptyMessageDelayed(0, 50);
};
}.sendEmptyMessage(0);
return view;
}
测试FrameLayout以替换RelativeLayout。
也许它运作良好。
扩展VideoView
并在其构造函数中调用setWillNotDraw(false)
。这样你就可以画onDraw(Canvas)
了。