2012-08-04 71 views
1

我試過這樣的,這是我在網上找到發揮GIF圖片:Play.gif圖像,而無需使用的WebView

private class MYGIFView extends View { 

    Movie movie; 
    InputStream is = null; 
    long moviestart; 

    public MYGIFView(Context context) { 
     super(context); 

     // Provide your own gif animation file 

     is = context.getResources().openRawResource(R.drawable.mygif); 
     movie = Movie.decodeStream(is); 

    } 

    @Override 
    protected void onDraw(Canvas canvas) { 

     canvas.drawColor(Color.WHITE); 
     super.onDraw(canvas); 
     long now = android.os.SystemClock.uptimeMillis(); 
     System.out.println("now=" + now); 
     if (moviestart == 0) { // first time 
      moviestart = now; 

     } 
     System.out.println("\tmoviestart=" + moviestart); 
     int relTime = (int) ((now - moviestart) % movie.duration()); 
     System.out.println("time=" + relTime + "\treltime=" 
       + movie.duration()); 
     movie.setTime(relTime); 
     movie.draw(canvas, this.getWidth()/2 - 20, 
       this.getHeight()/2 - 40); 
     this.invalidate(); 
    } 

雖然很多人說,他們得到了他們想要的結果。

我已經把gif圖像放在drawable中。 我越來越movie.duration()爲空。

請建議我在哪裏錯了,或者如果有什麼方法。

回答

1

如果沒有WebView,Android無法播放GIF文件。您必須將其拆分爲框架並自行設置動畫。

我發現另一個StackOverflow後this bit of software from XoyoSoft,稱爲GifSplitter,可以將GIF分割成幀。然後你會想用AnimationDrawable來組合這些。

這將是這個樣子:

// Your files: 
res\drawable-xxxx\frame1.jpg 
res\drawable-xxxx\frame2.jpg 
res\drawable-xxxx\frame3.jpg 
// ... 
res\drawable-xxxx\frame99.jpg 

然後,有AnimationDrawable文檔上面將展示如何顯示這些圖像中的一個例子。

最後,您必須加載和播放動畫;這也在AnimationDrawable文檔中有詳細說明。

相關問題