I created one simple demo project, which does the animation using AnimationDrawable class of android.
Here is my java code:
public class TestAnimationActivity extends Activity {
/** Called when the activity is first created. */
ImageView imgCircleWhite,imgCircleYellow;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgCircleYellow = (ImageView)findViewById(R.id.imgCircleYellow);
animateState(true, imgCircleYellow);
}
public void animateState(boolean flag,ImageView imageView)
{
imgCircleYellow.setBackgroundResource(R.drawable.animate_circle_sensor_1);
AnimationDrawable yourAnimation = (AnimationDrawable) imageView.getBackground();
if(!flag)
{
//imageView.getAnimation().reset();
imageView.setBackgroundResource(R.drawable.circle_label_white_1);
yourAnimation.stop();
}
else
{
imageView.setBackgroundResource(R.drawable.animate_circle_sensor_1);
yourAnimation = (AnimationDrawable) imageView.getBackground();
yourAnimation.start();
}
}
}
Here is my main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ImageView android:id="@+id/imgCircleYellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/circle_label_yellow_1"/>
</LinearLayout>
Below is my animate_sensor_circle_1.xml, which I’ve put in drawable folder to use with:
<?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/circle_label_white_2"
android:duration="50"/>
<item
android:drawable="@drawable/circle_label_yellow_1"
android:duration="50"/>
</animation-list>
When I run the app, only the first item in the above xml file gets displayed, but animation does not occur repeatedly as it should done.
Can anyone direct me where I am going wrong?
It is not the right time to call the
AnimationDrawable.start(). You should do this after the view initialization has been completed. Try this:Check this for more information: https://stackoverflow.com/a/5490922/813135