Here is the code for xml:
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/minepic" />
Here the minepic is a gif animated image but after running the application its just showing a static image.
Is there any solution about how to animate the .gif images in android application?
First you'll need to install GIFDroid from the Android Market, then open the app. Step 1: Press either the Select Video or Record Video button. Select Video will open your Gallery to choose a video. Step 2: Choose the section of the video you want to make into an animated GIF.
This example demonstrates how do I display animated gif images in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.
GIPHY's GIF MakerGIF Maker allows you to create animated GIFs from video files, YouTube links, existing GIFs, and even still photos.
To give a precise and complete answer here is what you need to do step wise:
You would need to have different .png
images which will act as
frames for your animation. Save them in res/drawable
folder.
Create anim.xml
file in res/drawable
folder with following content:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/image_3" android:duration="250" />
<item android:drawable="@drawable/image_2" android:duration="250" />
<item android:drawable="@drawable/image_1" android:duration="250" />
<item android:drawable="@drawable/image" android:duration="250" />
</animation-list>
In the layout xml
file inside which you want to show the animation:
//...
<ImageView
android:id="@+id/iv_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:contentDescription="Animation" />
//...
In the Java file which loads the the layout xml file and calls
setContentView
:
//...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView animImageView = (ImageView) findViewById(R.id.iv_animation);
animImageView.setBackgroundResource(R.drawable.anim);
animImageView.post(new Runnable() {
@Override
public void run() {
AnimationDrawable frameAnimation =
(AnimationDrawable) animImageView.getBackground();
frameAnimation.start();
}
});
// ... other code ...
}
// ...
In order to stop the animation you can call .stop()
on the AnimationDrawable
. For more details about the available methods, you can see AnimationDrawable documentation. Hope it helps someone.
I wouldn't recommend using Movie
or WebView
classes but ImageView
instead with source drawable set to animation-list
. Look at example(mydrawable.xml
):
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/image_1" android:duration="100" />
<item android:drawable="@drawable/image_2" android:duration="100" />
<item android:drawable="@drawable/image_3" android:duration="100" />
</animation-list>
// in your layout : <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/my_drawable" />
Obviously, you have to slice your .gif into seperate images before using this solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With