用于android的gif循环计数和最后一个图像

时间:2014-04-10 05:26:58

标签: android canvas gif

我想播放一个gif文件。我能够使用电影和绘制画布显示gif(下面的代码)。但通常gifs文件附带一个属性,告诉你所需的循环次数。如何提取该值并显示电影的最后一帧?下面是我用来玩gif的代码:

public TutorialGifView(Context context , InputStream is   , int width , int height ) {
        super(context);
        mContext = context;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        ScreenWidth = width;
        ScreenHeight = height ;
        mMovie = Movie.decodeStream(is);


        if(ScreenWidth > ScreenHeight)
        {
            scaleh = (float)height/(float)(mMovie.height()+1);
            scalew =  ((float)(4*height))/((float)(mMovie.width()*3));

        }
        else
        {
            scalew = ((float)width/((float)mMovie.width()+1));
            scaleh =  ((float)(4*mMovie.width())/(float)(3*mMovie.width()));
        }

    }

protected void onDraw(Canvas canvas)
{ 

        if (mMovie == null) {
            return;
        }

        canvas.scale(scalew, scaleh);
        canvas.drawColor(Color.TRANSPARENT);

        super.onDraw(canvas); 
        final long now = SystemClock.uptimeMillis();

        if (mMoviestart == 0) { 
            mMoviestart = now;
        }

        final int relTime = (int)((now - mMoviestart) % mMovie.duration());
        mMovie.setTime(relTime);
        mMovie.draw(canvas, padLeft/scalew , padTop/scaleh);//padLeft/scaleh);
        // here i shoud get the loop count 
        if(LoopCount > 0 )
        {
            if((now- mMoviestart) > mMovie.duration()*(LoopCount) )
            {

                setWillNotDraw(true);
                return ;
            }


        }
        this.invalidate();


}

1 个答案:

答案 0 :(得分:1)

    ...
    final int relTime = (int)((now - mMoviestart) % mMovie.duration());
    if (relTime >= mMovie.duration()) {
    relTime = 0;
    }
    mMovie.setTime(relTime);

这将从头开始启动GIF!