在Android VideoView上绘制叠加层(HUD)?

问题描述 投票:7回答:5

我有一个自定义视图,绘制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的项目是DroidPlanner您可以尝试克隆并查看问题。 (需要手动添加VideoView,因为它不在repo中)。
android video-streaming android-videoview rtsp ondraw
5个回答
2
投票

我找到了你的问题的答案:VideoView on top of SurfaceView SurfaceView和VideoView都是表面视图,并且不支持在旧版本的Android中重叠这些,但是从2.0开始可用。

你需要做什么:

  1. 将pixelformat设置为TRANSPARENT,以便让视频通过界面闪烁

hudWidget.getHolder().setFormat(PixelFormat.TRANSPARENT);

  1. 将HUD曲面设置为媒体覆盖。

hudWidget.setZOrderMediaOverlay(true);

  1. 创建一个新曲面并将其设置为播放视频的MediaPlayer的显示。

有拉动请求,您可以尝试这些。 https://github.com/arthurbenemann/droidplanner/pull/247

带来解决方案的线程:https://groups.google.com/forum/?fromgroups#!msg/android-developers/nDNQcceRnYA/ps9wTBfXIyEJ


1
投票

来自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);

1
投票

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

0
投票

测试FrameLayout以替换RelativeLayout。

也许它运作良好。


0
投票

扩展VideoView并在其构造函数中调用setWillNotDraw(false)。这样你就可以画onDraw(Canvas)了。

© www.soinside.com 2019 - 2024. All rights reserved.