2013-03-09 45 views
7

我希望能够显示一个按钮以启动一个视频,该视频集中在视频将播放的同一视图中(使用VideoView)。我也希望按钮在点击后消失,因为我使用MediaController类在视频启动后执行暂停,后退,快进操作。在Android中的VideoView上显示和隐藏播放按钮

我该怎么做?

这里的布局我到目前为止:

<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,但似乎并没有工作。

+0

如果我理解正确这,是你想放置VideoView内按钮提拉? – crazylpfan 2013-03-09 01:27:46

+0

并非像VideoView那么多。 – 2013-03-09 01:49:38

回答

9

确定,这里就是我解决了这个。布局文件非常简单。只需添加一个ImageButton的后VideoView元素:

<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" 

此属性中心在的FrameLayout中的ImageButton。

下一个技巧是让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); 
      } 
     } 
    }); 
0

而不是使用按钮,请尝试使FrameLayout Clickable

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

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

+0

感谢您的建议!我的确在考虑让框架可点击,但我需要给用户一些指示,说明FrameLayout是可操作的。所以我仍然需要在VideoView上绘制一些东西。为什么不是一个按钮?对于将ViewVisibility()调用到View.GONE,您是正确的。请参阅上面的自我回答。 – 2013-03-09 01:43:21

-1

有叫前景可绘制FrameLayout一个鲜为人知的功能。您可以指定一个将在所有FrameLayout子项的顶部上呈现的drawable。所以:

mFrameLayout.setForegroundDrawable(mPlayDrawable); 

会做的伎俩,你的布局将更有效率(更少的意见)。

可以使用重力常数正确对齐

mFrameLayout.setForegroundGravity(Gravity.XXXX)