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

标签 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();

这是我的 videoview 元素的 .xml(顺便说一句,在 RelativeLayout 中):

 <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" />

在我真正希望在公园散步时遇到的最令人惊讶的事情是无论我做什么,视频确实会拉伸(stretch)以填充屏幕的高度(因此 layout_alignParentTop 和 layout_alignParentBottom 显然有效) 但这该死的东西不会填满屏幕的宽度!(是的,我很沮丧)所以这个已经垂直的视频在屏幕中间被拉伸(stretch)成一个类似丝带的结构.

我绝望地添加了 marginLeft 和 marginRight 属性,想看看它是否有效,但没有任何效果!大吃一惊,我删除了 centerInParent 属性,但还是没有。我什至创建了一个自定义 VideoView,覆盖了 onMeasure,但仍然没有。

我试图以编程方式设置 VideoView 的 LayoutParams。你猜怎么着,没用。

最佳答案

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

/**
 * 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>

关于java - Android VideoView 中的纵向视频不会占据整个屏幕宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27356540/

相关文章:

java - Java中线性系统的符号解

video - 有没有更安全的方法将 FFMpeg 包含在商业应用程序中?

html - 如何在没有 iframe 的情况下播放 youtube 视频?

android - Android logcat 中的 SRIB_DCS 日志记录是什么?如何停止它?

Android收不到组播包

video - 如何更改 ffmpeg xstack 背景颜色

java - Spring - 上传表单 - 无法获取文件列表

java - 索贝尔滤波器很奇怪

java - 如何设置 MEDIASTORE 以从 android 上的特定文件夹获取图像

java - 将国际字符从 MySQL 输出到 Java/Android