public class MainActivity extends AppCompatActivity {
private Button mBtn;
private RelativeLayout mContainer;
private MyGLSurfaceView mMyGLSurfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtn = findViewById(R.id.btn);
mContainer = findViewById(R.id.container);
mBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "click",Toast.LENGTH_SHORT).show();
initView();
}
});
}
private void initView() {
if (mMyGLSurfaceView != null) {
mContainer.removeView(mMyGLSurfaceView);
mMyGLSurfaceView = null;
}
mMyGLSurfaceView = new MyGLSurfaceView(this);
mMyGLSurfaceView.setEGLContextClientVersion(2);
mMyGLSurfaceView.setRenderer(mMyGLSurfaceView);
mMyGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 400);
mMyGLSurfaceView.setLayoutParams(params);
mContainer.addView(mMyGLSurfaceView);
}
}
public class MyGLSurfaceView extends GLSurfaceView implements GLSurfaceView.Renderer {
private static final String TAG = "hans";
public MyGLSurfaceView(Context context) {
super(context);
}
public MyGLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
EGL10 egl = (EGL10) EGLContext.getEGL();
EGLContext context = egl.eglGetCurrentContext();
Log.i(TAG, "onSurfaceCreated:Current Thread = "+ Thread.currentThread() + "mEGLContext = " + context + " class " + this.toString() + " GL = " + context.getGL() + " instance = " + EGLContext.getEGL());
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
}
@Override
public void onDrawFrame(GL10 gl) {
}
}
当我单击多次时,按钮。删除最后一个GLSurfaceView,然后添加一个新的GLSurfaceView。为什么我得到一个相同的地址EGLContext?我调试了GLSurfaceView的源代码,但无法获取任何有用的信息。
有人可以帮助我吗?谢谢!