我正在视频中播放视频。水平视频播放效果很棒,但是当我播放垂直视频时,它会拉伸到屏幕的整个宽度,而不是按照垂直比例播放。
这是我的xml视图。
<FrameLayout
android:id="@+id/flMain"
android:layout_width="wrap_content"
android:layout_height="@dimen/playerHeight"
android:background="@color/black">
<com.controlcastappplayer.customviews.MyVideoView
android:id="@+id/videoView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:visibility="visible" /></FrameLayout>
我正在使用自定义视频视图
以下是自定义视频视图的课程
public class MyVideoView extends VideoView {
private int mVideoWidth;
private int mVideoHeight;
public MyVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyVideoView(Context context) {
super(context);
}
public void setVideoSize(int width, int height) {
mVideoWidth = width;
mVideoHeight = height;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.i("@@@", "onMeasure");
int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
if (mVideoWidth > 0 && mVideoHeight > 0) {
if (mVideoWidth * height > width * mVideoHeight) {
Log.i("@@@", " onMeasure image too tall, correcting");
height = width * mVideoHeight / mVideoWidth;
} else if (mVideoWidth * height < width * mVideoHeight) {
Log.i("@@@", "onMeasure image too wide, correcting");
width = height * mVideoWidth / mVideoHeight;
} else {
Log.i("@@@", "onMeasure aspect ratio is correct: " +
width + "/" + height + "=" +
mVideoWidth + "/" + mVideoHeight);
}
}
// Log.i("@@@", "setting size: " + width + 'x' + height);
setMeasuredDimension(width, height);
}}
通过使用此代码,我的视频就像这样。
但我的需求是视频应该像这样看。