如何加载 TextureView 作为视图的一部分
How to load TextureView as a part of view
我想加载 TextureView
作为布局的一部分。我在一些示例之前看到它在 setContentView
函数中使用 TextureView
。
...
TextureView textureView = new TextureView(this);
textureView.setSurfaceTextureListener(this);
setContentView(textureView);
但我想加载此 textureView
作为 xml 布局的一部分。我该怎么办?
试试这个,你可以像这样在你的布局中直接添加 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) {
}
}
你可以直接用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>
我想加载 TextureView
作为布局的一部分。我在一些示例之前看到它在 setContentView
函数中使用 TextureView
。
...
TextureView textureView = new TextureView(this);
textureView.setSurfaceTextureListener(this);
setContentView(textureView);
但我想加载此 textureView
作为 xml 布局的一部分。我该怎么办?
试试这个,你可以像这样在你的布局中直接添加 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) {
}
}
你可以直接用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>