一个SurfaceView何时从布局中创建(以编程方式)?

3

我想知道SurfaceView什么时候从布局中创建出来?

如果一个LiveCard是由Service创建的,并且没有Activity可以使用setContentView()设置这个布局,这种情况下会发生什么?

我还想知道程序中SurfaceView的创建需要哪些步骤。

以下是我的布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black">

    <SurfaceView
        android:id="@+id/camera_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

然后我有:

    mLayout = (FrameLayout) inflater.inflate(R.layout.orientation_recorder, null);
    mLayout.setWillNotDraw(false);

    cameraView = (SurfaceView) mLayout.findViewById(R.id.camera_view);
    cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                Log.e("OR", "camera view sufrace destroyed");
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                Log.e("OR", "camera view sufrace created");
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width,
                    int height) {
                Log.e("OR", "camera view sufrace changed "
                        + " format: " + format
                        + " width: " + width
                        + " height: " + height);
            }
    });

但是,我无法创建SurfaceView,例如上面从未调用surfaceCreated()。

需要什么才能实现这一点?

1个回答

4

您需要将内容视图设置为布局,并在您的活动或主活动类中将表面视图添加到布局中。

setContentView(R.layout.main);<---- basically points to and uses your xml

LinearLayout layout = (LinearLayout)findViewById(R.id.layout);<---- points to Layout defined in xml

layout.addView(new SurfaceView(this));<----adds SurfaceView to layout

您可以在主活动中执行此调用或那个调用。
view surface = new SurfaceView(this);
SetContentView(surface);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接