问题描述
我要控制我的GLSurfaceView.Renderer的渲染速度。我实现了在扩展GLSurfaceView类的一个线程,并把它在一段时间(真)循环,它没有做任何渲染减缓周期性睡觉。有一个很好的答案here该建议把GL线程由Renderer.onDrawFrame()方法中使用视频下载睡觉。我想从渲染器类的外部处理。怎样才能当的显式调用,需要传递一个GL10对象做什么?谢谢你。
I want to control the rendering rate of my GLSurfaceView.Renderer. I implemented a thread in the class that extends GLSurfaceView, and put it to sleep periodically in a while(true) loop, which did nothing to slow down the renderer. There's a good answer here that suggests putting the GL Thread to sleep by using a Thread.sleep within the Renderer.onDrawFrame() method. I'd like to handle it from outside the Renderer class. How can that be done when an explicit call requires passing in a GL10 object? Thanks.
推荐答案
不要延长GLSurfaceView。如果你没有准备好,保持渲染器作为一个变量在你的活动类:
Don't extend GLSurfaceView. If you're not already, keep the renderer as a variable in your activity class:
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);
}
}
然后你就可以在你的渲染器,控制帧速率创建一个方法:
Then you can create a method in your renderer to control the frame rate:
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
}
}
}
这篇关于机器人 - 把GLSurfaceView.Renderer睡(如在视频下载(20))的OnDrawFrame方法以外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!