开发一个播放器,在播放视频时,需要按视频的原始比例进行视频比例的设置,我在RelativeLayout中方surfaceview控件,布局设置的分辨率为1280x720,surfaceview在代码中设置。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="1280px"
android:layout_height="720px"
android:layout_marginBottom="60px"
android:background="@drawable/bg1"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/playLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/surface"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"/>
</LinearLayout>
</RelativeLayout>
计算视频需要设置的比例
int displayWith = playLinearLayout.getWidth();//获取布局的宽度1280
int displayHeight = playLinearLayout.getHeight();//获取布局的高度720
mVideoWidth = mMediaPlayer.getVideoWidth();//视频的宽度640
mVideoHeight = mMediaPlayer.getVideoHeight();//视频的高度272
if (mVideoWidth != 0 && mVideoHeight != 0) {
/* 设置视频的宽度和高度 */
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(displayWith, displayHeight);
//视频显示高度要重新调整
if (mVideoWidth*displayHeight > displayWith*mVideoHeight){
params.height = displayWith * mVideoHeight / mVideoWidth;
}//视频宽度要重新调整
else if (mVideoWidth*displayHeight < displayWith*mVideoHeight){
params.width = displayHeight * mVideoWidth / mVideoHeight;
}
else {
params.width = displayWith;
params.height = displayHeight;
}
m_SurfaceView.setLayoutParams(params);//设置surfaceview的大小,放大后是1280x544
m_SurfaceHolder.setFixedSize(params.width, params.height);
/* 开始播放 */
mediaPlayerPlay();
运行后,视频显示的大小是正确了,但是视频的上边是黑边,下边却是灰边,这灰边是怎么出现的?请大佬帮忙解决。
我的原始设计意图就是要视频按照正常比例缩放,避免全屏显示时,画面拉伸变形。
