我正在尝试创建一个显示游戏区域 map 的 GLSurfaceView。当玩家移动时,游戏 Activity 会调用 highlightSpot,这又会触发渲染请求。我唯一想重新绘制 View 的时间是玩家移动时。
然而,对于我当前的实现,尽管在我的 GLSurfaceView 上调用了 setRenderMode(RENDERMODE_WHEN_DIRTY)
,它的渲染模式似乎仍然是连续的。为了检查,我在我的 onDrawFrame 方法中加入了一个 println 语句,当我运行我的应用程序时,输出很快填满了我的 logcat,而玩家甚至没有移动一次——它显然没有按照我的预期运行。为了让 View 仅在被询问时呈现,我还需要做些什么吗?
(大部分代码源自 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();
}
}
最佳答案
好的,我想我已经解决了这个问题。设置渲染模式的地方似乎是包含您的 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();
}
}
关于android - 尽管更改渲染模式,GLSurfaceView 仍持续渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4330521/