public class VideoView extends android.widget.VideoView { /**
-
Android系统在更加xml布局找这个类,并且实例化的时候,用该构造方法实例化
-
@param context
-
@param attrs/ public VideoView(Context context, AttributeSet attrs) { super(context, attrs); }
public VideoView(Context context) { super(context); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); }
/**
-
设置视频画面的宽和高
-
@param videoWidth
-
@param videoHeight/ public void setVideoSize(int videoWidth, int videoHeight) { ViewGroup.LayoutParams layoutParams =getLayoutParams(); layoutParams.width = videoWidth; layoutParams.height = videoHeight; setLayoutParams(layoutParams); }
}
2_在播放器中得到屏幕高和宽方法
//方式一
wm = (WindowManager) getSystemService(WINDOW_SERVICE);screenWidth = wm.getDefaultDisplay().getWidth();screenHeight = wm.getDefaultDisplay().getHeight();
//方式二
DisplayMetrics displayMetrics = new DisplayMetrics();this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);screenWidth = displayMetrics.widthPixels;screenHeight = displayMetrics.heightPixels;
3_视频默认和全屏
一播放起来,在准备好了中设置视频播放默认
/**
-
true全屏
-
flase默认*/ private boolean isFullScreen = false; /**
-
视频全屏和默认
-
@param type*/ public void setVideoType(int type){ switch (type) { case SCREEN_FULL: videoview.setVideoSize(screenWidth, screenHeight); isFullScreen = true; btn_switch_screen.setBackgroundResource(R.drawable.btn_screen_dafult_selector); break;
case SCREEN_DEFULT: //视频的宽 int mVideoWidth = videoWidth; //视频的高 int mVideoHeight = videoHeight; //屏幕的宽 int width = screenWidth; //屏幕的宽 int height = screenHeight; if (mVideoWidth > 0 && mVideoHeight > 0) { if ( mVideoWidth * height > width * mVideoHeight ) { //Log.i("@@@", "image too tall, correcting"); height = width * mVideoHeight / mVideoWidth; } else if ( mVideoWidth * height < width * mVideoHeight ) { //Log.i("@@@", "image too wide, correcting"); width = height * mVideoWidth / mVideoHeight; } else { //Log.i("@@@", "aspect ratio is correct: " + //width+"/"+height+"="+ //mVideoWidth+"/"+mVideoHeight); } }
videoview.setVideoSize(width, height); btn_switch_screen.setBackgroundResource(R.drawable.btn_screen_full_selector); isFullScreen = false;
break; }
}
4_点击按钮的时候实现切换播放模式 case R.id.btn_switch_screen: if(isFullScreen){ setVideoType(SCREEN_DEFUALT); }else{ setVideoType(SCREEN_FULL); } break
5 手势识别器实现双击缩放屏幕
detector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() { /** * 长按 * @param e */ @Override public void onLongPress(MotionEvent e) { super.onLongPress(e); } /** * 双击 * @param e * @return */ @Override public boolean onDoubleTap(MotionEvent e) { // Toast.makeText(SystemVideoPlayer.this, "我被双击了", Toast // .LENGTH_SHORT).show(); if (isFullScreen) { //设置为默认 setVideoType(DEFAULT_SCREEN); } else { //设置为全屏 setVideoType(FULL_SCREEN); } return super.onDoubleTap(e); } /** * 单击 * @param e * @return */ @Override public boolean onSingleTapConfirmed(MotionEvent e) { // Toast.makeText(SystemVideoPlayer.this, "我被单击了", Toast // .LENGTH_SHORT).show(); return super.onSingleTapConfirmed(e); } });