在android中如何显示gif?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (15)

我有这个代码来显示与电影的GIF图像:

public class GIFView extends View{        
private Movie movie;  
private InputStream is;  
private long moviestart;  
public GIFView(Context context) {  
    super(context);
    is=getResources().openRawResource(R.drawable.anim_cerca);  
    movie=Movie.decodeStream(is);
}  

@Override  
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    long now=android.os.SystemClock.uptimeMillis();  

    if (moviestart == 0) 
        moviestart = now;  

    int relTime = (int)((now - moviestart) % movie.duration());
    movie.setTime(relTime);
    movie.draw(canvas,10,10);
    this.invalidate();
}                         

}
提问于
用户回答回答于

我用这种方式解决了:

public class GIFView extends View{        
    private Movie movie;    
    private long moviestart;  
    public GIFView(Context context) throws IOException {  
        super(context);
    movie=Movie.decodeStream(getResources().getAssets().open("anim_cerca.gif"));
    }
    public GIFView(Context context, AttributeSet attrs) throws IOException{
        super(context, attrs);
    movie=Movie.decodeStream(getResources().getAssets().open("anim_cerca.gif"));
    }
    public GIFView(Context context, AttributeSet attrs, int defStyle) throws IOException {
        super(context, attrs, defStyle);
        movie=Movie.decodeStream(getResources().getAssets().open("anim_cerca.gif"));
    }
@Override  
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    long now=android.os.SystemClock.uptimeMillis();
    Paint p = new Paint();
    p.setAntiAlias(true);
    if (moviestart == 0) 
            moviestart = now;
            int relTime;
            relTime = (int)((now - moviestart) % movie.duration());
            movie.setTime(relTime);
            movie.draw(canvas,0,0);
            this.invalidate();
       }                         
}    

并在布局中,我把这个自定义视图以这种方式:

<spazio.digitale.com.GIFView
        android:layout_marginLeft="30dp" android:layout_gravity="center"
        android:layout_width="wrap_content" android:layout_height="220dp"
        android:id="@+id/GIFSingle">
</spazio.digitale.com.GIFView>
用户回答回答于

在添加到视图以及资源或资源后加载不同的gif会使它更加有用。另外,对于具有硬件加速功能的设备,我获得了空白视图,因此我为此GIF视图关闭了它。

另外,一定要将resif / drawable-xhdpi目录中的动画GIF:

public class GIFView extends View{

    Movie movie;
    long moviestart;

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

    public void loadGIFResource(Context context, int id)
    {
        //turn off hardware acceleration
        this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        InputStream is=context.getResources().openRawResource(id);
        movie = Movie.decodeStream(is);
    }

    public void loadGIFAsset(Context context, String filename)
    {
        InputStream is;
        try {
            is = context.getResources().getAssets().open(filename);
            movie = Movie.decodeStream(is);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (movie == null) {
            return;
        }

        long now=android.os.SystemClock.uptimeMillis();

        if (moviestart == 0) moviestart = now;

        int relTime;
        relTime = (int)((now - moviestart) % movie.duration());
        movie.setTime(relTime);
        movie.draw(canvas,10,10);
        this.invalidate();
    }
}

用法:

imageView.loadGIFResource(this, R.drawable.quickguide_1);

和:

<com.eyeverify.GIFView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp"
        android:layout_marginTop="110dp"
        android:src="@drawable/quickguide_1"/>

扫码关注云+社区