获取gif动画的持续时间

时间:2013-08-19 10:25:47

标签: android gif

如何获取正在播放的gif动画的持续时间?我希望有它的持续时间,我正在使用Movie类,动画是一个循环。但由于电影课,它一次又一次地播放。我可以知道如何获得单个循环的持续时间吗?

public class GIFView extends View {

    private Movie mMovie;
    private long movieStart;

    public GIFView(Context context) {
        super(context);
        initializeView();
    }

    public GIFView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initializeView();
    }

    public GIFView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initializeView();
    }

    private void initializeView() {
        InputStream is = getContext().getResources().openRawResource(
                R.drawable.imageedit_ball);
        mMovie = Movie.decodeStream(is);
    }

    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.TRANSPARENT);
        super.onDraw(canvas);
        long now = android.os.SystemClock.uptimeMillis();

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


        }
    }}

1 个答案:

答案 0 :(得分:0)

如果您只想播放动画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();
  }
}

mMovie.duration()为您提供一个循环的持续时间。