以下是我的工作方式:
在XML文件(mine is main.xml)中,使用扩展类规范
<com.domain.project.VortexView android:id="@+id/vortexview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
在你的活动课上:
setContentView(R.layout.main);
VortexRenderer _renderer=new VortexRenderer(); // setup our renderer
VortexView glSurface=(VortexView) findViewById(R.id.vortexview); // use the xml to set the view
glSurface.setRenderer(_renderer); // MUST BE RIGHT HERE, NOT in the class definition, not after any other calls (see GLSurfaceView.java for related notes)
glSurface.showRenderer(_renderer); // allows us to access the renderer instance for touch events, etc
视图定义(VoReVIEW.java):
public class VortexView extends GLSurfaceView {
public VortexRenderer _renderer; // just a placeholder for now
public VortexView(Context context) { // default constructor
super(context);
}
public VortexView(Context context, AttributeSet attrs) { /*IMPORTANT - this is the constructor that is used when you send your view ID in the main activity */
super(context, attrs);
}
public void showRenderer(VortexRenderer renderer){ // sets our local object to the one created in the main activity, a poor man's getRenderer
this._renderer=renderer;
}
public boolean onTouchEvent(final MotionEvent event) { // An example touchevent from the vortex demo
queueEvent(new Runnable() {
public void run() {
_renderer.setColor(event.getX() / getWidth(), event.getY() / getHeight(), 1.0f);
}
});
return true;
}
}
vortexrenderer.java只有典型的onsurfacexxxxx调用。
无论如何,这似乎允许我在扩展的glsurface上堆叠其他XML定义的视图,这正是我首先想要的。
干杯!