title: Drawing to canvas is too slow , How do i achieve constant 30 fps or more ? (Android)
body: <p>I'm trying to create a 2d game for Android and for the moment I'm struggling with achieving a constant fps of about 30 fps (i think 30 is enough for a mobile game). I am using SurfaceView and a pretty straight forward game loop:</p>
  
  <pre><code> while (isOk==true)
      {
          if (!holder.getSurface().isValid()) continue;
  
          c = holder.lockCanvas();
  
          update();
          draw(c);
          holder.unlockCanvasAndPost(c);
  
          t2= System.currentTimeMillis();
          all++;
          if ( mspf /* miliseconds per frame, and it's set to 1000/30 */ -(t2-t1)  &gt; 0 )
          {
              try {
                  t.sleep(mspf-(t2-t1));
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
          else
                  Log.w("Super",String.valueOf(mspf-(t2-t1))+ " o " + String.valueOf(++total)+ "out of " +String.valueOf(all));
  
          t1=Math.max( t1+mspf, t2);
      }
  </code></pre>
  
  <p>This is the game loop . As you may have noticed , I use that Log.w to see in how many frames i have some remaining time to Thread.sleep . The results are not that great . I have about 10-25% of the frames that exceede 1000/30 ms to run:</p>
  
  <blockquote>
    <p>09-07 00:00:39.069: W/Super(28328): -1 o 4395out of 16353<br>
    09-07 00:00:39.109: W/Super(28328): -1 o 4396out of 16354<br>
    09-07 00:00:39.209: W/Super(28328):  0 o 4397out of 16357<br>
    09-07 00:00:40.269: W/Super(28328): -4 o 4398out of 16389<br>
    09-07 00:00:40.299: W/Super(28328): -3 o 4399out of 16390<br>
    09-07 00:00:40.339: W/Super(28328): -4 o 4400out of 16391<br>
    09-07 00:00:40.379: W/Super(28328): -4 o 4401out of 16392<br>
    09-07 00:00:40.409: W/Super(28328): -5 o 4402out of 16393<br>
    09-07 00:00:40.459: W/Super(28328): -8 o 4403out of 16394<br>
    09-07 00:00:40.499: W/Super(28328): -7 o 4404out of 16395<br>
    09-07 00:00:40.529: W/Super(28328): -3 o 4405out of 16396<br>
    09-07 00:00:40.569: W/Super(28328): -5 o 4406out of 16397<br>
    09-07 00:00:40.609: W/Super(28328): -5 o 4407out of 16398<br>
    09-07 00:00:40.639: W/Super(28328): -3 o 4408out of 16399<br>
    09-07 00:00:40.679: W/Super(28328): -3 o 4409out of 16400<br>
    09-07 00:00:40.719: W/Super(28328): -4 o 4410out of 16401  </p>
  </blockquote>
  
  <p>In draw , i don't have so many bitmaps to draw. ( about 15 bitmaps, including a shader , a transparency mask for some effect  and a scaling ) . All the bitmaps are loaded in the create method from resources (only PNG file). </p>
  
  <pre><code>public void draw(Canvas c)
  {
  
  
      c.drawBitmap(background,0,0,null);
      c.drawBitmap(m_image.get(m_cn),400 , 100, test);
  
  
      c.drawBitmap(start_menu,10 ,10 , test);
      c.drawBitmap(inventory_menu, 200 , 10, test);
      c.drawBitmap(shop_menu,400  , 10, test);
      c.drawBitmap(options_menu, 10 , 300, test);
      c.drawBitmap(facebook_menu,200 , 300, test);
  
  
  
  
  
      light.drawColor(Color.TRANSPARENT, Mode.CLEAR);
  
  
      light_matrix.setScale(scale_factor, scale_factor);
      light_matrix.postTranslate(light_x-mask_aux.getWidth()/2*scale_factor, light_y-mask_aux.getHeight()/2*scale_factor);
      light.drawBitmap(mask_aux, light_matrix, test);
  
  
      copy_the_screen.drawBitmap(buffer.get(b_activ),0,0,test);
      sh =  new BitmapShader(screen_copy,Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
      pnt.setShader(sh);
  
      c.drawARGB(200, 0, 0,0);
      c.drawBitmap(mask,0,0 ,pnt);
  
  
  }
  </code></pre>
  
  <p>The update method means nothing to the complexity , so there has be something to be done in these 2 functions  in order to achieve a better fps . Help me , pls and excuse me for my bad english. </p>
  
  <p>Ok . The update function is this :</p>
  
  <pre><code>     public void update()
  {
      m_time++;
      if (m_time==2)
      {
          m_time=0;
          m_cn++;
          if (m_cn==4)m_cn=0; 
      }
  
      if (touched==true)
      {
          light_x=t_x;
          light_y=t_y;
          scale_factor=t_y/150;
      }
  }
  </code></pre>
  
  <p>It only modifies the bitmap i choose from a sprite . I don't have a big bitmap with all the position of a character , but 4 different bitmaps. The second "if" refers to a mask image that creates a ball of light exactly where i touch . I use that light_matrix to scale that light-ball proportional to the Y - coordinate ( of the click event) . That is why I use the shader , to create the light effect . Here, take a look on the OnTouchEvent :</p>
  
  <pre><code>   public boolean onTouchEvent(MotionEvent event) {
      // TODO Auto-generated method stub
  
      //I put this sleep method here because I noticed it was the lagging the game if i let it run 
      // without a sleep.
      try {
          Thread.sleep(100);
      } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
       // after sleep , i only check for events.
  
   }
  </code></pre>