TextureView TranslationX&在API 23上没有预期的行为

时间:2017-02-07 16:29:47

标签: android xamarin translation textureview

目前我正在使用textureview制作相机,以渲染相机。因为预览可以有任何维度,所以我在调用OnSurfaceTextureUpdated时创建了一些自定义代码来改变纹理视图:

    void updateTextureMatrix(int width, int height) {
        Display display = WindowManager.DefaultDisplay;
        var isPortrait = (display.Rotation == SurfaceOrientation.Rotation0 || display.Rotation == SurfaceOrientation.Rotation180);

        int previewWidth = orgPreviewWidth;
        int previewHeight = orgPreviewHeight;

        if(isPortrait) {
            previewWidth = orgPreviewHeight;
            previewHeight = orgPreviewWidth;
        }

        // determine which part to crop
        float widthRatio = (float)width / previewWidth;
        float heightRatio = (float)height / previewHeight;

        float scaleX;
        float scaleY;

        if(widthRatio > heightRatio) {
            // height must be cropped
            scaleX = 1;
            scaleY = widthRatio * ((float)previewHeight / height);
        } else {
            // width must be cropped
            scaleX = heightRatio * ((float)previewWidth / width);
            scaleY = 1; 
        }

        Android.Graphics.Matrix matrix = new Android.Graphics.Matrix();

        matrix.SetScale(scaleX, scaleY);
        _textureView.SetTransform(matrix);

        float scaledWidth = width * scaleX;
        float scaledHeight = height * scaleY;

        float dx = (width - scaledWidth) * 0.5f;
        float dy = (height - scaledHeight) * 0.5f;
        _textureView.TranslationX = dx;
        _textureView.TranslationY = dy;
    }

缩放&计算dx& dy在较旧的Android设备上工作得非常好,但是我使用API​​级别23处理的设备会引发意外行为。

Galaxy S3正确显示: Galaxy S3

但是在S7上: S7

尽管正确定位,但手机会切断很多图像。这让我相信底部部分不会在旧设备上呈现。任何人都可以证实这一点并指出我正确的位置来解决这个问题吗?

1 个答案:

答案 0 :(得分:4)

经过长时间的测试后,我发现问题归因于SetTransform方法。我正在使用矩阵设置我的比例,但这在某种程度上渲染了我的纹理&忽略了TranslationX& TranslationY。去除矩阵&替换它 float scaledWidth = width * scaleX;             float scaledHeight = height * scaleY;

        float dx = (width - scaledWidth) * 0.5f;
        float dy = (height - scaledHeight) * 0.5f;
        _textureView.ScaleX = scaleX;
        _textureView.ScaleY = scaleY;
        _textureView.TranslationX = dx;
        _textureView.TranslationY = dy;

修复了我在某些Android设备上错误渲染的问题。