Settle FPS in the drawing of image and thus acheive smooth motion on the app screen
<p>I am working on making a ball move along a certain path. The desired speed only comes for a few milliseconds and then the thread seems to be slowing down. May I know why is this happening.</p>

<p>I am attaching my view and the thread class.</p>

<p>-> ScreenView.java</p>

<pre><code>package com.pkg.SurfacePrac;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class ScreenView extends SurfaceView{

    private Bitmap bmp;
    private SurfaceHolder holder;
    private ScreenThread gameLoopThread;

    private int x = 0; 
    private int xSpeed = 1;


    public ScreenView(Context context) {
        super(context);
        gameLoopThread = new ScreenThread(this);
        holder = getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {

            public void surfaceChanged(SurfaceHolder holder, int format, int width,
                    int height) {


            }

            public void surfaceCreated(SurfaceHolder holder) {

                gameLoopThread.setRunning(true);
                gameLoopThread.start();

            }

            public void surfaceDestroyed(SurfaceHolder holder) {
                // TODO Auto-generated method stub

            }

        });

        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.blueball);
    }


    @Override
    protected void onDraw(Canvas canvas) {

        if (x == getWidth() - bmp.getWidth()) {
            xSpeed = -1;
     }

        if (x == 0) {
            xSpeed = 1;
     }

     x = x + xSpeed;
     canvas.drawColor(Color.BLACK);
     canvas.drawBitmap(bmp, x , 10, null);
    }

}
</code></pre>

<p>-> ScreenThread.java</p>

<pre><code>package com.pkg.SurfacePrac;

import android.graphics.Canvas;

public class ScreenThread extends Thread {

    static final long FPS = 50;
    private ScreenView view;
    private boolean running = false;   

    public ScreenThread(ScreenView view) {
          this.view = view;
          }

    public void setRunning(boolean run) {
          running = run;
    }


    public void run() {
          long ticksPS = 1000 / FPS;
          long startTime;
          long sleepTime;

          while (running) {
                 Canvas c = null;
                 startTime = System.currentTimeMillis();

                 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 &gt; 0)
                              sleep(sleepTime);                        
                        else
                             sleep(10);

                 } 
                 catch (Exception e) {}
}}}
</code></pre>
