2013-03-09 125 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) 
相關問題