gpt4 book ai didi

android - 未调用 surfaceCreated

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:06:52 27 4
gpt4 key购买 nike

我试图将相机预览 (SurfaceView) 与显示器上的按钮放在一起,但我得到的只是一个仅显示按钮的空白屏幕。如果我将 SurfaceView 设置为唯一的内容(使用 setContentView(surfaceView) 则预览在屏幕上显示正常。我做错了什么?

提前致谢

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1">
<SurfaceView
android:id="@+id/camera_surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/trigger"
android:id="@+id/trigger_picture_button"
android:layout_gravity="bottom" />
</FrameLayout>
</LinearLayout>

我的 Activity :

public class CameraActivity extends Activity {

private SurfaceView cameraPreview;
private Button triggerPicture;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);

cameraPreview = (SurfaceView) findViewById(R.id.camera_surface);
triggerPicture = (Button) findViewById(R.id.trigger_picture_button);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
cameraPreview = new CameraPreview(this);
// setContentView(cameraPreview);
}

我的相机预览:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder holder;
private Camera camera;

public CameraPreview(Context context) {
super(context);
holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
camera = openFrontFacingCamera();
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
camera.release();
camera = null;
}
}

public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(width, height);
parameters.set("orientation", "portrait");
camera.setParameters(parameters);
camera.startPreview();
}

private Camera openFrontFacingCamera() {
// returns Camera.open(
}

最佳答案

当您在布局中声明 SurfaceView 时,您告诉 Android 使用内置类。您需要做的是通过将 XML 中的 SurfaceView 替换为 your.package.name.CameraPreview 使其使用您的类。

另一件事:你应该向 CameraPreview 添加一个构造函数,它带有参数 ContextAttributeSet,否则布局 inflater 将无法使用给你的类(class)充气。此外,您不需要行 cameraPreview = new CameraPreview(this);

关于android - 未调用 surfaceCreated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5912053/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com