Android 在活动中使用GlSurfaceview
我有一个活动,我将活动的内容视图设置为“R.layout.main.xml”。我还有一个类,其中包含使用openGL创建的动画。现在我需要在活动的背景中使用此动画 代码是这样的Android 在活动中使用GlSurfaceview,android,opengl-es,android-view,glsurfaceview,Android,Opengl Es,Android View,Glsurfaceview,我有一个活动,我将活动的内容视图设置为“R.layout.main.xml”。我还有一个类,其中包含使用openGL创建的动画。现在我需要在活动的背景中使用此动画 代码是这样的 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_pixie); mGLView = new
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_pixie);
mGLView = new ClearGLSurfaceView(this);
setContentView(mGLView);
}
但是我的应用程序正在崩溃。我如何解决这个问题。当你第二次调用
setContentView()
时,你会替换第一次设置的内容,只留下背景。崩溃很可能是因为您依赖于主布局中的元素,而主布局中的元素已被删除
与其调用两次setContentView()
,不如在主布局中包含GLSurfaceView
。下面是一个如何做到这一点的示例:
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent>
<your.application.package.ClearGLSurfaceView
android:layout_width="match_parent"
android:layout_width="match_parent"/>
<!--put the rest of your layout here, i.e the contents of the original R.layout.main_pixie-->
</FrameLayout>
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_pixie_new);
}