希望它可以成为您问题的答案
MyGlSurfaceView sv = (MyGlSurfaceView)findViewById(R.id.gl_surface_view);
yourButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
sv = new MySurfaceView(yourContext);
}
});
这是我的第一个答案XD
添加:
public class MainActivity extends Activity{
MyGLSurfaceView myGl;
Button button;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context= this;
setContentView(R.layout.main_activity);
myGl = (MyGLSurfaceView)findViewById(R.id.main_gl);
button = (Button)findViewById(R.id.main_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myGl = new MyGLSurfaceView(context);
}
});
}
}
public class MyGLSurfaceView extends GLSurfaceView {
public MyGLSurfaceView(Context context) {
super(context);
this.setRenderer(new MyRenderer());
}
public MyGLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setRenderer(new MyRenderer());
}
static class MyRenderer implements GLSurfaceView.Renderer{
public void onSurfaceChanged(GL10 gl, int w, int h) {
gl.glViewport(0, 0, w, h);
}
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// 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" >
<Button
android:id="@+id/main_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<com.renderer.test.MyGLSurfaceView
android:id="@+id/main_gl"
android:layout_width="300dp"
android:layout_height="200dp" />
</LinearLayout>
我不确定这是否适合您的情况,但它不会在我的手机上出错。