【问题标题】:GLSurfaceView continuously renders despite changing render mode尽管更改了渲染模式,GLSurfaceView 仍会持续渲染
【发布时间】:2011-05-18 20:21:05
【问题描述】:

我正在尝试创建一个显示游戏区域地图的 GLSurfaceView。当玩家移动时,游戏 Activity 会调用 highlightSpot,这又会触发渲染请求。我想重新绘制视图的唯一时间是玩家移动时。

但是,在我当前的实现中,尽管在我的 GLSurfaceView 上调用了setRenderMode(RENDERMODE_WHEN_DIRTY),但它的渲染模式似乎仍然是连续的。为了检查,我在我的 onDrawFrame 方法中抛出了一个 println 语句,当我运行我的应用程序时,输出很快就填满了我的 logcat,而玩家甚至没有移动一次——它显然没有按照我的预期运行。为了使视图仅在被询问时呈现,我还需要做些什么吗?

(这段代码的大部分来自http://insanitydesign.com/wp/projects/nehe-android-ports/ 的教程。为了简洁起见,我省略了我的 onDrawFrame、OnSurfaceChanged 和 onSurfaceCreated 方法,因为我没有更改渲染模式或在其中的任何地方请求渲染方法。如果有人认为它可能相关,我也可以发布。)

public class SurfaceViewClass extends GLSurfaceView implements Renderer {
    public SurfaceViewClass(Context context) {
        super(context);

        ...

        this.setRenderer(this);
        this.setRenderMode(RENDERMODE_WHEN_DIRTY);
    }

    public void highlightSpot(int x, int y) {
        /* change some variables here */
        ...

        this.requestRender();
    }
}

【问题讨论】:

    标签: android opengl-es glsurfaceview


    【解决方案1】:

    好的,我想我已经解决了这个问题。设置渲染模式的地方似乎是包含您的 GLSurfaceView 对象的类,而不是在 GLSurfaceView 构造函数中。另外(我认为我在the Android documentation for GLSurfaceView 中忽略了这一点)在设置渲染器之前不能设置 GLSurfaceView 的渲染模式。这也许就是为什么尝试在构造函数中设置渲染模式不起作用的原因。

    这似乎迫使它只在我想要的时候渲染,这正是我想要的:

    public class Game extends Activity {
    private GLSurfaceView glSurface;
    private SurfaceViewClass svc;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        glSurface = (GLSurfaceView) findViewById(R.id.SurfaceView01);
    
        svc = new SurfaceViewClass(this);
        glSurface.setRenderer(svc);
        glSurface.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }
    
    public void movePlayer() {
        svc.highlightSpot(location[PLAYER], 0);
        glSurface.requestRender();
    }
    }
    

    【讨论】:

    • 注意:修复此问题后,我还删除了 highlightSpot 函数中的 requestRender 语句,因为我现在从 movePlayer() 调用 requestRender。我相信它可以在 highlightSpot 中正常工作,但我只需要调用一次。
    • 实际上,不,它在highlightSpot()中不起作用,因为它需要在GLSurfaceView对象上调用,而不是在我用作渲染器的SurfaceViewClass对象上调用。如果我没有把这两者混为一谈,我可能完全不必问这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    相关资源
    最近更新 更多