20

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 .

Garg
  • 2,731
  • 2
  • 36
  • 47
neeraj
  • 740
  • 1
  • 6
  • 21
  • I was looking for question an [play GIF inside android application](http://stackoverflow.com/questions/40468033/how-to-set-imageview-to-show-different-position-in-png-android-sprite-alike-an/40481803#40481803) – Roy Doron Nov 08 '16 at 08:06

3 Answers3

10

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" />
Vipul Purohit
  • 9,807
  • 6
  • 53
  • 76
Om Narain Shukla
  • 361
  • 1
  • 4
  • 12
  • 4
    Cannot pass a gif resource it shows me this: "Expected resource of type. raw...". I use Android API16 with Android Studio – Ispas Claudiu Mar 04 '16 at 09:31
2

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

Tobliug
  • 2,992
  • 30
  • 28
0

You can achieve this with a WebView. This is what I use:

A custom LinearLayout that anchors a WebView Class

import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Spinner extends LinearLayout{

    public Spinner(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        View.inflate(context, R.layout.spiner, this);
        WebView web = (WebView) findViewById(R.id.web);
        web.loadUrl("file:///android_asset/booting.gif");
    }
    public void setText(String text){
        TextView t = (TextView) findViewById(R.id.txtloading);
        t.setText(text);
    }
    public void refresh(){
        WebView web = (WebView) findViewById(R.id.web);
        web.loadUrl("file:///android_asset/booting.gif");       
    }
}

The R.layout.Spinner XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center|center_vertical"
    android:padding="10dp">
    <WebView android:id="@+id/web" 
        android:layout_width="24dp"
        android:layout_height="12dp"
        />

    <TextView
        android:id="@+id/txtloading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Loading..."
        />

</LinearLayout>

You can use as

Spinner spinner = new Spinner(this);
myContainer.addView(spinner);

You can call refresh on spinner to ensure image playing:

spinner.refresh();

Hope that helps.

Oluwatumbi
  • 1,321
  • 1
  • 12
  • 19