为什么我们只应在" onDrawFrame"中使用GLSurfaceView上的快照?方法?否则,它将以黑色空图像结束

时间:2018-05-20 06:39:53

标签: android bitmap snapshot glsurfaceview

目前我正试图从Android上的GLSurfaceView获取快照。我的方法是生成一个位图,显示GLSurfaceView的实时内容。我使用的实际算法是:

Bitmap createBitmapFromView(int x, int y, int width, int height, GL10 gl) throws OutOfMemoryError{
    int buffer[] = new int[width * height];
    int source[] = new int[width * height];

    IntBuffer intBuffer = IntBuffer.wrap(buffer);
    intBuffer.position(0);

    try{
        gl.glReadPixels(x, y, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, intBuffer);

        int offset1, offset2;
        for(int i = 0; i < height; i++){
            offset1 = i * width;
            offset2 = (height - i - 1) * width;

            for(int j = 0; j < width; j++){
                int texturePixel = buffer[offset1 + j];

                int blue = (texturePixel >> 16) & 0xff;
                int red = (texturePixel << 16) & 0x00ff0000;
                int pixel = (texturePixel & 0xff00ff00) | red | blue;

                source[offset2 + j] = pixel;
            }
        }
    } catch(GLException e){
        return null;
    }

    return Bitmap.createBitmap(source, width, height, Bitmap.Config.ARGB_8888);
}

然而,至于这种方法的用法,我最初的想法是使从GLSurfaceView扩展的类创建一个新的GL10实例进行显示。在表面创建之后,我在方法&#34; onSurfaceCreated&#34;上面调用了这个函数。然后,我发现现在的每个快照都是黑色的全空,看起来图像数据永远不会通过这个GLSurfaceView。

在使用该算法的不同地方进行了几次其他测试后,我偶然发现只在方法&#34; onDrawFrame&#34;中调用此函数。所以我只是在GLSurfaceView的渲染器中设置一个布尔标志来标记当前我们是否需要快照。由于GLSurfaceView将继续调用&#34; onDrawFrame&#34;刷新的函数,每当标志为真时,我都会执行快照并将标志设置为false。它终于奏效了。

因此,我的问题是:为什么我们只应调用该方法使其在&#34; onDrawFrame&#34;而不是任何可以获得GL10实例的地方?有人知道调用快照方法的不同地方有什么区别吗?

0 个答案:

没有答案