TextureView Android上的矩阵比例视频

ole*_*men 3 video android scale exoplayer

我想一起玩的视频ExoPlayeTextureView.为了缩放和裁剪视频以适应视图,我使用矩阵.我的自定义视图扩展了`TextureView'这是我的代码:

@Override
public void onVideoSizeChanged(int width, int height, float pixelWidthHeightRatio) {
    updateTextureViewSize(width, height);
    Log.v("EXO", "onVideoSizeChanged() " +  width + "x" + height);
}

private void updateTextureViewSize(float videoWidth, float videoHeight) {
    float viewWidth = getWidth();
    float viewHeight = getHeight();

    float scaleX = 1.0f;
    float scaleY = 1.0f;

    float viewRatio = viewWidth / viewHeight;
    float videoRatio = videoWidth / videoHeight;
    if (viewRatio > videoRatio) {
        // video is higher than view
        scaleY = videoHeight / videoWidth;
    } else {
        //video is wider than view
        scaleX = videoWidth / videoHeight;
    }

    Matrix matrix = new Matrix();
    matrix.setScale(scaleX, scaleY, viewWidth / 2, viewHeight / 2);


    setTransform(matrix);
}
Run Code Online (Sandbox Code Playgroud)

我有一个长方形的视图,它工作得很好.但现在视图有两种状态:扩展(旧的仍然是矩形)和折叠(具有较小的高度.

所以现在视频在那些折叠的视图中被垂直拉伸.

例如,我有视图1080x480和视频,360x640但它看起来像视频缩放和裁剪到1080x1080拉伸到1080x480.

我在这做错了什么?

UPD:以下是截图: 在此输入图像描述

ole*_*men 7

我通过将比例因子乘以或除以viewRatio(width / height)来解决此问题:

    if (viewRatio > videoRatio) {
        // video is higher than view
        scaleY = videoHeight / videoWidth * viewRatio;
    } else {
        //video is wider than view
        scaleX = videoWidth / videoHeight / viewRatio;
    }
Run Code Online (Sandbox Code Playgroud)

但我不明白为什么它会像这样工作.根据我的计算,如果我有,例如,视图1080x480和视频360x640视频应缩放以使宽度x' = 1080和高度成比例.所以身高应该是y' = 640*1080/360(videoHeight * viewHeight / videoWidth)和x' = 1080

根据这张图片: 在此输入图像描述

sx * 360 = 1080 => sx = 1080 / 360

sy * 640 = 640*1080/360 => sy = 1080 / 360

看起来很有意义.如果我们需要保存比例,宽度和高度应乘以相同的因子.但它不会这样.哪里出错?这有什么好的文件吗?


ram*_*ari 2

查看updateTextureViewSize

/**
 * Set the display options
 *
 * @param layout <ul>
 *               <li>{@link #VIDEO_LAYOUT_ORIGIN}
 *               <li>{@link #VIDEO_LAYOUT_SCALE}
 *               <li>{@link #VIDEO_LAYOUT_STRETCH}
 *               <li>{@link #VIDEO_LAYOUT_ZOOM}
 *               </ul>
 */
public void updateTextureViewSize(int layout,float videoWidth, float videoHeight) {
    RelativeLayout.LayoutParams lp = (android.widget.RelativeLayout.LayoutParams) getLayoutParams();
    DisplayMetrics disp = m_Context.getResources().getDisplayMetrics();
    int windowWidth = disp.widthPixels, windowHeight = disp.heightPixels;
    float windowRatio = windowWidth / (float) windowHeight;
    float videoRatio = (float) videoWidth / (float) videoHeight;
    m_iSurfaceHeight = videoHeight;
    m_iSurfaceWidth = videoWidth;
    if (VIDEO_LAYOUT_ORIGIN == layout && m_iSurfaceWidth < windowWidth && m_iSurfaceHeight < windowHeight) {
        lp.width = (int) (m_iSurfaceHeight * videoRatio);
        lp.height = m_iSurfaceHeight;
    } else if (layout == VIDEO_LAYOUT_ZOOM) {
        lp.width = windowRatio > videoRatio ? windowWidth : (int) (videoRatio * windowHeight);
        lp.height = windowRatio < videoRatio ? windowHeight : (int) (windowWidth / videoRatio);
    } else {
        boolean full = layout == VIDEO_LAYOUT_STRETCH;
        lp.width = (full || windowRatio < videoRatio) ? windowWidth : (int) (videoRatio * windowHeight);
        lp.height = (full || windowRatio > videoRatio) ? windowHeight : (int) (windowWidth / videoRatio);
        lp.leftMargin = (disp.widthPixels - lp.width) / 2;
        lp.topMargin = (disp.heightPixels - lp.height) / 2;
    }

    lp.leftMargin = (disp.widthPixels - lp.width) / 2;
    lp.topMargin = (disp.heightPixels - lp.height) / 2;

    getHolder().setFixedSize(m_iSurfaceWidth, m_iSurfaceHeight);

    setLayoutParams(lp);

    m_iVideoLayout = layout;
}
Run Code Online (Sandbox Code Playgroud)