Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

play downloaded Gif image in android

Tags:

I'm downloading GIF image in my app from server and then i'm showing it with ImageView but it was not animated . is there any other way to play downloaded animated GIF image . Thanks in advance .

like image 506
neeraj Avatar asked Feb 29 '12 06:02

neeraj


People also ask

How can I play GIFs on my Android?

Android smartphones have a built-in image viewing application called Gallery. When you connect your Android smartphone to your computer and transfer a GIF file to your phone's storage area, you can open the Gallery appliation and view that GIF file.

How do I play a downloaded GIF?

GIFs are also easy to open through web-based browsers, including Chrome, Firefox, and Internet Explorer. In the case of Internet Explorer, simply click on the File menu and then Open. Select Browse followed by All Files. Click on the GIF file name and then Open.

Why do GIFs not play when I click on them?

If your GIF file is not playing or looping, it might be because the file is too large. If it's more than 1080 pixels high or 1920 pixels wide, you'll need to reduce the size. There are a number of free, online tools of varying sophistication that you can use.

Why are my 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.


2 Answers

I am using the below custom View instead of Image View.

public class SampleView extends View {

    private Movie mMovie;
    private long mMovieStart;

    public SampleView(Context context) {
        super(context);
        setFocusable(true);

        java.io.InputStream is;
        is = context.getResources().openRawResource(R.drawable.girl_dances);
        mMovie = Movie.decodeStream(is); 
    }

    public SampleView(Context context, AttributeSet attrSet) {
        super(context, attrSet);
        setFocusable(true);

        java.io.InputStream is;
        is = context.getResources().openRawResource(R.drawable.girl_dances);
        mMovie = Movie.decodeStream(is);
    }

    public SampleView(Context context, AttributeSet attrSet, int defStyle) {
        super(context, attrSet, defStyle);
        setFocusable(true);

        java.io.InputStream is;
        is = context.getResources().openRawResource(R.drawable.girl_dances);
        mMovie = Movie.decodeStream(is);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(0x00000000);

        Paint p = new Paint();
        p.setAntiAlias(true);

        long now = android.os.SystemClock.uptimeMillis();
        if (mMovieStart == 0) { // first time
            mMovieStart = now;
        }
        if (mMovie != null) {
            int dur = mMovie.duration();
            if (dur == 0) {
                dur = 1000;
            }
            int relTime = (int) ((now - mMovieStart) % dur);
            mMovie.setTime(relTime);
            mMovie.draw(canvas, getWidth() / 2 - mMovie.width() / 2,
                    getHeight() / 2 - mMovie.height() / 2);
            invalidate();
        }
    }
    }

XML Layout :

<com.test.sample.SampleView
android:id="@+id/gif_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
like image 132
Om Narain Shukla Avatar answered Oct 13 '22 05:10

Om Narain Shukla


After some research, it seems that the best (or easiest) solution is to use a WebView :

ex :

put myAnimatedGif.gif file into the assets folder.

create an xml web View:

<WebView
    android:id="@+id/myWebView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

then load the gif file into the web view :

    WebView view = (WebView) findViewById(R.id.myWebView);
    view.loadUrl("file:///android_asset/myAnimatedGif.gif");

Enjoy !

An other solution is to use this kind of library: https://github.com/koral--/android-gif-drawable

like image 37
Tobliug Avatar answered Oct 13 '22 05:10

Tobliug