简体   繁体   English

GLSurfaceView onPause仅显示黑屏

[英]GLSurfaceView onPause shows only a black screen

I am developing an little OpenGL Activity, it's composed by this layout: 我正在开发一个小的OpenGL活动,它由以下布局组成:

<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>

It's shows an View like this: 它显示了这样的视图:

在此处输入图片说明

But when I press the home button and I return on my application I can see only a black screen, the GLSurfaceView and also the RelativeLayout are not displayed!! 但是当我按下主页按钮并返回到我的应用程序时,我只能看到黑屏,GLSurfaceView和RelativeLayout都不会显示!

After about two minuts system say: 经过约两分钟的系统说: 在此处输入图片说明

I am very frustrating, I call GLSurface onPause() and onResume() when my Activity calls the corresponding methods. 我非常沮丧,当我的Activity调用相应的方法时,我将调用GLSurface onPause()和onResume()。

Someone can help me!!???? 有人可以帮助我!

I post some of my code: 我发布了一些代码:

@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);
      }

This is my Handler inner class (I use it for switch texture when users performs some action) 这是我的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;

        }


    }

The DownloadImageProgressBar is a AsyncTask which downloads images from the web and performs some UI actions through the Activity Handler. DownloadImageProgressBar是一个AsyncTask,可从Web下载图像并通过活动处理程序执行一些UI操作。

There are my onPause and onResume methods: 我有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();
}

I call this methods in onSurfaceChanged and onSurfaceCreated: 我在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;
    }

I have another question, when my activity switch in background the textures images I downloaded before are deleted? 我还有一个问题,当我的活动在后台切换时,我之前下载的纹理图像会被删除吗?

It's possible that this is the issues? 这可能是问题所在吗?

When you press 'home', the GL context is lost. 当您按“ home”时,GL上下文会丢失。

From the Android documentation: 从Android文档中:

There are situations where the EGL rendering context will be lost. 在某些情况下,EGL渲染上下文将丢失。 This typically happens when device wakes up after going to sleep. 通常在设备进入睡眠状态后唤醒时会发生这种情况。 When the EGL context is lost, all OpenGL resources (such as textures) that are associated with that context will be automatically deleted. 当EGL上下文丢失时,与该上下文关联的所有OpenGL资源(例如纹理)将被自动删除。 In order to keep rendering correctly, a renderer must recreate any lost resources that it still needs. 为了保持正确的渲染,渲染器必须重新创建仍然需要的任何丢失的资源。 The onSurfaceCreated(GL10, EGLConfig) method is a convenient place to do this. onSurfaceCreated(GL10,EGLConfig)方法是执行此操作的便捷位置。

You can read about it here . 你可以在这里阅读。

To fix the problem, perform all OpenGL texture and initialization in your renderer's onSurfaceChanged . 要解决此问题,请在渲染器的onSurfaceChanged执行所有OpenGL纹理和初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM