我想控制GLSurfaceView.Renderer的渲染率。我在类中实现了一个扩展GLSurfaceView的线程,并在一个while(true)循环中定期进入睡眠状态,这对于减慢渲染器没有任何作用。有一个很好的答案here建议使用Renderer.onDrawFrame()方法中的Thread.sleep将GL线程置于睡眠状态。我想在Renderer课外面处理它。当显式调用需要传入GL10对象时,如何才能完成?感谢。
答案 0 :(得分:3)
不要扩展GLSurfaceView。如果您还没有,请将渲染器保留为活动类中的变量:
public class MyActivity extends Activity {
protected GLSurfaceView mGLView;
protected GraphicsRenderer graphicsRenderer; // keep reference to renderer
protected void initGraphics() {
graphicsRenderer = new GraphicsRenderer(this);
mGLView = (GLSurfaceView) findViewById(R.id.graphics_glsurfaceview1);
mGLView.setEGLConfigChooser(true);
mGLView.setRenderer(graphicsRenderer);
graphicsRenderer.setFrameRate(30);
}
}
然后,您可以在渲染器中创建一个方法来控制帧速率:
public class GraphicsRenderer implements GLSurfaceView.Renderer {
private long framesPerSecond;
private long frameInterval; // the time it should take 1 frame to render
private final long millisecondsInSecond = 1000;
public void setFrameRate(long fps){
framesPerSecond = fps;
frameInterval= millisecondsInSeconds / framesPerSecond;
}
public void onDrawFrame(GL10 gl) {
// get the time at the start of the frame
long time = System.currentTimeMillis();
// drawing code goes here
// get the time taken to render the frame
long time2 = System.currentTimeMillis() - time;
// if time elapsed is less than the frame interval
if(time2 < frameInterval){
try {
// sleep the thread for the remaining time until the interval has elapsed
Thread.sleep(frameInterval - time2);
} catch (InterruptedException e) {
// Thread error
e.printStackTrace();
}
} else {
// framerate is slower than desired
}
}
}