I’m developing an android game and I’m using SurfaceView. I have a method that will be called every 16ms (I want to have 60fps)
public void myDraw(SurfaceHolder holder) {
Canvas c = null;
long start = System.currentMillis();
try {
synchronized(holder) {
c = holder.lockCanvas();
if (c != null) {
c.drawColor(Color.GREEN);
}
}
} finally {
if (c != null) {
holder.unlockCanvas(c);
}
}
Log.i(TAG, "total time:" + (System.currentMillis() - start));
}
When I run my app, the output from LogCat is:
total time:30
total time:23
total time:6
total time:39
total time:17
Why does the lock/unlock of canvas takes too much time? Are there any better approach to my problem? (that is, to have a 60 fps app)
Thanks!
I had a similar FPS issue with low FPS on the Samsung Intercept. I was getting only 12FPS with nothing more than a blank screen. I found that the issue was related to Android doing auto-scaling on the canvas and draw functions.
In the
AndroidManifest.xmlif you add the line<supports-screens android:anyDensity=”true”>
then Android won’t auto-scale the canvas and draw functions. The default is “
false” which will enable auto-scaling.With
android:anyDensity="true"I was able to get 40+ FPS!The downside to all this is that you now have to do all of the math yourself to match the different screen sizes out there. But that should not be that hard.
Hope this helps anyone with similar problems. It took me 5+ hours to figure this little bugger out.