Question

I have a gif playing on a canvas. My gif keeps looping. Does anyone know how to spot it from looping? here is my code:

if (movieStart == 0) {
        movieStart = (int) now;
    }
    if (mMovie != null) {
        int relTime = (int) ((now - movieStart) % mMovie.duration());
        mMovie.setTime(relTime);
        mMovie.draw(canvas, getWidth() / 2 - mMovie.width(), getHeight() / 2
            - mMovie.height());
    this.invalidate();
    }

My guess is if I play around with setTime I could get it to run well. But I can't find any docs for setTime? What does it do exactly?

ADDITIONAL TRIAL 1

if (movieStart == 0) {
        movieStart = (int) now;
    }
    while (mMovieLoop-- > 0) {
        if (mMovie != null) {
            int relTime = (int) ((now - movieStart) % mMovie.duration());
            mMovie.setTime(relTime);
            mMovie.draw(canvas, getWidth() / 2 - mMovie.width(), getHeight() / 2
                - mMovie.height());

            this.invalidate();
        }
    }

ADDITIONAL TRIAL 2

if (movieStart == 0) {
        movieStart = (int) now;
    }
    if (mMovie != null) {
        int relTime = (int) ((now - movieStart) % mMovie.duration());
        mMovie.setTime(relTime);
        mMovie.draw(canvas, getWidth() / 2 - mMovie.width(), getHeight() / 2
            - mMovie.height());
        while (mMovieLoop-- > 0) {
            this.invalidate();
        }
    }

Also my gif is made of 4 frames.

Was it helpful?

Solution

If you only want to play the animated gif once, stop calling invalidate() once you pass the duration.

if (movieStart == 0) {
  movieStart = (int) now;
}

if (mMovie != null) {
  int relTime = (int) (now - moviestart);

  if (relTime > mMovie.duration()) {
    relTime = mMovie.duration();
  }

  mMovie.setTime(relTime);
  mMovie.draw(canvas, 
      getWidth() / 2 - mMovie.width() / 2, 
      getHeight() / 2 - mMovie.height() / 2);

  if (relTime < mMovie.duration()) {
    invalidate();
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top