如何加载TextureView作为视图的一部分

时间:2018-03-01 04:19:47

标签: android textureview

我想加载TextureView作为布局的一部分。我在一些例子中看到它在TextureView函数中使用setContentView

    ...
    TextureView textureView = new TextureView(this);
    textureView.setSurfaceTextureListener(this);

    setContentView(textureView);

但我希望将此textureView加载为xml布局的一部分。我怎么办?

2 个答案:

答案 0 :(得分:0)

试试这个你可以直接在你的布局中添加TextureView,如下所示

<LinearLayout
    android:id="@+id/rootView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    android:orientation="vertical">

    <TextureView
        android:id="@+id/textureView1"
        android:layout_width="350dp"
        android:layout_height="350dp"
        android:layout_below="@+id/textView1" />
</LinearLayout>

或者您可以在此布局中动态添加TextureView

public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener {

    LinearLayout rootLinearLayout;

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

        rootLinearLayout = findViewById(R.id.rootView);
        TextureView textureView = new TextureView(this);
        textureView.setSurfaceTextureListener(this);
        rootLinearLayout.addView(textureView);


    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {

    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {

    }
}

答案 1 :(得分:0)

您可以直接使用TextureView在布局中使用它,并使用findViewById()获取它。

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextureView
        android:id="@+id/texture_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>