I have some difficulties with my frame rate using SurfaceView. I am doing typical material that I found in some textbooks (they all said the same thing), but I can’t achieve a decent frame rate on my Galaxy S-samsung S (old, i9000).
Here's the code i for the thread loop. FPS is initialized at level 30.
@Override public void run() { long ticksPS = 1000/FPS; long startTime; long sleepTime; //fps checker long contms=0; long lasttimecheck = System.currentTimeMillis(); int fps=0; while (running) { long time = System.currentTimeMillis(); if(contms>1000) { Log.v("FPS",String.valueOf(fps)); contms=time-lasttimecheck; fps=1; } else { fps++; contms+=time-lasttimecheck; } lasttimecheck = time; Canvas c = null; startTime =time; try { c = view.getHolder().lockCanvas(); synchronized (view.getHolder()) { view.onDraw(c); } } finally { if (c != null) { view.getHolder().unlockCanvasAndPost(c); } } sleepTime = ticksPS-(System.currentTimeMillis() - startTime); try { if (sleepTime > 10) sleep(sleepTime); else { Log.w("LOWFPS",String.valueOf(contms)); sleep(10); } } catch (Exception e) {} } }
In surfaceView, I initialize the holder with holder.setFormat (PixelFormat.RGBA_8888); but I don’t know if I need to do something with bitmaps to avoid processor uselessness (I save bitmaps in local variables and then draw them). The game is simple, it should work much faster.
The frame rate is quite random, sometimes it works with smoothing, sometimes it does not work, but always under 30FPS.
Any ideas ???
EDIT WITH LONELINE
@Override protected void onDraw(Canvas canvas) { canvas.drawBitmap(bg, 0, 0, null); //1 stars.draw(canvas,dx,dy); //2 if(playing.on()) reactors.draw_reaccio(canvas,dx,dy); //3 gotes.draw(canvas,dx,dy); //4 reactors.draw(canvas,dx,dy); //5 sg.draw(canvas); //6 sr.draw(canvas); //7 eraser.draw(canvas); //8 playing.draw(canvas); //9 opcions.draw(canvas); //10 }
1) Drawing a background (480x800) 2) it is a class that contains a list of basic getHeight () objects (stars) with its coordinates (x, y) and an identifier with an associated image (about 9 images of different stars) 3) it draws n * 2 a circle, one with a bay, and the other with a stroke on a related object (about 20 or so) 4) He draws the main object of the game, small drops with animation. There are 9 different kinds of drops, and each of them has 5 related animation images (should I place 5 images in 1, maybe?) 5) the same as drops, but without animation From 6 to 10) it doesn’t matter, he just draws an image
I assume that the slowness is due to: (2) due to the number of stars (4) associated with the animation, the witch changes every 2-3 frames to another image and maybe this is too much for memory, I think I should combine all the images in just 1.
Frame rate around 20-22 FPS with S. Galaxy S i9000