如何阻止gif在android中循环

时间:2014-01-19 17:27:55

标签: android android-layout animated-gif movie

我在画布上玩了一个gif。我的gif不断循环。有谁知道如何从循环中发现它?这是我的代码:

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();
    }

我的猜测是,如果我玩setTime,我可以让它运行良好。但我找不到setTime的任何文档?它到底做了什么?

其他试用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();
        }
    }

其他试用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();
        }
    }

我的gif也是由4帧组成。

1 个答案:

答案 0 :(得分:4)

如果您只想播放动画gif一次,请在通过持续时间后停止致电invalidate()

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();
  }
}