如果我在android 4.x.x下运行我的程序(Canvas,SurfaceView),我可以获得稳定的60 FPS,但在2.3.3下,FPS会增加到75-80。 如何在android 2.3.3下轻松实现60 FPS(vsync)?
更新(一些绘图代码):
public class game extends Activity implements OnTouchListener
{
FastRenderView renderView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
renderView = new FastRenderView(this);
renderView.setOnTouchListener(this);
setContentView(renderView);
}
protected void onResume() {
super.onResume();
renderView.resume();
}
protected void onPause() {
super.onPause();
renderView.pause();
}
class FastRenderView extends SurfaceView implements Runnable {
Thread renderThread = null;
SurfaceHolder holder;
volatile boolean running = false;
public FastRenderView(Context context) {
super(context);
holder = getHolder();
private void drawSurface(Canvas canvas)
{
// Draw all
}
public void resume() {
running = true;
renderThread = new Thread(this);
renderThread.start();
}
public void pause() {
running = false;
while(true) {
try {
renderThread.join();
break;
} catch (InterruptedException e) {
// retry
}
}
renderThread = null;
}
public void run() {
while(running) {
if(!holder.getSurface().isValid())
continue;
Canvas canvas = holder.lockCanvas();
drawSurface(canvas);
holder.unlockCanvasAndPost(canvas);
}
}
}
UPDATE2: 找到简单的解决方案(谷歌):
int max_fps = 60;
int frame_period = 1000/max_fps;
long beginTime;
long timeDiff;
int sleepTime;
public void run() {
sleepTime = 0;
while(running) {
if(!holder.getSurface().isValid())
continue;
beginTime = System.currentTimeMillis();
Canvas canvas = holder.lockCanvas();
drawSurface(canvas);
holder.unlockCanvasAndPost(canvas);
timeDiff = System.currentTimeMillis() - beginTime;
sleepTime = (int)(frame_period - timeDiff); // calculate sleep time
if (sleepTime > 0) {
try { Thread.sleep(sleepTime); } catch (InterruptedException e) {}
}
}
答案 0 :(得分:1)
vsync是在果冻豆释放(项目黄油)时引入的。在较旧的Android版本上无法做到这一点。如果你想要完整的故事,请看一下这个视频:http://www.youtube.com/watch?v=Q8m9sHdyXnE。值得关注!