簡體   English   中英

Android VideoView中的人像視頻不會占據整個屏幕寬度

[英]Portrait video in Android VideoView will not take up whole width of the screen

我的布局中有一個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內部,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。 猜怎么着,沒有用。

我創建了這個簡單的VideoView子類,該子類在保持寬高比的情況下全屏顯示,您可以在此處找到最新版本的Gist ,但這是一個示例:

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

沒關系,我是個白痴。 我很確定,即使是菜鳥視頻編輯者也可以做到這一點(即使我也不是菜鳥),但在旋轉視頻后,由於縱橫比的緣故,在肖像視頻的側面添加了黑條,這就是視頻無法填充屏幕寬度的原因-因為那里有黑條! 原來,裁剪完黑條之后,視頻可以完美拉伸-就像以前一樣,但是黑條消失了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM