视频没有使用videoview android在横向模式下全屏显示

时间:2015-09-18 12:08:20

标签: android xml android-linearlayout nine-patch

我在纵向模式下使用videoview并在清单中设置了这个

<activity android:name="com.ui.VideoDetailActivity" android:configChanges="orientation|screenSize|keyboard"   android:theme="@style/newtheme"
            />

我的布局在纵向模式下工作正常但在横向上我不想从layout-land加载另一个布局,因为这将从开始位置开始缓冲视频,所以我试图以编程方式更改视图但这不能按预期工作.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>

在横向中,我希望将视频视频显示为全屏,并在视频视图的顶部显示搜索栏布局,并且所有内容都应该隐藏,但这不起作用。

  @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

see landscape layout portrait layout

在风景中我走了每一个视图并给出红色背景这表明它没有采取全高度如何避免这个问题?

2 个答案:

答案 0 :(得分:0)

问题在于您只是更改视频视图的参数

 videoView.setLayoutParams(params);

您还应该更改所有其他按钮所在的容器参数。 即android:id="@+id/rlayout_detailvideo"

因此,我认为您应该在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);

答案 1 :(得分:0)

现在为时已晚,但未来参考:

1-在Manifest.xml中添加以下内容:

android:configChanges="orientation|keyboardHidden|screenSize"

2-在您的活动覆盖onConfiguration中更改如下:

@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-将这些功能放在某处:

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);
    }