首页 > 代码库 > Android SurfaceView播放视频时横竖屏的调整
Android SurfaceView播放视频时横竖屏的调整
对于横屏录制的视频就横屏播放,对于竖屏录制的视频就竖屏播放。
在mainifest文件里对负责播放的Activity添加以下属性“
android:configChanges="orientation|keyboardHidden|screenSize"
重写Acitivity的onConfigurationChanged方法:
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); }
根据视频的属性来区分:
@Override 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 = mMediaPlayer.getVideoWidth(); int hig = mMediaPlayer.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; Logger.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; Logger.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 SurfaceView播放视频时横竖屏的调整
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。