视频切换横竖屏参考文章: https://blog.csdn.net/csdnwr/article/details/121141056
第一步:圈定画布为 宽400像素. 高度videoScale计算出实际像素 public static float videoScale = 16f / 9f; float videoX = 400f; String videoy = (videoX / videoScale) + ""; double videoHeight = Math.round(Double.parseDouble(videoy)); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((int) videoX, (int) videoHeight); // lp.addRule(RelativeLayout.CENTER_IN_PARENT); lp.addRule(RelativeLayout.BELOW, R.id.left_video_title_view); mVideoView.setLayoutParams(lp);
第二步:拿到视频实际宽高
mMediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() { @Override public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { if (mSurfaceView != null) { changeVideoSize(mSurfaceView, width, height); } } });
第三步:计算mSurfaceView实际视频宽高和画布宽度比例(不需要描点的到此结束)
public void changeVideoSize(SurfaceView mSurfaceView, int videoWidth, int videoHeight) { //视频容器宽度以400像素计算比例 float surfaceWidth = 400f; float max = (float) videoWidth / (float) surfaceWidth; //视频宽高分别/最大倍数值 计算出放大后的视频尺寸 videoWidth = (int) Math.ceil((float) videoWidth / max); videoHeight = (int) Math.ceil((float) videoHeight / max); //无法直接设置视频尺寸,将计算出的视频尺寸设置到surfaceView 让视频自动填充。 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(videoWidth, videoHeight); lp.gravity = Gravity.CENTER; mSurfaceView.setLayoutParams(lp); }
第四步:视频添加等比例tag标记
视频需要用Frameview单独包裹,否则某些机型无法正常显隐藏 mVideoFrameview.getChildAt(0).setVisibility(View.GONE); 此处给的是第一步拿到的画布宽高 mVideoTagView.setVideoXY((int) videoX, (int) videoHeight);
自定义标记View
public class TagTextView extends View { private Paint bgPaint = new Paint(); private Paint innerBgPaint = new Paint(); private Paint textPaint = new Paint(); private float bgRadius = 12; //外圆半径 private float innerBgRadius = 8; //内圆半径 private float testSize = 4; //内容字号 private int bgColor = R.color.dot_color_trans_25; private int innerBgColor = R.color.dot_color; private List<IotVideo.RelCoordinateList> mTagInfoBean = new ArrayList<>(); public TagTextView(Context context, List<IotVideo.RelCoordinateList> tagInfoBean) { this