安卓videoView 横屏,全屏显示

时间:2020-12-20 09:23:10

 

videoView的原码中对videoView播放的视频做了一定的处理导致视频不能按你以为的形式呈现在videoView中。

首先重写VideoView

   <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <com.sxy.view.CustomVideoView
                android:id="@+id/videoView"
                android:layout_width="match_parent"
                android:layout_height="230dip" />
        </RelativeLayout>

public class CustomVideoView extends VideoView {

 private int mVideoWidth;
 private int mVideoHeight;

 public CustomVideoView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
 }

 public CustomVideoView(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
 
 }

 public CustomVideoView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  // TODO Auto-generated constructor stub
 }

 @Override
 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  
  int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
  int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
  setMeasuredDimension(width, height);
 }
}

其实就是屏蔽掉了系统的VideoView的一些控制方法,让视频的播放的长宽是根据你设置的参数来决定

 然后,控制点击方法来实现横竖屏

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置videoView全屏播放
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//设置videoView横屏播放
     WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
     android.view.Display display = wm.getDefaultDisplay();
     int height_01 = display.getHeight();
     int width_01 = display.getWidth();
     RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) videoView
       .getLayoutParams(); // 取控当前的布局参数
     layoutParams.height = height_01;//设置 当控件的高强
     layoutParams.width = width_01;  
     videoView.setLayoutParams(layoutParams); // 使设置好的布局参数应用到控件

好了视频的横屏播放就完成了,棒棒哒!