Phi*_*ens 5 android translation xamarin textureview
目前我正在使用相机播放器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;
}
Run Code Online (Sandbox Code Playgroud)
在旧版Android设备上进行缩放和计算dx
并dy
完美运行,但我使用API级别23处理的设备会引发意外行为.
尽管正确定位,手机仍能切断大量图像.这让我相信底部部分不会在旧设备上呈现.任何人都可以证实这一点并指出我正确的位置来解决这个问题吗?
经过长时间的测试,我发现问题是由于SetTransform
方法造成的。我正在使用矩阵设置比例,但这以某种方式渲染了我的纹理并忽略了TranslationX
& TranslationY
。删除矩阵并用 floatscaledWidth = width *scaleX 替换它;浮动缩放高度=高度*缩放Y;
float dx = (width - scaledWidth) * 0.5f;
float dy = (height - scaledHeight) * 0.5f;
_textureView.ScaleX = scaleX;
_textureView.ScaleY = scaleY;
_textureView.TranslationX = dx;
_textureView.TranslationY = dy;
Run Code Online (Sandbox Code Playgroud)
修复了在某些 Android 设备上渲染错误的问题。
归档时间: |
|
查看次数: |
264 次 |
最近记录: |