通过从UI线程指令来渲染线程(GLSurfaceView) [英] passing instructions from UI thread to Rendering thread (GLSurfaceView)

查看:773
本文介绍了通过从UI线程指令来渲染线程(GLSurfaceView)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的设置:作为图像中显示有GLSurfaceView和一个按钮的RelativeLayout ..

The Setup : A RelativeLayout with a GLSurfaceView and a Button as shown in the image..

问题:可以说我有其他的三角模型(一个在画面作为初始模型)......我希望周期性改变按钮的点击模式。由于按钮是UI线程上运行glSurfaceView在一个单独的线程,我不知道究竟怎样的信息/指令传递给它。我知道有这个东西叫做处理器 Android中这可能是有用这种情况下,...但我在这里需要一些帮助。

The Problem: Lets say I have other triangle models (The one in the picture being the initial model)... I wish to change the models cyclically on click of the button. Since button is on the UI thread and glSurfaceView runs on a separate thread, I don't exactly know how to pass the info/instruction to it. I know there is this thing called Handler in Android which might be useful in this case... But I need some help here..

编辑:如果处理程序是正确的方式,我需要知道如何添加的活套以该处理程序...文档说添加活套。prepare()在运行开始()方法..但glSurfaceView创建线程含蓄,导致无run()方法直接使用。

If Handler is the right way, I need to know how to add Looper to that Handler... The documentation says add looper.prepare() at the start of run() method.. But the glSurfaceView creates thread implicitly, resulting in no run() method directly available..

推荐答案

我不认为这是必要的使用处理程序来解决这个问题,但你可能需要调整您组织类的方式。

I don't think it is necessary to use handlers to solve this issue but you may need to adjust the way you organise your classes.

下面是一个可能解决您的问题的组织结构的一个例子:

Here is an example of an organisational structure that might solve your issue:

活动课

public class MainActivity extends Activity {

    private int modelNumber = 0;
    private ArrayList<Model> models = new ArrayList<Model>();
    private YourRendererClass renderer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...

        // Setup GLSurfaceView
        GLSurfaceView surface = new GLSurfaceView(this);
        setContentView(surface);

        renderer = new YourRendererClass();
        surface.setRenderer(renderer);

        // Set up models
        models.add(new Model(x, y, size etc..));
        models.add(new Model(x, y, size etc..));
        models.add(new Model(x, y, size etc..));
        etc.

        // Display first model
        renderer.setCurrentModel(models.get(modelNumber));

        ...
    }

    // Called by the button press:
    // Use android:onClick="onClick"
    // in your layout xml file within button
    public void onClick(View view){
        // Make it loop round
        modelNumber++;
        if(modelNumber>=models.size()){
            modelNumber=0;
        }

        // Display current model
        renderer.setCurrentModel(models.get(modelNumber));

    }
}

渲染器类

public class YourRendererClass implements Renderer {

    private Model currentModel;

    @Override
    public void onDrawFrame(GL10 gl) {

        // ** Your existing set-up code **//

        // Draw model
        if (currentModel!=null){
            currentModel.draw(gl);
        }

    }

    public void setCurrentModel(Model model){
        currentModel = model;
    }

}

Model类

public class Model {
    // Holds model information
    private int size;
    private int x;
    private int y;
    // etc...

    public model(int x, int y, int size etc...){
        this.x=x;
        this.y=y;
        this.size=size;
        // etc....
    }

    public void draw(GL10 gl) {
        // ** Draw model based on model information fields above **
    }
}

以上code是未经检验的,因为我没有访问到您的绘图code,但如果正确实施的结构应该工作。我试图说清楚,你就必须插入自己的code,使其工作。尤其是我不知道是什么定义了每个不同的模式,所以你需要包括Model类中有足够的本地变量来定义它们。

The above code is untested as I don't have access to your drawing code but the structure should work if implemented correctly. I've tried to make it clear where you'll have to insert your own code to make it work. In particular I wasn't sure what defines each of your different models so you'll need to include sufficient local variables within the Model class to define them.

我希望我的回答可以帮助,让我知道如果你有任何问题。

I hope my answer helps, let me know if you have any questions.

这篇关于通过从UI线程指令来渲染线程(GLSurfaceView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆