VideoView在SurfaceView之上

时间:2011-03-25 14:06:02

标签: android visibility surfaceview android-videoview

我尝试在VideoView之上显示SurfaceView。但它不可见但会对点击作出反应(MediaController出现并播放声音)。该视频似乎是在SurfaceView后面播放的,所以我也尝试使用setZOrderMediaOverlay()和/或setZOrderOnTop(),但没有改变

当我进入主屏幕时,我会在淡入淡出的动画中看到VideoView一瞬间,所以它确实存在。

我尝试使用xml布局,也完全采用编程方式,但没有任何效果。

这是我的活动:

public class VideoTest extends Activity {
    private RelativeLayout mLayout;
    private VideoView mVideo;
    private Handler mHandler;
    private FrameLayout mFrameLayout;
    private SurfaceView mSurfaceView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mFrameLayout = new FrameLayout(this);
        mFrameLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        mLayout = new RelativeLayout(this);
        mLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        mVideo = new VideoView(this);
        mVideo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        mSurfaceView = new SurfaceView(this);
        mSurfaceView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        mSurfaceView.setZOrderMediaOverlay(false);
        mSurfaceView.setZOrderOnTop(false);

        mFrameLayout.addView(mSurfaceView);
        mLayout.addView(mVideo);
        mFrameLayout.addView(mLayout);

        setContentView(mFrameLayout);

        // with xml
//        setContentView(R.layout.main);
//        mFrameLayout = (FrameLayout) findViewById(R.id.activity_reader);
//        mLayout = (RelativeLayout) findViewById(R.id.videoview);
//        mVideo = (VideoView) findViewById(R.id.video_view);
//        mSurfaceView = (SurfaceView) findViewById(R.id.surface_view);

        // Set the handler to me messaged from the threads
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg != null) {
                    mVideo.setMediaController(new MediaController(mVideo.getContext()));
                    mVideo.setVideoPath("/mnt/sdcard/myvideo.mp4");
                    if (mLayout.getVisibility() == View.VISIBLE) {
                        mLayout.setVisibility(View.GONE);
                    } else {
                        mLayout.setVisibility(View.VISIBLE);
                    }
//                    mFrameLayout.bringChildToFront(mLayout);
//                    mFrameLayout.bringChildToFront(mVideo);
//                    mFrameLayout.requestLayout();
//                    mFrameLayout.invalidate();
//                    mFrameLayout.postInvalidate();
//                    mVideo.requestFocus();
                }
                super.handleMessage(msg);
            }
        };
    }

    /** Inflate the menu */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.activity_reader_menu, menu);
            return true;
    }

    /** Handle menu items events */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.toggle_video:
                new Thread() {
                    public void run() {
                        mHandler.sendEmptyMessage(0);
                    };
                }.start();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

我的xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_reader"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <SurfaceView android:id="@+id/surface_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    <RelativeLayout android:id="@+id/videoview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone">
        <VideoView android:id="@+id/video_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
</FrameLayout>

一个提示:因为这只是一个测试,以找出它无法正常工作的原因我试图最小化我的实际应用程序中的代码,其中VideoView的可见性由获取消息的处理程序控制来自SurfaceView的绘图线程。这就是我在使用菜单时实现发送消息的线程的原因。

1 个答案:

答案 0 :(得分:3)

根据Android框架工程师的回答this thread,我认为你不会有重叠的SurfaceViews:

  

抱歉,你不能这样做 - 因为   表面视图非常特殊而不是   真的看法(表面是一个   Z-ordered与你的单独窗口   他们的Z排序不匹配   你的意见。表面视图很大,   重物;你不打算   将SurfaceView视为常规视图   就这样。