【问题标题】:Show and hide a play button over a VideoView in Android在 Android 中的 VideoView 上显示和隐藏播放按钮
【发布时间】:2013-03-09 01:23:27
【问题描述】:

我希望能够显示一个按钮来开始播放视频,在播放视频的同一视图内居中(使用 VideoView)。我还希望按钮在单击后消失,因为我使用 MediaController 类在视频开始后执行暂停、倒带、快进等操作。

我该怎么做?

这是我目前的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/LinearLayout1"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">

<FrameLayout
  android:id="@+id/video_frame"
  android:layout_width="fill_parent"
  android:layout_height="480px"
  android:background="#000"
  >

  <VideoView
    android:id="@+id/video_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

</FrameLayout>

我尝试以编程方式将 ImageButton 添加到 FrameLayout,但这似乎不起作用。

【问题讨论】:

  • 如果我理解正确,您是否尝试在 VideoView 中放置一个 Button?
  • 在 VideoView 内部不如在它的顶部。

标签: android android-videoview


【解决方案1】:

好的,这就是我解决这个问题的方法。布局文件非常简单。只需在 VideoView 元素之后添加一个 ImageButton:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/LinearLayout1"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical">

<FrameLayout
  android:id="@+id/video_frame"
  android:layout_width="fill_parent"
  android:layout_height="480px"
  android:background="#000"
  >

  <VideoView
    android:id="@+id/video_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

  <ImageButton
    android:id="@+id/play_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal"
    android:src="@drawable/ic_launcher"
    />

</FrameLayout>

FrameLayout 视图元素按照您在布局中定义的顺序将其子元素层叠在一起。因此,布局中添加的最后一个元素被绘制在顶部。注意 ImageButton 有这个属性:

android:layout_gravity="center_vertical|center_horizontal"

此属性使 ImageButton 在 FrameLayout 中居中。

下一个技巧是让 ImageButton 在单击后消失。使用 ImageButton 上的 setVisibility() 方法来执行此操作:

    // Setup a play button to start the video
    mPlayButton = (ImageButton) findViewById(R.id.play_button);
    mPlayButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mPlayer.isPlaying()) {
                resetPlayer();
            } else {
                playVideo(videoUrl, mVideoView.getHolder());
                // show the media controls
                mController.show();
                // hide button once playback starts
                mPlayButton.setVisibility(View.GONE);
            }
        }
    });

【讨论】:

    【解决方案2】:

    FrameLayout 有一个鲜为人知的功能,称为 Foreground Drawable。您可以指定将在所有 FrameLayout 子项的 顶部 呈现的可绘制对象。所以:

    mFrameLayout.setForegroundDrawable(mPlayDrawable);
    

    会成功的,你的布局会更有效率(更少的视图)。

    您可以使用 Gravity 常量正确对齐可绘制对象

    mFrameLayout.setForegroundGravity(Gravity.XXXX)
    

    【讨论】:

      【解决方案3】:

      尝试制作 FrameLayout Clickable,而不是使用按钮。

      或者,您可以在 FrameLayout 下方放置一个 Button,然后在 onClick 事件处理程序中,将 View 的可见性设置为 GONE。

      编辑:或尝试这样的事情:https://stackoverflow.com/a/7511535/826731

      【讨论】:

      • 感谢您的建议!我确实考虑过让 Frame 可点击,但我需要给用户一些指示 FrameLayout 是可操作的。所以我仍然需要在 VideoView 上画一些东西。为什么不是按钮?您对 View.GONE 调用 setVisibility() 是正确的。请参阅上面的我的自我回答。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多