作者:李旺成
時間:2016年5月21日
這個 Hack 將介紹當屏幕旋轉時使視頻全屏顯示。
要實現的效果
一般的,手機用戶在觀看視頻的時候都喜歡切換為橫屏模式,全屏觀看。先演示一下這里想要實現的效果:
效果是這樣的,當把手機橫屏的時候,自動將視頻播放界面全屏(上面的動圖只是簡單的處理,沒有真正實現全屏)。當然有些應用為了更好的用戶體驗,專門提供了一個全屏視頻的按鈕,點擊該按鈕,就可以切換為橫屏全屏。
簡單實現
要實現上述效果應該很簡單,我們先來理理思路:首先,我們得知道當前屏幕處于什么狀態(豎屏還是橫屏);然后,監聽屏幕狀態切換事件,做出響應。
思路理清了,下面看看怎么實現。
創建布局
先看布局的效果:
上面的布局分為上下兩部分,以一條橫線分割。上面的部分左側顯示一個稍小的文本,可以上下滑動,右側顯示一個視頻(這里用 View 模擬的);下面的部分顯示一長段描述信息。
代碼如下(activity_videoview.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/main_portrait_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:orientation="horizontal" >
<ScrollView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/par_01" />
</ScrollView>
<View
android:id="@+id/main_portrait_position"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="#ffffff" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#ffffff" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="2dp"
android:layout_weight="0.75" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/par_02" />
</ScrollView>
</LinearLayout>
<VideoView
android:id="@+id/main_videoview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</RelativeLayout>
好了,布局有了,下面要解決如何在 Activity 中處理屏幕旋轉。
響應屏幕旋轉
Activity 可以響應屏幕旋轉,但是需要做一些配置。
1、在清單文件中配置
要讓 Activity 能夠處理屏幕旋轉,需要在清單文件中為該 Activity 設置 configChanges 屬性。設置該屬性后,當屏幕旋轉的時候,系統不會重新調用 onCreate() 方法,而是調用 onConfigurationChanged() 方法。
<activity android:name=".VideoViewActivity"
android:configChanges="orientation"/>
2、判斷當前屏幕狀態
在 Activity 中可以通過如下方式獲取當前屏幕狀態:
getResources().getConfiguration().orientation
看看源碼中的 orientation:
/**
* Overall orientation of the screen. May be one of
* {@link #ORIENTATION_LANDSCAPE}, {@link #ORIENTATION_PORTRAIT}.
*/
public int orientation;
看一下源碼中的注釋應該就知道怎么用這個 orientation 了吧!
3、屏幕旋轉處理
重寫 Activity 中屏幕旋轉時的回調方法:
@Override
public void onConfigurationChanged(Configuration newConfig) {
setVideoViewPosition();
super.onConfigurationChanged(newConfig);
}
根據屏幕的旋轉狀態改變布局:
private void setVideoViewPosition() {
// 判斷當前屏幕狀態
switch (getResources().getConfiguration().orientation) {
case Configuration.ORIENTATION_LANDSCAPE: {
mPortraitContent.setVisibility(View.GONE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
mVideoView.setLayoutParams(params);
break;
}
case Configuration.ORIENTATION_SQUARE:
case Configuration.ORIENTATION_UNDEFINED:
case Configuration.ORIENTATION_PORTRAIT:
default: {
mPortraitContent.setVisibility(View.VISIBLE);
int[] locationArray = new int[2];
mPortraitPosition.getLocationOnScreen(locationArray);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
mPortraitPosition.getWidth(), mPortraitPosition.getHeight());
params.leftMargin = locationArray[0];
params.topMargin = locationArray[1];
mVideoView.setLayoutParams(params);
break;
}
}
}
代碼很簡單了,就是根據屏幕的狀態,顯示或隱藏相應布局,設置布局參數。
小結
這個簡單的示例,只是希望當你遇到要針對橫豎屏做不同處理的場景時,可以提供一個思路。