效果如图:
实现步骤:
1.给videoview添加ontouch监听,this是所在的activity:
videoView.setOnTouchListener(this);
2.重写ontouch方法:
@Override
public boolean onTouch(View v, MotionEvent event) {
if(v.getId() == R.id.video_view) {
return mGesDetector.onTouchEvent(event);
}
return super.onTouchEvent(event);
}
3.写一个内部类实现
GestureDetector.SimpleOnGestureListener
接口:
/*
*
* 手势监听类
* */
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
public MyGestureListener() {
super();
}
@Override
public boolean onDoubleTap(MotionEvent e) {
if (screenState == LAND_SCREEN) {
convertToPortScreen();
} else if (screenState == PORT_SCREEN) {
convertToLandScreen();
}
return true;
}
}
4.使用手势监听类:
private GestureDetector mGesDetector;
mGesDetector = new GestureDetector(this,new MyGestureListener());
mGesDetector.setOnDoubleTapListener(new MyGestureListener());
走完这四步,对videoview的双击事件监听已经写完了。接下来奉上横竖屏切换的代码:
private void convertToPortScreen(){
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//设置videoView竖屏播放
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ToolUtils.dip2px(StudyWithExerciseActivity.this, 198f));
params.setMargins(ToolUtils.dip2px(StudyWithExerciseActivity.this,12),ToolUtils.dip2px(StudyWithExerciseActivity.this,15),
ToolUtils.dip2px(StudyWithExerciseActivity.this,12),ToolUtils.dip2px(StudyWithExerciseActivity.this,14));
//params.addRule(RelativeLayout.CENTER_IN_PARENT);
videoView.setLayoutParams(params);
screenState = PORT_SCREEN;
}
以上是切换成竖屏时走的代码。
private void convertToLandScreen(){
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();
Point point = new Point();
int API_LEVEL = android.os.Build.VERSION.SDK_INT;
if(API_LEVEL >= 17){
display.getRealSize(point);
}else{
display.getSize(point);
}
int height = point.y;
int width = point.x;
Log.i(TAG,"screenHeight = "+height+" ; screenWidth = "+width);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) videoView
.getLayoutParams(); // 取控当前的布局参数
layoutParams.height = height;
layoutParams.width = width;
layoutParams.setMargins(0,0,0,0);
videoView.setLayoutParams(layoutParams);
screenState = LAND_SCREEN;
}
以上为切换成横屏时走的代码。
注意:清单文件中要在该activity的便签中加入
android:configChanges="orientation|screenSize|keyboardHidden"
否则会报错。
好了,到这里VideoView双击全屏的效果就实现了。如果实际应用中遇到什么问题给我留言,会尽快回复。
欢迎参观博主其他博客。
最后推荐给一些想进大厂或者还没有拿到心仪offer的攻城狮们一本书,由大厂java面试官胡书敏编写,满满的干货,助你进到想去的公司。
博主上传资源下载链接:
自制免费无广告小说阅读APP下载:
https://download.csdn.net/download/yonghuming_jesse/10390364
全屏播放视频不拉伸源码:
https://download.csdn.net/download/yonghuming_jesse/10646274
科大讯飞语音评测服务接入源码:
https://download.csdn.net/download/yonghuming_jesse/10616924
android饺子播放器使用源码:
https://download.csdn.net/download/yonghuming_jesse/10619119
视频播放前显示视频第一帧源码:
https://download.csdn.net/download/yonghuming_jesse/10646332