【问题标题】:Adding GLSurfaceView in layout在布局中添加 GLSurfaceView
【发布时间】:2020-09-13 14:38:40
【问题描述】:

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

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

如何在Android布局中使用GLSurfaceView?

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

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

表面视图

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" />

【问题讨论】:

    标签: android opengl-es


    【解决方案1】:

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

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

    <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 膨胀的视图都是必需的。

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

    【讨论】:

    • 这应该没问题,但它不能正常工作,因为 GLSurfaceView 必须使用没有属性的默认构造函数进行实例化。
    【解决方案2】:

    在布局编辑器中使用子框架布局并从代码中添加自定义 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多