我正在开发一个小的OpenGL Activity,它由这个布局组成:
<RelativeLayout
android:id="@+id/vt_layout_opengl"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
>
<android.opengl.GLSurfaceView
android:id="@+id/surface_vt"
android:layout_width="900dp"
android:layout_height="285dp"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/surface_vt"
android:layout_marginTop="5dp"
android:background="@drawable/barra_menu_vt"
>
<ImageButton android:id="@+id/vt_left_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:background="@drawable/prev_a"
android:onClick="prec"
/>
<ImageButton android:id="@+id/vt_right_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/next_a"
android:layout_toRightOf="@+id/vt_left_arrow"
android:layout_marginLeft="20dp"
android:onClick="next"
/>
<TextView
android:id="@+id/vt_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/vt_right_arrow"
android:layout_marginLeft="30dp"
android:maxLength="30"
android:singleLine="true"
android:ellipsize="marquee"
/>
<ImageButton android:id="@+id/vt_information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/vt_addbookmarks"
android:layout_marginRight="20dp"
android:background="@drawable/info_a"
android:onClick="info"
/>
<ImageButton android:id="@+id/vt_addbookmarks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/vt_preferences"
android:layout_marginRight="20dp"
android:background="@drawable/book_a"
android:onClick="bookmark"
/>
<ImageButton android:id="@+id/vt_preferences"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:background="@drawable/sett_a"
android:onClick="settings"
/>
</RelativeLayout>
</RelativeLayout>
它显示了这样的视图:
但是当我按下主页按钮并返回我的应用程序时,我只能看到黑屏,GLSurfaceView和RelativeLayout都没有显示!!
约两分钟后系统说:
我很沮丧,当我的Activity调用相应的方法时,我调用GLSurface onPause()和onResume()。
有人可以帮助我!! ????
我发布了一些代码:
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG,"onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.virtualtour);
loading_ly = (RelativeLayout) findViewById(R.id.vt_layout_loading);
panorama_description = (TextView) findViewById(R.id.vt_description);
virtualtour_relatively = (RelativeLayout) findViewById(R.id.vt_layout_opengl);
vt_handler = new VirtualTourHandler(this);
ext_container = new GLExtensionContainer();
th_checkGL = new ThreadCheckOpenGL(ext_container, extensions_requested,vt_handler);
th_checkGL.start();
mFacebook = new Facebook(ArounderTouchMainActivity.APP_ID);
mAsyncRunner = new AsyncFacebookRunner(mFacebook);
msg = vt_handler.obtainMessage();
output_thread = msg.getData();
output_thread.putInt(VirtualTour.TYPE_TAG, VirtualTour.START_TAG);
vt_handler.handleMessage(msg);
}
这是我的Handler内部类(当用户执行某些操作时,我将它用于切换纹理)
@Override
public void handleMessage(Message msg) {
bd = msg.getData();
type = bd.getInt(TYPE_TAG);
bd = msg.getData();
switch (type) {
case GL_EXTENSIONS_CHECK_TAG:
showDialog(GL_EXTENSIONS_CHECK_DIALOG);
break;
case START_TAG:
Log.d(TAG, "START_TAG");
loading_ly.setVisibility(View.VISIBLE);
virtualtour_relatively.setVisibility(View.GONE);
progress_bar.setProgress(0);
thread = new DownloadImageProgressBar(vt_handler,getActivity());
thread.execute(panorama_input.get(position).getUrl_virtual());
break;
case BAR_TAG:
progress = bd.getInt(PROGRESS_TAG);
if(progress <= 100)
progress_bar.setProgress(progress);
break;
case SHUTDOWN_TAG:
finish();
break;
case SHOW_TAG:
Log.d(TAG, "SHOW_TAG");
if(first_visualization){
glSurface = (GLSurfaceView) findViewById(R.id.surface_vt);
glSurface.setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR);
renderer = new OpenGLRenderer(first_data,second_data,sphere,vt_handler,ext_container, ( (panorama_input.get(position)).getType().compareTo(CardImage.SPHERE) == 0));
glSurface.setRenderer(renderer);
first_visualization = false;
}
// potrebbe essere fatto molto meglio: ricorda!
panorama_description.setText(panorama_input.get(position).getTitolo());
loading_ly.setVisibility(View.GONE);
virtualtour_relatively.setVisibility(View.VISIBLE);
renderer.setImage(first_data, second_data,( (panorama_input.get(position)).getType().compareTo(CardImage.SPHERE) == 0));
renderer.resetZoom();
renderer.resetAngle();
renderer.startTimer();
enable_touch = false;
case NOCONNECTION_ERROR_TAG:
showDialog(OUTOFMEMORY_ERROR_DIALOG);
break;
case OUTOFMEMORY_ERROR_TAG:
showDialog(OUTOFMEMORY_ERROR_DIALOG);
break;
default:
break;
}
return;
}
}
DownloadImageProgressBar是一个AsyncTask,它从Web下载图像并通过Activity Handler执行一些UI操作。
有我的onPause和onResume方法:
@Override
protected void onResume() {
Log.d(TAG,"onResume()");
super.onResume();
if(glSurface != null)
glSurface.onResume();
}
@Override
protected void onPause() {
Log.d(TAG,"onPause()");
super.onPause();
glSurface.onPause();
}
我在onSurfaceChanged和onSurfaceCreated中调用此方法:
public void setGLTexture(GL10 gl10) {
GL11 gl = (GL11) gl10;
//Generate one texture pointer...
gl.glGenTextures(2, textures_id);
gl.glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
//...and bind it to our array
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures_id.get(0));
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE );
gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE );
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, first, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures_id.get(1));
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE );
gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE );
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, second, 0);
return;
}
我有另一个问题,当我的活动在后台切换我之前下载的纹理图像时会被删除吗?
这可能是问题吗?
答案 0 :(得分:6)
按“主页”时,GL上下文将丢失。
来自Android文档:
有些情况下EGL 渲染上下文将丢失。这个 通常在设备唤醒时发生 睡觉后。当EGL 所有OpenGL资源都丢失了上下文 (例如纹理)相关联 有了这样的背景 自动删除。为了 保持正确渲染,一个渲染器 必须重新创建任何丢失的资源 它仍然需要。该 onSurfaceCreated(GL10,EGLConfig) 方法是一个方便的地方 此
您可以阅读here。
要解决此问题,请在渲染器的onSurfaceChanged
中执行所有OpenGL纹理和初始化。