2011-10-23 27 views
3

在我的android應用程序中,我使用Movie來顯示gif,但是我面臨的問題是如果我使用擴展名.gif接收圖像時只顯示一幀圖像,它顯示的是空白屏幕。
請讓我知道在android中顯示gif的更好方法。
我使用的代碼是在android中處理gif圖像

public MYGIFView(Context context, InputStream is, int i, int j,int screenWidth,int screenHeight) { 
    super(context); 

    //is=context.getResources().openRawResource(red); 
    movie=Movie.decodeStream(is); 
    xcoordinate = i; 
    ycoordinate = j; 
    viewWidth = screenWidth; 
    viewHeight = screenHeight; 


    setLayoutParams(new LayoutParams(viewWidth,viewHeight)); 

} 

@Override 
protected void onDraw(Canvas canvas) { 
    if(movie != null) { 
     long now = android.os.SystemClock.uptimeMillis(); 
     int dur = Math.max(movie.duration(), 1); // is it really animated? 
     int pos = (int)(now % dur); 
     movie.setTime(pos); 
     movie.draw(canvas,xcoordinate, ycoordinate); 
    } 

回答

0

API demos itself provides a solution了這一點。

我想你錯過的是這部電影的觸發。

這樣做:

Handler handler = new Handler(); 
new Thread() { 
    @Override public void run() { 
     // ... setup the movie (using the code from above) 
     // ... create and display the custom view, passing the movie 

     while(!Thread.currentThread().isInterrupted()) { 
      handler.post(new Runnable() { 
       public void run(){ 
        view.invalidate(); 
       } 
      }); 
      try { 
       Thread.sleep(50); // yields 20 fps 
      } catch (InterruptedException e) { 
       Thread.currentThread().interrupt(); 
      } 
     } 
    } 
}.start(); 

或作爲API的演示顯示,在無效的onDraw()事件本身的看法,引起重繪爲止,影片的時間已經結束了。

+0

Thread.sleep(50); //產量20 fps 我們需要爲你製造一個新的地獄 –

0

我已經嘗試過這樣的工作gif文件在android中運行。

MainActivity.java

package com.example.gifview; 
import android.os.Bundle; 
import android.app.Activity; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     GifWebView view = new GifWebView(this, "file:///android_asset/earth.gif"); 
     setContentView(view); 
    } 



} 

GifWebView.java

package com.example.gifview; 

import android.content.Context; 
import android.webkit.WebView; 

public class GifWebView extends WebView { 

    public GifWebView(Context context, String path) { 
     // TODO Auto-generated constructor stub 
     super(context); 
     loadUrl(path); 
    } 

} 

注:activity_main.xml中