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

3

Well, I've been having this problem for some time, I tried to solve using StackOverflow answers but I did not succeed.

I need to put an animated GIF image in my activity but ImageView does not play a .gif image, it just shows it as an image without animation.

Can anyone help me?

Remembering that I'm programming for Android.

    
asked by anonymous 24.09.2015 / 19:39

3 answers

6

You can use lib Glide for this

followtheexample:

InGradleadd

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

}

exampleinyourActivity

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

If it's in 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 the Gif or drawable and third your ImageView that in the case is the id of it.

Link to the link

    
24.09.2015 / 19:49
5

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

Then create an xml named 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 ImageView and create AnimationDrawable by implementing your class 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();

    
24.09.2015 / 19:52
1

Hello

I had problems implementing the animated gif in my project and solved it 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 changed it to: (I removed the reference from the android image: background="@ drawable / android1")

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

The implementation is the same already informed:

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

It might help someone.

    
01.11.2017 / 02:08