android - 在布局中添加 GLSurfaceView

标签 android opengl-es

我正在尝试运行 android 示例 http://developer.android.com/training/graphics/opengl/index.html

我必须向 View 添加一些控件,如文本和按钮,看起来像 android ui

如何在Android布局中使用GLSurfaceView?

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mGLSurfaceView = (myGLSurfaceView) findViewById(R.id.glSurfaceViewID); //crashes 
 }

表面 View

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 在将其添加到布局时与其他 View 没有任何不同。唯一需要考虑的是,它在使用时主要是子类化的,就像在您的代码中所做的那样,而对于其他类型的 View 来说,这并不常见(但也不是非常不寻常)。

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

<GLSurfaceView
    ...

毫不奇怪,这将在布局膨胀时创建 GLSurfaceView 类的实例。但这不是你想要的。您想要创建派生类的实例,即 myGLSurfaceView。一个明显的尝试是:

<myGLSurfaceView
    ...

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

package com.msl.myglexample;

public class myGLSurfaceView extends GLSurfaceView {
    ...
}

然后布局文件中的功能入口将是:

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

这不是 OP 代码中的问题,但这是人们在尝试执行此操作时遇到的常见问题:派生的 GLSurfaceView 具有采用 的构造函数也很重要AttributeSet 作为参数。所有从 XML 扩展的 View 都需要这样做。

public myGLSurfaceView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

关于android - 在布局中添加 GLSurfaceView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26936020/

相关文章:

android - 从 fragment 对话框中显示 fragment

android - 为动态内容保持 ScrollView 滚动位置

Android Studio 3.0 和应用卡住

java - Android OpenGL-ES 纹理显示困惑

android - 丢弃对OpenGL中的程序性能有害吗?

android - 如何使用 MockWebServer 在 response.body().string() 中触发 IOException

android - 找不到文件 res/drawable/abc_ic_ab_back_material.xml

c++ - OpenGL - OBJ 中的顶点法线

opengl-es - OpenGL es 中的纹理未声明标识符

android - 旋转后平移