问题
i m using videoview in portrait mode and have set this in manifest
<activity android:name="com.ui.VideoDetailActivity" android:configChanges="orientation|screenSize|keyboard" android:theme="@style/newtheme"
/>
my layout is working fine in portrait mode but in landscape i do not want to load another layout from layout-land because this will start buffering of video from start position so i m trying to change views programtically but this is not working as expected.Layout for portrait :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<RelativeLayout
android:id="@+id/rlayout_detailvideo"
android:layout_width="match_parent"
android:layout_height="250dp"
android:visibility="visible"
android:background="#000000">
<com.controls.VideoViewCustom android:id="@+id/videoplayer"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:id="@+id/imageview_videodetailbackarrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/video_detail_back_btn"
android:padding="15dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="@dimen/videodetail_backarrow_marginleft"
android:layout_marginTop="@dimen/videodetail_backarrow_margintop"/>
<ImageView
android:id="@+id/imageview_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/video_detail_edit_btn"
android:padding="15dp"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/videodetail_edit_marginright"
android:layout_marginTop="@dimen/videodetail_backarrow_margintop"/>
<ImageView
android:id="@+id/imageview_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/video_detail_fullscreen_btn"
android:padding="15dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/videodetail_edit_marginright"
android:layout_marginTop="@dimen/videodetail_backarrow_margintop"/>
<LinearLayout
android:id="@+id/layout_videocontrols"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:background="#000000">
<SeekBar
android:id="@+id/seekbar_video"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:progressDrawable="@drawable/seek_bar"
android:thumb="@drawable/thumb"
android:layout_margin="3dp"
android:minHeight="5dip"
android:maxHeight="8dip"
android:progress="0"
android:indeterminate="false"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/videodetail_videocontrol_height"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageview_playpause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/video_detail_play_btn"
android:layout_gravity="center_vertical"
android:layout_marginRight="@dimen/videodetail_edit_marginright"
android:padding="5dp"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="@+id/textview_videoduration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:layout_gravity="center_vertical"
android:textSize="@dimen/videodetail_textview_videoduration_fontsize"
android:textColor="@color/white"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<!-- <android.support.design.widget.TabLayout
android:id="@+id/videodetail_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabIndicatorColor="@color/actionbar_backgroundcolor"
app:tabIndicatorHeight="3dp"
android:layout_below="@+id/rlayout_detailvideo"
android:background="#ffffff"
/>-->
<android.support.design.widget.TabLayout
android:id="@+id/videodetail_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@color/actionbar_backgroundcolor"
app:tabIndicatorHeight="3dp"
app:tabGravity="fill"
android:layout_below="@+id/rlayout_detailvideo"
android:background="#ffffff"/>
<View
android:id="@+id/view_videodetail"
android:layout_height="0.3dp"
android:layout_width="match_parent"
android:background="@android:color/darker_gray"
android:layout_below="@+id/videodetail_tabs"
/>
<android.support.v4.view.ViewPager
android:id="@+id/videodetail_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_below="@+id/view_videodetail"
android:background="@color/white"
android:layout_marginTop="@dimen/infotab_layout_margintop"
/>
</RelativeLayout>
in landscape i want to show videoview as full screen with seekbar layout on top of videoview and everything should be hidden but this is not working.
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) //To fullscreen
{
tabsVideoDetail.setVisibility(View.GONE);
pagerVideoDetail.setVisibility(View.GONE);
view.setVisibility(View.GONE);
seekBarLayout.setVisibility(View.GONE);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
android.widget.RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) videoView.getLayoutParams();
params.width = metrics.widthPixels;
params.height = metrics.heightPixels;
params.leftMargin = 0;
videoView.setLayoutParams(params);
}
else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
tabsVideoDetail.setVisibility(View.VISIBLE);
pagerVideoDetail.setVisibility(View.VISIBLE);
view.setVisibility(View.VISIBLE);
seekBarLayout.setVisibility(View.VISIBLE);
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);
android.widget.LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) videoView.getLayoutParams();
params.width = metrics.widthPixels;
params.height = (int) (250*metrics.density);
videoView.setLayoutParams(params);
}
}
this is producing this layout while seekbar layout should stay on bottom everytime
.
in landscape i gone every view and give red background this indicates that its not taking full height how to avoid this problem ?
回答1:
The problem is that you are just changing the params of your videoview
videoView.setLayoutParams(params);
You should also change the container's params in which your all other buttons are.
i.e. android:id="@+id/rlayout_detailvideo"
so in my opinion you should try this on your Configuration.ORIENTATION_LANDSCAPE
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
android.widget.RelativeLayout.LayoutParams params = new android.widget.RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT);
videoView.setNewDimension(metrics.widthPixels, metrics.heightPixels);
videoView.getHolder().setFixedSize(metrics.heightPixels,
metrics.widthPixels);
rlayout_detailvideo.setLayoutParams(params);
回答2:
It's too late but for future Reference :
1- Add following to Manifest.xml :
android:configChanges="orientation|keyboardHidden|screenSize"
2- In you Activity Override onConfiguration changed as follow :
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (null == emVideoView) return;
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().getDecorView().invalidate();
float height = getWidthInPx(this);
float width = getHeightInPx(this);
emVideoView.getLayoutParams().height = (int) width;
emVideoView.getLayoutParams().width = (int) height;
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
final WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setAttributes(attrs);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
float width = getWidthInPx(this);
float height = dip2px(this, 200.f);
emVideoView.getLayoutParams().height = (int) height;
emVideoView.getLayoutParams().width = (int) width;
}
}
3- Put these function someWhere :
public static final float getHeightInPx(Context context) {
final float height = context.getResources().getDisplayMetrics().heightPixels;
return height;
}
public static final float getWidthInPx(Context context) {
final float width = context.getResources().getDisplayMetrics().widthPixels;
return width;
}
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
来源:https://stackoverflow.com/questions/32651695/video-not-taking-full-screen-in-landscape-mode-using-videoview-android