Android VideoView中的纵向视频不会占据整个屏幕宽度

时间:2014-12-08 11:01:16

标签: java android video android-videoview

我的布局中有一个VideoView,在这个VideoView中,我试图播放一个垂直方向的视频。我的印象是,这是一个旨在以纵向显示的肖像视频,事情变得更加容易。男孩,我错了。

这是我的Java代码:

videoView1 = (VideoView) findViewById(R.id.videoView1);


        videoView1.setVideoPath("android.resource://" + getPackageName() + "/"
                + R.raw.video);
        videoView1.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                videoView1.start();
            }
        });
videoView1.start();

这是我的.xml视频元素(位于RelativeLayout,btw中):

 <VideoView
        android:id="@+id/videoView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp" />

我真正期待在公园里散步时遇到的最令人惊讶的事情是,无论我做什么,视频都会拉伸以填充屏幕的高度(因此layout_alignParentTop和layout_alignParentBottom显然正在工作)但该死的东西不会填满屏幕的宽度!(是的,我很沮丧)所以这个已经垂直的视频被拉伸到屏幕中间的带状结构

我在绝望的尝试中添加了marginLeft和marginRight属性,看看它是否可行但没有任何效果!大惊小怪,我删除了centerInParent属性但是没有。我甚至创建了一个自定义的VideoView,覆盖了onMeasure,但仍然没有。

我尝试以编程方式设置VideoView的LayoutParams。猜猜什么,没用。

2 个答案:

答案 0 :(得分:1)

我创建了这个简单的VideoView子类,在保持宽高比的同时全屏显示,你可以找到最新版本的 Gist here ,但是这里& #39;是一个例子:

/**
 * Subclass of VideoView to enable video scaling
 * CustomAttribute: "scaleType": "normal", "centerCrop", "fill"
 * <p>
 * Add this stylable:
 * <declare-styleable name="IntroVideoView">
 * <attr name="scaleType" format="integer">
 * <flag name="normal" value="0" />
 * <flag name="centerCrop" value="1" />
 * <flag name="fill" value="2" />
 * </attr>
 * </declare-styleable>
 * <p>
 * Created by joaquimley on 13/01/2017.
 */

public class IntroVideoView extends VideoView {

    private static final int SCALE_TYPE_NORMAL = 0;
    private static final int SCALE_TYPE_CENTER_CROP = 1;
    private static final int SCALE_TYPE_FILL = 2;

    private int mScaleType;
    private int mHorizontalAspectRatioThreshold;
    private int mVerticalAspectRatioThreshold;

    public IntroVideoView(Context context) {
        this(context, null, 0);
    }

    public IntroVideoView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IntroVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.IntroVideoView, 0, 0);
        mScaleType = attributes.getInt(R.styleable.IntroVideoView_scaleType, SCALE_TYPE_NORMAL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mScaleType == SCALE_TYPE_CENTER_CROP) {
            applyCenterCropMeasure(widthMeasureSpec, heightMeasureSpec);
        } else if (mScaleType == SCALE_TYPE_FILL) {
            applyFillMeasure(widthMeasureSpec, heightMeasureSpec);
        } // else default/no-op
    }

    @Override
    public void layout(int l, int t, int r, int b) {
        if (mScaleType == SCALE_TYPE_CENTER_CROP) {
            applyCenterCropLayout(l, t, r, b);
        } else {
            super.layout(l, t, r, b);
        }
    }

    private void applyCenterCropLayout(int left, int top, int right, int bottom) {
        super.layout(left + mHorizontalAspectRatioThreshold, top + mVerticalAspectRatioThreshold, right
                + mHorizontalAspectRatioThreshold, bottom + mVerticalAspectRatioThreshold);
    }

    private void applyCenterCropMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int videoWidth = getMeasuredWidth();
        int videoHeight = getMeasuredHeight();

        int viewWidth = getDefaultSize(0, widthMeasureSpec);
        int viewHeight = getDefaultSize(0, heightMeasureSpec);

        mHorizontalAspectRatioThreshold = 0;
        mVerticalAspectRatioThreshold = 0;
        if (videoWidth == viewWidth) {
            int newWidth = (int) ((float) videoWidth / videoHeight * viewHeight);
            setMeasuredDimension(newWidth, viewHeight);
            mHorizontalAspectRatioThreshold = -(newWidth - viewWidth) / 2;
        } else {
            int newHeight = (int) ((float) videoHeight / videoWidth * viewWidth);
            setMeasuredDimension(viewWidth, newHeight);
            mVerticalAspectRatioThreshold = -(newHeight - viewHeight) / 2;

        }
    }

    private void applyFillMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = getDefaultSize(0, widthMeasureSpec);
        int height = getDefaultSize(0, heightMeasureSpec);
        setMeasuredDimension(width, height);
    }
}

自定义属性的样式:

<resources>
    <declare-styleable name="IntroVideoView">
        <attr name="scaleType" format="integer">
            <flag name="normal" value="0" />
            <flag name="centerCrop" value="1" />
            <flag name="fill" value="2" />
        </attr>
    </declare-styleable>
</resources>

答案 1 :(得分:0)

没关系,我是个白痴。我很确定即使是新手视频编辑也不会犯这个错误(我甚至不是新手)但是在旋转视频后,由于纵横比,在画面的侧面添加了黑条视频以及为什么视频无法填满屏幕的宽度 - 因为黑条在那里!事实证明,在拍完黑条之后,视频完美地延伸 - 就像之前一样,但黑条消失了!