我正在使用谷歌示例中的Camera2Basic案例(https://github.com/googlesamples/android-Camera2Basic),以在片段中显示摄像机预览。 该片段包含一个RelativeLayout和一个自定义的AutoFitTextureView。您可以在上面的链接中找到后者。我已经调整了布局,使预览居中,并删除了控制按钮:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/camera_rel">
<uk.co.deluxe_digital.messngr.AutoFitTextureView
android:id="@+id/texture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
我有三个片段在ViewPager中显示。我对Camera2BasicFragment进行了一些小改动,使其成为android.support.v4.app.Fragment,以便与我的ViewPager配合使用并删除按钮。
我想让相机预览填满整个屏幕。通过拉伸预览图像可以实现这一点。但这不是我想要的。有人知道一种缩放预览或TextureView的方法,使预览的高度与容器匹配,即使两侧被裁剪也不会影响。我希望它能够填满视图并保持纵横比。这意味着两侧的预览会被裁剪,但这没关系。
这是我目前的情况:
![current incorrect display of camera preview](https://istack.dev59.com/lPZXH.webp)
即使您能指导我正确的方向来解决这个问题,那也会是一个巨大的帮助!