title: Animations in Android
body: <p>I'm new in Android and I want to do some animations. I'm trying to make my sprite sheet move automatically. But there is a problem with screen rendering. It leaves a trail while it is moving.<a href="http://s204.photobucket.com/albums/bb108/agentx44_naruto24/?action=view&amp;current=error.png" rel="nofollow">Click here to see the screen shot</a></p>
  
  <p>This is my code:</p>
  
  <pre><code>public class SampleAnimationActivity extends Activity {
      /** Called when the activity is first created. */
  
      Screen screen;
      MapAnimation mapAnimation;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          screen = new Screen(this);
          setContentView(screen);
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
      }
  
  
      @Override
      protected void onPause() {
          super.onPause();
      }
      @Override
      protected void onResume() {
          super.onResume();
      }
  
      public class Screen extends SurfaceView implements Callback{
  
          private SurfaceHolder holder;
          private MySurfaceViewThread mySurfaceViewThread;
          private boolean isSurfaceCreated;
          private Bitmap character, tiles;
  
          public Screen(Context context) {
              super(context);
              initialize();
          }
  
          public void initialize(){
              //Create a new SurfaceHolder and assign this class as its callback...
              holder = getHolder();
              holder.addCallback(this);
              isSurfaceCreated = false;
              character = BitmapFactory.decodeResource(getResources(),R.drawable.penguin_sprite);
              tiles = BitmapFactory.decodeResource(getResources(), R.drawable.tile_sprites);
              resume();
          }
  
          public void resume(){
              //Create and start the graphics update thread.
              if(mySurfaceViewThread == null){
                  mySurfaceViewThread = new MySurfaceViewThread();
                  if(isSurfaceCreated == true){
                      mySurfaceViewThread.start();
                  }
              }
          }
  
          public void pause(){
              //Kill the graphics update thread
              if(mySurfaceViewThread != null){
                  mySurfaceViewThread.pause();
                  mySurfaceViewThread = null;
              }
          }
  
          @Override
          public void surfaceChanged(SurfaceHolder holder, int format, int width,
                  int height) {
          }
  
          @Override
          public void surfaceCreated(SurfaceHolder holder) {
              isSurfaceCreated = true;
              if(mySurfaceViewThread != null){
                  mySurfaceViewThread.start();
              }
          }
  
          @Override
          public void surfaceDestroyed(SurfaceHolder holder) {
              isSurfaceCreated = false;
              pause();
          }
  
          public class MySurfaceViewThread extends Thread{
  
              private boolean isPaused;
              private boolean characterLoaded, characterDrawn;
              private SurfaceHolder surfaceHolder;
  
              public MySurfaceViewThread(){
                  super();
                  isPaused = false;
                  characterLoaded = false;
                  surfaceHolder = holder;
                  characterDrawn = false;
              }
  
              public void run(){
                  //Repeat the drawing loop until the thread is stopped
                  while(!isPaused){
                      if(!surfaceHolder.getSurface().isValid()){
                          continue;
                      }
                      if(characterLoaded == false){
                          mapAnimation = new MapAnimation(screen, character);
                          characterLoaded = true;
                      }
                      Canvas canvas = surfaceHolder.lockCanvas();
                      mapAnimation.onDraw(canvas);
                      surfaceHolder.unlockCanvasAndPost(canvas);
                  }
              }
  
              public void pause(){
              }
              public void onDraw(){
              }
          }
      }   
  }
  
  
  public class MapAnimation {
  
      private Screen screen;
      private Bitmap character;
      private int width, height, xPosition, yPosition, xSpeed, ySpeed;
  
      public MapAnimation(Screen screen, Bitmap character) {
          this.screen = screen;
          this.character = character;
          this.width = character.getWidth();
          this.height = character.getHeight();
          xPosition = 0;
          yPosition = 0;
          xSpeed = 5;
          ySpeed = 5;
      }
  
      public void updateCharacter(){
          if(xPosition &gt; screen.getWidth() - width - xSpeed){
              xSpeed = 0;
              ySpeed = 5;
          }
          if(yPosition &gt; screen.getHeight() - height - ySpeed){
              xSpeed = -5;
              ySpeed = 0;
          }
          if(xPosition + xSpeed &lt; 0){
              xPosition=0;
              xSpeed = 0;
              ySpeed = -5;
          }
          if(yPosition+ySpeed &lt; 0){
              yPosition = 0;
              xSpeed = 5;
              ySpeed = 0;
          }
          xPosition += xSpeed;
          yPosition += ySpeed;
      }
  
      public void onDraw(Canvas canvas){
          updateCharacter();
          Rect src = new Rect(0, 0,135,225);
          Rect dst = new Rect(xPosition, yPosition, xPosition+width, yPosition+height);
          canvas.drawBitmap(character, src, dst, null);
      }
  }
  </code></pre>
  
  <p>Your help will be deeply appreciated :)</p>