我使用AutoFitTextureView的camera2预览出现问题。执行此代码后,布局将正常显示。相机预览不会显示。我有权使用相机。在此之前,我写道
setContentView(mTextureView)
这有效。预先谢谢你。
MainActivity
public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener, ActivityCompat.OnRequestPermissionsResultCallback {
private AutoFitTextureView mTextureView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextureView = new AutoFitTextureView(this);
mTextureView.setSurfaceTextureListener(this);
}
SurfaceTexture
@RequiresApi(api = Build.VERSION_CODES.M)
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
openCamera(width, height);
}
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
closeCamera();
return true;
}
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
AutoFitTextureView
public class AutoFitTextureView extends TextureView {
private int mRatioWidth = 0;
private int mRatioHeight = 0;
public AutoFitTextureView(Context context) {
this(context, null);
}
public AutoFitTextureView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setAspectRatio(int width, int height) {
if (width < 0 || height < 0) {
throw new IllegalArgumentException("Size cannot be negative.");
}
mRatioWidth = width;
mRatioHeight = height;
requestLayout();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height);
} else {
if (width < height * mRatioWidth / mRatioHeight) {
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
} else {
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
}
}
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000" >
<TextureView
android:id="@+id/texture"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<Button
android:id="@+id/button_take_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
布局文件中的TextureView在代码中并非自动为mTextureView。这是一个完全独立的TextureView,您可以通过常用的findViewById方法获得引用。
如果要在布局中使用AutoFitTextureView,则必须实际调用它,这需要花费更多的工作,因为必须告知布局系统有关AutoFixTextureViews的信息。
或者,您可以通过找到FrameLayout然后向其添加子级来将创建的mTextureView手动添加到布局中。
这些都是标准的Android UI创建主题,而不是特定于相机的主题,因此您应该能够找到很多资源,有关在Android上创建自定义视图或如何混合使用XML和程序化UI布局。
通过添加init(){}
AutoFitTextureView类的方法和findViewById
到MainActivity类。