1
如果我在android 4.x.x下運行我的程序(Canvas,SurfaceView),我得到穩定的60 FPS,但在2.3.3下FPS增加到75-80。 如何使在android 2.3.3下更容易60 FPS(vsync)?android 2.3:如何將fps降低到60(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) {}
}
}
你的意思是製作60FPS的上限?向我們展示您的繪圖代碼。 – Raptor
是的,60FPS的上限。 Shivan猛禽,請告訴,解決方案好嗎?或者可以更好? – user2534722