private boolean mIsVideoSizeKnown = false;
private int mVideoWidth;
private int mVideoHeight;
private int mSurfaceViewWidth;
private int mSurfaceViewHeight;
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
if (width == 0 || height == 0) {
Log.e(TAG, "invalid video width(" + width + ") or height(" + height
+ ")");
return;
}
// Logger.d(TAG, "onVideoSizeChanged width:" + width + " height:" + height);
mIsVideoSizeKnown = true;
mVideoHeight = height;
mVideoWidth = width;
int wid = mPlayer.getVideoWidth();
int hig = mPlayer.getVideoHeight();
// 根据视频的属性调整其显示的模式
if (wid > hig) {
if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
} else {
if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
mSurfaceViewWidth = dm.widthPixels;
mSurfaceViewHeight = dm.heightPixels;
if (width > height) {
//调节其上下的空余
int w = mSurfaceViewHeight * width / height;
int margin = (mSurfaceViewWidth - w) / 2;
Log.d(TAG, "margin:" + margin);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lp.setMargins(margin, 0, margin, 0);
mSurfaceView.setLayoutParams(lp);
} else {
//调节其左右的空余
int h = mSurfaceViewWidth * height / width;
int margin = (mSurfaceViewHeight - h) / 2;
Log.d(TAG, "margin:" + margin);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lp.setMargins(0, margin, 0, margin);
mSurfaceView.setLayoutParams(lp);
}
}
Android横竖屏切换
最新推荐文章于 2022-08-08 20:30:09 发布