Question :
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.
Answer :
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
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();
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.