首页 > 解决方案 > 将 openGL 参数传递给 AsyncTask

问题描述

如何正确地将 javax.microedition.khronos.opengles.GL10 作为参数传递给 AsyncTask。这是我尝试过但没有用的代码。如果我直接将参数传递给 SavePNG 方法,它就可以工作。有什么线索吗?谢谢。

这是我的代码:

if (mTakeScreenshot) {
    mTakeScreenshot = false;
    MyTaskParams params = new MyTaskParams(0, 0, mViewWidth, mViewHeight, gl);
    MyTask myTask = new MyTask();
    myTask.execute(params);
}

private static class MyTaskParams {
    int x;
    int y;
    int w;
    int h;
    GL10 gl;

    MyTaskParams(int x, int y, int w, int h, GL10 gl) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
        this.gl = gl;
    }
}

public class MyTask  extends AsyncTask<MyTaskParams, Void, Void> {

    @Override
    protected Void doInBackground(MyTaskParams... params) {
        int x = params[0].x;
        int y = params[0].y;
        int w = params[0].w;
        int h = params[0].h;
        GL10 gl = params[0].gl;

        SavePNG(0, 0, w,  h,  gl);

        return null;
    }
}

标签: android-asynctaskvariadic-functionsprimitive

解决方案


OpenGL 帧缓冲区操作是单线程的,因此您需要使用回调函数并在必要时调用它,通常在按键或按钮按下回调函数中。

IE

void main()
{
   //GL Operations

   //GLswapbuffers

   //call here or in key / button press callback function
   doInBackground(params);
}

 protected Void doInBackground(MyTaskParams... params) {
        int x = params[0].x;
        int y = params[0].y;
        int w = params[0].w;
        int h = params[0].h;
        GL10 gl = params[0].gl;

        SavePNG(0, 0, w,  h,  gl);
    }

推荐阅读