在纹理视图上播放视频与视频的宽高比

Rav*_*put 13 android textureview

我正在使用纹理视图在我的服务器上播放视频,但是它会拉伸视频并且无法保持视频的宽高比.但是,我希望保持视频的长宽比,如在应用程序或Instagram应用程序中.

fad*_*den 13

您可以更改视图的大小(使用自定义FrameLayout),也可以使用TextureView矩阵更改纹理的渲染方式.

两者的例子都可以在Grafika中找到."播放视频(TextureView)"活动演示了如何配置矩阵以匹配视频的宽高比.查看adjustAspectRatio()方法,其核心是:

    Matrix txform = new Matrix();
    mTextureView.getTransform(txform);
    txform.setScale((float) newWidth / viewWidth, (float) newHeight / viewHeight);
    txform.postTranslate(xoff, yoff);
    mTextureView.setTransform(txform);
Run Code Online (Sandbox Code Playgroud)

请注意它的中心以及缩放视频.


Uma*_*Ata 5

好吧,我来不及回答,但是从格拉菲卡(Grafika)获得时,您可以使用此功能

 private void adjustAspectRatio(int videoWidth, int videoHeight) {
    int viewWidth = mTextureView.getWidth();
    int viewHeight = mTextureView.getHeight();
    double aspectRatio = (double) videoHeight / videoWidth;

    int newWidth, newHeight;
    if (viewHeight > (int) (viewWidth * aspectRatio)) {
        // limited by narrow width; restrict height
        newWidth = viewWidth;
        newHeight = (int) (viewWidth * aspectRatio);
    } else {
        // limited by short height; restrict width
        newWidth = (int) (viewHeight / aspectRatio);
        newHeight = viewHeight;
    }
    int xoff = (viewWidth - newWidth) / 2;
    int yoff = (viewHeight - newHeight) / 2;
    Log.v(TAG, "video=" + videoWidth + "x" + videoHeight +
            " view=" + viewWidth + "x" + viewHeight +
            " newView=" + newWidth + "x" + newHeight +
            " off=" + xoff + "," + yoff);

    Matrix txform = new Matrix();
    mTextureView.getTransform(txform);
    txform.setScale((float) newWidth / viewWidth, (float) newHeight / viewHeight);
    //txform.postRotate(10);          // just for fun
    txform.postTranslate(xoff, yoff);
    mTextureView.setTransform(txform);
}
Run Code Online (Sandbox Code Playgroud)