ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

TextureView Android上的矩阵比例视频

2019-08-23 04:25:14  阅读:436  来源: 互联网

标签:android exoplayer video scale


我正在尝试使用TextureView上的ExoPlaye播放视频.为了缩放和裁剪视频以适应视图,我使用矩阵.我的自定义视图扩展了`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);
}

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

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

例如,我有视图1080×480和视频360×640但它看起来像视频缩放并裁剪为1080×1080而不是拉伸到1080×480.

我在这做错了什么?

UPD:以下是截图:
enter image description here

解决方法:

检查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;
}

标签:android,exoplayer,video,scale
来源: https://codeday.me/bug/20190823/1695111.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有