Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate .gif images in an android?

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?

like image 739
Danny Avatar asked Feb 22 '13 10:02

Danny


People also ask

How do you animate a GIF on Android?

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.

Can we use GIF in Android?

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.

Can you animate GIFs?

GIPHY's GIF MakerGIF Maker allows you to create animated GIFs from video files, YouTube links, existing GIFs, and even still photos.


2 Answers

To give a precise and complete answer here is what you need to do step wise:

  1. You would need to have different .png images which will act as frames for your animation. Save them in res/drawable folder.

  2. 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>
    
  3. 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" />
    //...
    
  4. 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.

like image 64
Shobhit Puri Avatar answered Oct 10 '22 10:10

Shobhit Puri


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.

like image 44
Piotr Avatar answered Oct 10 '22 08:10

Piotr