【问题标题】:when is a SurfaceView created from a layout (programmatically)?何时从布局(以编程方式)创建 SurfaceView?
【发布时间】:2014-01-22 13:58:55
【问题描述】:

我想知道 SurfaceView 是什么时候从布局中实际创建的?

这是在从 Google Glass 上的 LiveCard 服务创建布局的场景中 - 因此没有可以使用 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() 从未在上面调用过。

要做到这一点需要什么?

【问题讨论】:

    标签: android surfaceview google-glass android-framelayout


    【解决方案1】:

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

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-01
      • 2015-10-17
      • 2019-06-01
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      相关资源
      最近更新 更多