Android 在布局中添加GLSurfaceView

Android 在布局中添加GLSurfaceView,android,opengl-es,Android,Opengl Es,我正在尝试运行android示例 我必须在视图中添加一些控件,如文本和按钮 如何在Android布局中使用GLSurfaceView 主要活动(跳过一些自动生成的代码) 曲面视图 public class myGLSurfaceView extends GLSurfaceView { private myRenderer mRenderer; @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-

我正在尝试运行android示例

我必须在视图中添加一些控件,如文本和按钮

如何在Android布局中使用GLSurfaceView

主要活动(跳过一些自动生成的代码)

曲面视图

public class myGLSurfaceView extends GLSurfaceView {
private myRenderer mRenderer;

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    return super.onTouchEvent(event);
}

public myGLSurfaceView (Context context) {
    super(context);
     // Create an OpenGL ES 2.0 context.
    setEGLContextClientVersion(2);


    // Creating a Renderer
    setRenderer(new myRenderer(context));       

    // Render the view only when there is a change in the drawing data
    setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}

public myGLSurfaceView (Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

public void setRenderer(myRenderer mRenderer, float density) {
    // TODO Auto-generated method stub

}

}
布局

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

<Button
    android:id="@+id/buttonID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:text="A Button" />

<GLSurfaceView
    android:id="@+id/glSurfaceViewID"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.23" />


GLSurfaceView
在将其添加到布局时与其他视图没有任何不同。唯一需要考虑的是,在使用它时,它大部分都是子类化的,就像在代码中一样,而对于其他类型的视图来说,这并不常见(但也不是非常罕见)

尝试中的问题是将其添加到布局时使用的类名:

<GLSurfaceView
    ...
这还不行。类名需要用包名限定。假设您的
myGLSurfaceView
类是
com.msl.myglexample
包的一部分:

package com.msl.myglexample;

public class myGLSurfaceView extends GLSurfaceView {
    ...
}
布局文件中的功能条目将为:

<com.msl.myglexample.myGLSurfaceView
    ....

在布局编辑器中使用子框架布局,并从代码向其添加自定义gl视图

class MyGLSurfaceView(context: Context?) : GLSurfaceView(context) {
    private val renderer: DiceRenderer

    init {
        setEGLContextClientVersion(2)
        renderer = DiceRenderer()
        setRenderer(renderer)
        setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY)
    }
}

class PlayActivity : ...() {
    private var mySurfaceView: MyGLSurfaceView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
       ...
        mySurfaceView = MyGLSurfaceView(this)
        val frame = findViewById<FrameLayout>(R.id.someFrameId)
        frame.addView(mySurfaceView)
类MyGLSurfaceView(上下文:上下文?):GLSurfaceView(上下文){
私有val渲染器:dice渲染器
初始化{
setEGLContextClientVersion(2)
renderer=renderer()
设置渲染器(渲染器)
setRenderMode(GLSurfaceView.RENDERMODE_)
}
}
班级活动:…(){
私有变量mySurfaceView:MyGLSurfaceView?=null
重写创建时的乐趣(savedInstanceState:Bundle?){
...
mySurfaceView=MyGLSurfaceView(此)
val frame=findViewById(R.id.someFrameId)
frame.addView(mySurfaceView)

这应该可以,但它不能正常工作,因为GLSURFACHEVIEW必须使用没有属性的默认构造函数实例化。
<com.msl.myglexample.myGLSurfaceView
    ....
public myGLSurfaceView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
class MyGLSurfaceView(context: Context?) : GLSurfaceView(context) {
    private val renderer: DiceRenderer

    init {
        setEGLContextClientVersion(2)
        renderer = DiceRenderer()
        setRenderer(renderer)
        setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY)
    }
}

class PlayActivity : ...() {
    private var mySurfaceView: MyGLSurfaceView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
       ...
        mySurfaceView = MyGLSurfaceView(this)
        val frame = findViewById<FrameLayout>(R.id.someFrameId)
        frame.addView(mySurfaceView)