Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing gif in android

Tags:

android

gif

movie

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

}

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?

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

like image 841
Jayyrus Avatar asked Oct 15 '11 10:10

Jayyrus


People also ask

Why are GIFs not working on Android?

Android devices have not had built-in animated GIF support, which causes GIFs to load slower on some Android phones than on other OS.

How do I view GIFs in my gallery?

Tap on the GIF image that you like and preview it. Press and hold on the image and then select Download image from the pop-up window. Once downloaded, you will be to view the GIF image in the Gallery app.


1 Answers

Good start. Gotta make it more useful for loading different gifs after being added to the view and for either assets or resources. Also, for devices with hardware acceleration I was getting blank views, so I turned it off for this GIFView.

Also, be sure to put animated gifs in the res/drawable-xhdpi directory (or assets if using that way)

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

Usage:

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

and

<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"/>
like image 181
Joel Teply Avatar answered Sep 23 '22 22:09

Joel Teply