android – TextureView TranslationX&Y在API 23上没有预期的行为
作者:互联网
目前我正在制作一个带有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处理的设备会引发意外行为.
尽管正确定位,手机仍能切断大量图像.这让我相信底部部分不会在旧设备上呈现.任何人都可以证实这一点并指出我正确的位置来解决这个问题吗?
解决方法:
经过长时间的测试,我发现问题是由于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设备上错误渲染的问题.
标签:android,xamarin,translation,textureview 来源: https://codeday.me/bug/20190522/1154553.html