Android TextureView TranslationX&;Y不符合API 23的预期行为
目前我正在制作一个带有Android TextureView TranslationX&;Y不符合API 23的预期行为,android,xamarin,translation,textureview,Android,Xamarin,Translation,Textureview,目前我正在制作一个带有textureview的相机播放器来渲染我的相机。因为预览可以有任何维度,所以我创建了一些自定义代码,以便在调用OnSurfaceTextureUpdated时更改textureview: void updateTextureMatrix(int width, int height) { Display display = WindowManager.DefaultDisplay; var isPortrait = (display.
textureview
的相机播放器来渲染我的相机。因为预览可以有任何维度,所以我创建了一些自定义代码,以便在调用OnSurfaceTextureUpdated
时更改textureview:
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将正确显示:
但在S7上:
尽管定位正确,但手机仍能截取大量图像。这使我相信底部部分没有在旧设备上渲染。有人能确认这一点并为我指出解决此问题的正确位置吗?经过长时间的测试,我发现问题是由于
SetTransform
方法造成的。我正在使用矩阵设置比例,但这以某种方式渲染了我的纹理&忽略了TranslationX
&TranslationY
。移除矩阵并将其替换为
float scaledWidth=宽度*scaleX;
浮动缩放高度=高度*缩放
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设备上错误渲染的问题