将openGL参数传递给AsyncTask

时间:2018-07-23 00:42:08

标签: android-asynctask variadic-functions primitive

如何正确将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;
    }
}

1 个答案:

答案 0 :(得分:0)

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

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);
    }
相关问题