Adding GLSurfaceView in layout

牧云@^-^@ 提交于 2020-01-15 13:19:59

问题


I am trying to run android samples http://developer.android.com/training/graphics/opengl/index.html

I have to add some controls like text and buttons to the view which will look like

How to use GLSurfaceView in Android layout?

Main Activity (skipped some auto generated code)

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

Surface 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

}

}

Layout

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


回答1:


GLSurfaceView isn't any different from other views when it comes to adding it to a layout. The only consideration is that it's mostly subclassed when it's used, like it's done in your code, while that's not quite as common (but also not terribly unusual) for other types of views.

The problem in your attempt is the class name used when adding it to the layout:

<GLSurfaceView
    ...

Not surprisingly, this would create an instance of the GLSurfaceView class when the layout is inflated. But that's not what you want. You want to create an instance of your derived class, which is myGLSurfaceView. An obvious attempt to do this would be:

<myGLSurfaceView
    ...

This is not going to work yet. The class name needs to be qualified with the package name. Say your myGLSurfaceView class is part of the com.msl.myglexample package:

package com.msl.myglexample;

public class myGLSurfaceView extends GLSurfaceView {
    ...
}

Then the functional entry in the layout file will be:

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

This is not a problem in the OP's code, but since it's a common issue people encounter when trying to do this: It's also critical that your derived GLSurfaceView has a constructor that takes an AttributeSet as an argument. This is needed for all views that get inflated from XML.

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


来源:https://stackoverflow.com/questions/26936020/adding-glsurfaceview-in-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!