我正在开发一个带有CameraFragment和实时预览的小型应用程序。在纵向模式下,该片段占据了大约一半的屏幕。在其下方是仅中心的“放大镜”视图。为了帮助用户对齐所有内容,我将绿色十字准线精确地绘制在预览的中间。
问题是,我的十字线的中心(即AutoFitTextureView的中心)与实际图片不对齐。我不确定为什么。
布局如下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CameraFragment">
<!-- TODO: Update blank fragment layout -->
<com.xxx.yyy.AutoFitTextureView
android:id="@+id/previewTextureView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"/>
<com.xxx.yyy.GuideView
android:id="@+id/greenLines"
android:scaleType="fitXY"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</FrameLayout>
GuideView组件在onfChanged()获得的0.5f处绘制0.5f的线。
打开相机时,我会以通常的camera2方式选择尺寸,例如:
val sizes: Array<Size> = map.getOutputSizes(Allocation::class.java)
val previewSize = chooseOptimalSize(sizes, 2000, 2000)
然后我相应地设置AutoFitTextureView:
val orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
previewTextureView.setAspectRatio(width = previewSize.width, height = previewSize.height)
} else {
previewTextureView.setAspectRatio(width = previewSize.height, height = previewSize.width)
}
我认为所有这些都与camera2示例一致,但是十字准线在所有尺寸下都不太一致。上面的2000,2000要求实际上使我2164,2164,十字准线关闭。如果我改为选择1080x1920,则它或多或少地完美排列。
我不太在乎它是否会修剪或缩放(失真),我只需要让十字准线对齐即可,我不确定为什么不这样。我觉得它现在正在做的是以某种方式裁剪相机图像,使它看起来像是一个转变。