2013-08-19 178 views
1

如何獲取正在播放的gif動畫的持續時間?我想要有它的持續時間,我正在使用電影類和動畫是一個單一的循環。但是因爲電影類,它一次又一次地播放。我能知道如何獲得單個循環的持續時間?獲取gif動畫的持續時間

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


     } 
    }} 
+0

duration()rerurn是什麼? – pskink

+0

我猜不出什麼,我怎樣才能打印持續時間的值? – Meghs

+0

使用Log.d()和adb logcat – pskink

回答

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()給你一個循環的持續時間。