在 Android 中结合 OpenGL ES 渲染和 UI 元素
Combining the OpenGL ES rendering and UI-elements in Android
Android 应用程序使用 android.opengl.GLSurfaceView 渲染 OpenGL ES:
public class GameActivity extends AppCompatActivity {
private SurfaceView surfaceView;
@Override
protected void onCreate(Bundle state) {
...
surfaceView = new SurfaceView(this);
setContentView(surfaceView);
}
}
public class SurfaceView extends GLSurfaceView {
private final SceneRenderer renderer;
public SurfaceView(Context context) {
...
renderer = new SceneRenderer(context);
setRenderer(renderer);
}
}
如何在 OpenGL ES 渲染上显示 Android UI 元素(如文本标签)?
需要使用:
public class GameActivity extends AppCompatActivity {
private SurfaceView surfaceView;
@Override
protected void onCreate(Bundle state) {
setContentView(R.layout.activity_gl);
surfaceView = findViewById(R.id.oglView);
surfaceView.init(this.getApplicationContext());
}
}
public class SurfaceView extends GLSurfaceView {
private SceneRenderer renderer;
public SurfaceView(Context context) {
super(context);
}
public SurfaceView(Context context, AttributeSet attributes) {
super(context, attributes);
}
public void init(Context context) {
renderer = new SceneRenderer(context);
setRenderer(renderer);
...
}
}
并创建布局 activity_gl.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
tools:context=".activities.GameActivity">
<com.app.SurfaceView
android:id="@+id/oglView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView ... />
</androidx.constraintlayout.widget.ConstraintLayout>
要从渲染线程更新元素,可以使用Handler/Looper。
Android 应用程序使用 android.opengl.GLSurfaceView 渲染 OpenGL ES:
public class GameActivity extends AppCompatActivity {
private SurfaceView surfaceView;
@Override
protected void onCreate(Bundle state) {
...
surfaceView = new SurfaceView(this);
setContentView(surfaceView);
}
}
public class SurfaceView extends GLSurfaceView {
private final SceneRenderer renderer;
public SurfaceView(Context context) {
...
renderer = new SceneRenderer(context);
setRenderer(renderer);
}
}
如何在 OpenGL ES 渲染上显示 Android UI 元素(如文本标签)?
需要使用:
public class GameActivity extends AppCompatActivity {
private SurfaceView surfaceView;
@Override
protected void onCreate(Bundle state) {
setContentView(R.layout.activity_gl);
surfaceView = findViewById(R.id.oglView);
surfaceView.init(this.getApplicationContext());
}
}
public class SurfaceView extends GLSurfaceView {
private SceneRenderer renderer;
public SurfaceView(Context context) {
super(context);
}
public SurfaceView(Context context, AttributeSet attributes) {
super(context, attributes);
}
public void init(Context context) {
renderer = new SceneRenderer(context);
setRenderer(renderer);
...
}
}
并创建布局 activity_gl.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
tools:context=".activities.GameActivity">
<com.app.SurfaceView
android:id="@+id/oglView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView ... />
</androidx.constraintlayout.widget.ConstraintLayout>
要从渲染线程更新元素,可以使用Handler/Looper。