1

I am creating a live wallpaper app. I want that, in this app after some certain interval a movie object will draw its frame on surfaceHolder canvas.

My code is

public void startTimer() {
  timer = new Timer();
  initializeTimerTask();

}

private void initializeTimerTask() {
  timerTask = new TimerTask() {@
    Override
    public void run() {
      Canvas canvas = holder.lockCanvas();
      canvas.save();
      // Adjust size and position so that
      // the image looks good on your screen
      canvas.scale(1f, 1f);
      movie.draw(canvas, -500, 0);
      canvas.restore();
      holder.unlockCanvasAndPost(canvas);
      movie.setTime((int)(System.currentTimeMillis() % movie.duration()));

    }
  };
  timer.schedule(timerTask, 33);
}

But when I run this app only one frame is shown on the surface and it is not changing over time.

What is the error in my code? How can I see that the surface is changing over time?

torox
  • 460
  • 1
  • 3
  • 11
Bangla
  • 11
  • 2

1 Answers1

0

Your timer task is only scheduled once you need to reschedule it inside of your task again

private void initializeTimerTask() {
  timerTask = new TimerTask() {@
    Override
    public void run() {
      Canvas canvas = holder.lockCanvas();
      canvas.save();
      // Adjust size and position so that
      // the image looks good on your screen
      canvas.scale(1f, 1f);
      movie.draw(canvas, -500, 0);
      canvas.restore();
      holder.unlockCanvasAndPost(canvas);
      movie.setTime((int)(System.currentTimeMillis() % movie.duration()));
      timer.schedule(timerTask, 33);    
    }
  };
  timer.schedule(timerTask, 33);
}

and you need to make your timer as final in order to work

Westranger
  • 1,308
  • 19
  • 29