在android中显示gif [英] Showing gif in android

查看:27
本文介绍了在android中显示gif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码来显示带有电影的 gif 图像.

i have this code to show gif image with Movie.

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

}

我的问题是在加载 gif 时出现的,它绘制得很糟糕,只显示第一帧,而其他帧都被干扰了.我能做什么?

My problem borns when gif is loaded, it draw very bad, only the first frame is shown and the other are like disturbed. What can i do?

问题是模拟器!它不显示 GIF,但在设备上它没问题!:)

THE PROBLEM IS EMULATOR! IT DOESN'T SHOW GIF, BUT ON DEVICE IT'S OK! :)

推荐答案

我是这样解决的:

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

在布局中,我以这种方式放置了这个自定义视图:

and in layout i put this custom view in this way:

<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>

这篇关于在android中显示gif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆