Search code examples
androidanimationdrawable

Animation Drawable not working in GingerBread


This question is already been asked,but i didn't find any suitable answers and so I'm posting the question. I've a ProgressBar animation which has multiple images and loads these images like a progressbar. I'm able to run it successfully in 4.0 and higher versions but it doesn't work in 2.3.How to make it work in lower versions?

My Animation Drawable XML file

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false"
    android:pivotX="50%"
    android:pivotY="50%" >
    <item android:drawable="@drawable/p1_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p2_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p3_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p4_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p5_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p6_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p7_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p8_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p9_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p10_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p11_wheel" android:duration="200"/>
    <item android:drawable="@drawable/p12_wheel" android:duration="200"/>
</animation-list>

My XML File

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerInParent="true"/>


</RelativeLayout>

My Java File

package com.example.progressbarsample;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.DrawableContainer;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class MainActivity extends Activity {
    ImageView progressbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressbar = (ImageView) findViewById(R.id.imageView1);
        progressbar.setBackgroundResource(R.drawable.progressbar);
        final AnimationDrawable frame = (AnimationDrawable) progressbar.getBackground();
        progressbar.post(new Runnable() {

            @Override
            public void run() {
                frame.start();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Solution

  • At Last I found a solution to this.

     progressbar = (ImageView) findViewById(R.id.imageView1);
     progressbar.setBackgroundResource(R.drawable.progressbar);
     final AnimationDrawable frame = (AnimationDrawable)getResources.getDrawable(R.drawable.progressbar);  \\progressbar is the anim.xml file
     progressbar.setBackgroundDrawable(frame);
     progressbar.post(new Runnable() {
    
                @Override
                public void run() {
                    frame.start();
                }
            });
        }