How to put an animated GIF in an APP on Android?

Asked

Viewed 7,988 times

4

Well, I have been with this problem for some time, I tried to solve using answers from Stackoverflow gringo but I was not successful.

I need to put an animated GIF image in my Activity but Imageview does not play an image. gif, it just shows it as an animation-free image.

Someone can help me?

Remembering that I’m programming for Android.

  • The idea of android has never been to support gif, instead try using Animation and SVG

3 answers

6


You can use the Glide lib for this

follow the example:

in Gradle add

dependencies {
compile 'com.github.bumptech.glide:glide:3.5.2'

}

example in your Activity

Glide.with(context)
.load("https://inthecheesefactory.com/uploads/source/glidepicasso/gifanimation2.gif")
.into(idDoTeuImageView);

If it is the drawable folder

Glide.with(this)
            .load(R.drawable.gifanimation2) // aqui é teu gif
            .asGif()
            .into(gif);

First context of your Class, according to the url of Gif or drawable and third your Imageview which in this case is its id.

Link from the lib https://github.com/bumptech/glide

  • I did what I said but the image view just doesn’t appear anything, not even an image without animation, it is empty.

  • In the case as you did? took from the drawable folder or from a url? remembering that the id of your Image view you have

  • I tested by taking the drawable folder and then with a URL. Yes I did the casting. In Activity it looks like this: Imageview gif = (Imageview)findViewById(R.id.imageView); Glide.with(this). load(R.drawable.animacao). into (gif);

  • I edited my answer please see. I just tested it and it worked perfectly.

  • Glide.with(this) . load(R.drawable.gifanimation2) . asGif() . into(gif);

  • It worked!! But the strange thing is that the image seems to have lost some quality, it tbm moves more slowly than the original gif and tbm leaves a trail where it moves. You know how to fix it or you know why?

  • Good, this link: http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en. has explained about this and how to improve the quality this lib is very good, it has a parameter to leave and load the cache. diskCacheStrategy(Diskcachestrategy.ALL) it is good because your app will no longer need to go in the drawable folder to fetch the image/gif but the cache is faster. Also take a look at the wiki on the lib link I left in the reply.

  • I’ll see what I can do :)

Show 3 more comments

6

The android natively has no resource for .gif lively. For you to do this effect we will have to have all the animation images available to be added in one xml.

Then create an xml called animation.xml and add this content to it:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
id="animacao" android:oneshot="false" >
<item android:drawable="@drawable/android1" android:duration="150" />
<item android:drawable="@drawable/android2" android:duration="150" />
<item android:drawable="@drawable/android3" android:duration="150" />
<item android:drawable="@drawable/android4" android:duration="150" />
<item android:drawable="@drawable/android5" android:duration="150" />
<item android:drawable="@drawable/android6" android:duration="150" />
<item android:drawable="@drawable/android7" android:duration="150" />
</animation-list>

And in the xml of your Activity create a ImageView

<ImageView android:layout_height="wrap_content"
    android:layout_width="wrap_content" android:id="@+id/imgAndroid"
    android:background="@drawable/android1"
    android:layout_gravity="center_horizontal" />

Now in your Activity class link the ImageView and create a AnimationDrawable implementing your class in this way:

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;

public class Main extends Activity {
    private ImageView imgAndroid;
    private AnimationDrawable mAnimation;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    imgAndroid = (ImageView)findViewById(R.id.imgAndroid);
    imgAndroid.setBackgroundResource(R.drawable.animation);    

    mAnimation = (AnimationDrawable)imgAndroid.getBackground();
    mAnimation.start();

}

}

And to stop the animation use: mAnimation.stop();

1

Hello!

I had problems to implement animated gif in my project and solved in a very simple way:

<ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content" android:id="@+id/imgAndroid"
android:background="@drawable/android1"
android:layout_gravity="center_horizontal" />

I switched to: (removed the image reference android:background="@drawable/android1")

<ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content" android:id="@+id/imgAndroid"
android:layout_gravity="center_horizontal" />

Implementation is the same as already informed:

Glide.with(this).load(R.drawable.gift_animated).asGif().into(img_splash_animated);

It might help someone.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.