如何在旋转 android 设备时设置全屏视频
how to set full screen video when rotating android device
我正在使用视频视图播放来自外部卡的 MP4 视频文件。当我旋转 android 设备时,视频未全屏显示。正在处理 android 4.4 正常,包括下面的 ui 输出屏幕
当我旋转 phone
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/video"
android:orientation="vertical"
android:background="@drawable/planebackground">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/videotoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="@+id/layout1"
android:layout_width="wrap_content"
android:layout_height="200dp"
>
<VideoView
android:id="@+id/VideoView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout1"
android:weightSum="1">
<TextView
android:text="About Course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="16sp"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp"
android:layout_marginTop="70dp"
android:id="@+id/aboutcuz" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:id="@+id/about_course_text"
android:fontFamily="Arial"
android:layout_below="@+id/aboutcuz"
android:layout_alignLeft="@+id/aboutcuz"
android:layout_alignStart="@+id/aboutcuz"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
android:gravity="center"
/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="2"
android:scrollHorizontally="true"
android:ellipsize="end"
android:id="@+id/coursenamev"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/aboutcuz"
android:layout_alignStart="@+id/aboutcuz"
android:layout_marginTop="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="60dp"
android:background="@android:color/darker_gray"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/aboutcuz"
android:layout_marginTop="10dp"
android:background="@android:color/darker_gray"/>
</RelativeLayout>
</LinearLayout>
我的代码中是否存在任何布局问题
当你创建一个新的"full screen activity"时,simple.You可以找到一个例子。
是这样的:
// Hide UI first
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
请试试这个。我希望这可能会导致您想要的结果。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/video"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/videotoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="200dp"
>
<VideoView
android:id="@+id/VideoView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout1"
android:weightSum="1">
<TextView
android:text="About Course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="16sp"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp"
android:layout_marginTop="70dp"
android:id="@+id/aboutcuz" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:id="@+id/about_course_text"
android:fontFamily="Arial"
android:layout_below="@+id/aboutcuz"
android:layout_alignLeft="@+id/aboutcuz"
android:layout_alignStart="@+id/aboutcuz"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
android:gravity="center"
/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="2"
android:scrollHorizontally="true"
android:ellipsize="end"
android:id="@+id/coursenamev"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/aboutcuz"
android:layout_alignStart="@+id/aboutcuz"
android:layout_marginTop="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="60dp"
android:background="@android:color/darker_gray"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/aboutcuz"
android:layout_marginTop="10dp"
android:background="@android:color/darker_gray"/>
</RelativeLayout>
自定义 VideoView class
public class FullScreenVideoView extends VideoView {
public FullScreenVideoView(Context context) {
super(context);
}
public FullScreenVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FullScreenVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
public void setFixedVideoSize(int width, int height)
{
getHolder().setFixedSize(width, height);
}
}
并在 VideoView
的布局中设置
<yourpackage.FullScreenVideoView
android:id="@+id/VideoView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
做这样的事情
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Logger.debug(TAG, "onConfigChanged");
updateLayout(newConfig.orientation);
}
/**
* Update the current layout params according to current device orientation.
*/
private void updateLayout(int orientation) {
LinearLayout.LayoutParams favParams = (LinearLayout.LayoutParams) mFavoriteLayout.getLayoutParams();
LinearLayout.LayoutParams historyParams = (LinearLayout.LayoutParams) mHistoryLayout.getLayoutParams();
LinearLayout.LayoutParams colParams = (LinearLayout.LayoutParams) mColumnLayout.getLayoutParams();
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
mColumnLayout.setOrientation(LinearLayout.VERTICAL);
historyParams.width = favParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
historyParams.height = favParams.height = 0;
colParams.weight = 1.0f;
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
mColumnLayout.setOrientation(LinearLayout.HORIZONTAL);
historyParams.height = favParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
historyParams.width = favParams.width = 0;
colParams.weight = 2.0f;
}
// Set back updated params and redraw
mFavoriteLayout.setLayoutParams(favParams);
mHistoryLayout.setLayoutParams(historyParams);
mColumnLayout.setLayoutParams(colParams);
mColumnLayout.invalidate();
}
当你改变方向改变重量,你可以达到你想要的。
我正在使用视频视图播放来自外部卡的 MP4 视频文件。当我旋转 android 设备时,视频未全屏显示。正在处理 android 4.4 正常,包括下面的 ui 输出屏幕
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/video"
android:orientation="vertical"
android:background="@drawable/planebackground">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/videotoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="@+id/layout1"
android:layout_width="wrap_content"
android:layout_height="200dp"
>
<VideoView
android:id="@+id/VideoView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout1"
android:weightSum="1">
<TextView
android:text="About Course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="16sp"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp"
android:layout_marginTop="70dp"
android:id="@+id/aboutcuz" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:id="@+id/about_course_text"
android:fontFamily="Arial"
android:layout_below="@+id/aboutcuz"
android:layout_alignLeft="@+id/aboutcuz"
android:layout_alignStart="@+id/aboutcuz"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
android:gravity="center"
/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="2"
android:scrollHorizontally="true"
android:ellipsize="end"
android:id="@+id/coursenamev"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/aboutcuz"
android:layout_alignStart="@+id/aboutcuz"
android:layout_marginTop="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="60dp"
android:background="@android:color/darker_gray"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/aboutcuz"
android:layout_marginTop="10dp"
android:background="@android:color/darker_gray"/>
</RelativeLayout>
</LinearLayout>
我的代码中是否存在任何布局问题
当你创建一个新的"full screen activity"时,simple.You可以找到一个例子。
是这样的:
// Hide UI first
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
请试试这个。我希望这可能会导致您想要的结果。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/video"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/videotoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="200dp"
>
<VideoView
android:id="@+id/VideoView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout1"
android:weightSum="1">
<TextView
android:text="About Course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="16sp"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp"
android:layout_marginTop="70dp"
android:id="@+id/aboutcuz" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:id="@+id/about_course_text"
android:fontFamily="Arial"
android:layout_below="@+id/aboutcuz"
android:layout_alignLeft="@+id/aboutcuz"
android:layout_alignStart="@+id/aboutcuz"
android:layout_marginLeft="43dp"
android:layout_marginStart="43dp"
android:gravity="center"
/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="2"
android:scrollHorizontally="true"
android:ellipsize="end"
android:id="@+id/coursenamev"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/aboutcuz"
android:layout_alignStart="@+id/aboutcuz"
android:layout_marginTop="18dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="60dp"
android:background="@android:color/darker_gray"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/aboutcuz"
android:layout_marginTop="10dp"
android:background="@android:color/darker_gray"/>
</RelativeLayout>
自定义 VideoView class
public class FullScreenVideoView extends VideoView {
public FullScreenVideoView(Context context) {
super(context);
}
public FullScreenVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FullScreenVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
public void setFixedVideoSize(int width, int height)
{
getHolder().setFixedSize(width, height);
}
}
并在 VideoView
的布局中设置<yourpackage.FullScreenVideoView
android:id="@+id/VideoView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
做这样的事情
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Logger.debug(TAG, "onConfigChanged");
updateLayout(newConfig.orientation);
}
/**
* Update the current layout params according to current device orientation.
*/
private void updateLayout(int orientation) {
LinearLayout.LayoutParams favParams = (LinearLayout.LayoutParams) mFavoriteLayout.getLayoutParams();
LinearLayout.LayoutParams historyParams = (LinearLayout.LayoutParams) mHistoryLayout.getLayoutParams();
LinearLayout.LayoutParams colParams = (LinearLayout.LayoutParams) mColumnLayout.getLayoutParams();
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
mColumnLayout.setOrientation(LinearLayout.VERTICAL);
historyParams.width = favParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
historyParams.height = favParams.height = 0;
colParams.weight = 1.0f;
} else if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
mColumnLayout.setOrientation(LinearLayout.HORIZONTAL);
historyParams.height = favParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
historyParams.width = favParams.width = 0;
colParams.weight = 2.0f;
}
// Set back updated params and redraw
mFavoriteLayout.setLayoutParams(favParams);
mHistoryLayout.setLayoutParams(historyParams);
mColumnLayout.setLayoutParams(colParams);
mColumnLayout.invalidate();
}
当你改变方向改变重量,你可以达到你想要的。