我在使用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" />
发布于 2020-03-11 19:30:17
布局文件中的TextureView在代码中不是自动的mTextureView。它是一个完全独立的TextureView,您可以通过通常的findViewById方法获得引用。
如果您想在布局中使用AutoFitTextureView,则必须将其称为AutoFixTextureViews,这需要更多的工作,因为您必须告诉布局系统有关AutoFixTextureViews的信息。
或者,您可以手动将创建的mTextureView添加到布局中,方法是找到FrameLayout,然后向布局中添加一个子元素。
这些都是标准的Android创建主题,而不是特定于摄像机的主题,因此您应该能够找到大量关于在Android上创建自定义视图或如何混合使用XML和编程UI布局的资源。
发布于 2020-03-12 09:47:16
通过将init(){}
方法添加到AutoFitTextureView类,将findViewById
添加到MainActivity类。
并删除mTextureView = new AutoFitTextureView(this); mTextureView.setSurfaceTextureListener(this);
。
您还必须将XML文件从TextureView更改为“包名”.AutoFitTextureView。
在进行此更改之前,没有通过TextureView执行AutoFitTextureView的方法。
对于那些刚接触安卓系统并无法理解的人,请查看这段Youtube视频:https://youtu.be/sb9OEl4k9Dk。
我希望这能帮到你。
https://stackoverflow.com/questions/60638636
复制相似问题