在机器人中显示gif

Jac*_*rky 17 android gif movie

我有这个代码用电影显示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();
}                         

}
Run Code Online (Sandbox Code Playgroud)

当gif加载时,我的问题就出现了,它画得非常糟糕,只显示了第一帧,而另一帧则被打扰了.我能做什么?

编辑:问题是模拟器!它不会显示GIF,但设备它没关系!:)

Jac*_*rky 10

我这样解决了:

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();
       }                         
}    
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

  • 不知怎的,这对我不起作用.它给出了错误/ libc(20288):致命信号11(SIGSEGV)在0x00000000(代码= 1),线程20288(.track.driv)任何建议? (4认同)

Joe*_*ply 10

好的开始.在添加到视图以及资源或资源之后,必须使它更有用于加载不同的GIF.此外,对于具有硬件加速功能的设备,我获得了空白视图,因此我将其关闭以用于此GIFView.

另外,请务必将动画GIF放在res/drawable-xhdpi目录中(如果使用这种方式,则为资产)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

imageView.loadGIFResource(this, R.drawable.quickguide_1);
Run Code Online (Sandbox Code Playgroud)

<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"/>
Run Code Online (Sandbox Code Playgroud)