视频播放宽度大小各不一样,就需要根据视频的尺寸去适应屏幕的宽度和高度进行缩放。
思路是在onPrepared方法中,或者切换屏幕后,获取video的高度和宽度。以及屏幕展示区域的高度的宽度。并对比二者比例,进行缩放。
简单的代码如下,仅供参考
WindowManager wm = (WindowManager) MovieOverViewActivity.this
.getSystemService(Context.WINDOW_SERVICE);
Rect outRect = new Rect();
MovieOverViewActivity.this.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(outRect);
int width = wm.getDefaultDisplay().getWidth()-outRect.top;
int height = wm.getDefaultDisplay().getHeight();
int mVideoWidth = mVV.getVideoWidth();
int mVideoHeight = mVV.getVideoHeight();
RelativeLayout.LayoutParams layoutParams=(RelativeLayout.LayoutParams)mVV.getLayoutParams();
if(mVideoWidth>0&&mVideoHeight>0){
Log.d("宽度,",width+":"+height+":"+mVideoWidth+":"+mVideoHeight);
if(width*mVideoHeight>height*mVideoWidth){
width = height * mVideoWidth / mVideoHeight;
}else if(mVideoWidth * height < width * mVideoHeight){
height = width * mVideoHeight / mVideoWidth;
}
}
layoutParams.height = height;
layoutParams.width = width;
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
// layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
// layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
// layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
// layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
mVV.setLayoutParams(layoutParams);//mvv 为BVideoView