旋转纹理视图和外观填充纹理视图到容器的大小

时间:2018-08-10 00:24:20

标签: android matrix textureview

我有一个TextureView,可以在其中播放视频。

我的方向锁定为纵向,并且当我横向录制视频时,我也需要横向显示图像旋转90度保留长宽比,而方面填充不适合)。

我设法使它在高度大于宽度时起作用,但反之则不行:

       private void adjustAspectRatio(int videoWidth, int videoHeight, TextureView mTextureView, int degrees) {
        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 = (int) (viewWidth * aspectRatio);
            newHeight = viewHeight;
        } 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(degrees);          // rotate the image a number of degrees specified.
        txform.postTranslate(xoff, yoff);
        mTextureView.setTransform(txform);
}

做到这一点并使之起作用的最佳方法是什么?

0 个答案:

没有答案