Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android animation-list

I have read several articles but none have fixed my problem using an animation list. I already know I cant start the animation in onCreate() so I placed it inside an onClick() but it still does not work...

public class Main extends Activity implements OnClickListener {
/** Called when the activity is first created. */

AnimationDrawable explosionAnimation;
Button btnGo;
ImageView explosionImage;

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  explosionImage = (ImageView) findViewById(R.id.balloon);
  explosionImage.setBackgroundResource(R.drawable.explosion);
  explosionAnimation = (AnimationDrawable) explosionImage.getBackground();

  btnGo = (Button)findViewById(R.id.btnGo);
  btnGo.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.btnGo:
        explosionAnimation.start();
        break;
    }
}
} 

explosion.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
android:oneshot="true">

<item android:drawable="@drawable/explode_1" android:duration="200" />
<item android:drawable="@drawable/explode_2" android:duration="200" />
<item android:drawable="@drawable/explode_3" android:duration="200" />
<item android:drawable="@drawable/explode_4" android:duration="200" />
<item android:drawable="@drawable/explode_5" android:duration="200" />

</animation-list>

main.xml

<?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/balloon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="30dp"
    android:src="@drawable/balloon" />

<Button 
    android:id = "@+id/btnGo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="10dp"
    android:text="Play"
    />

</LinearLayout>
like image 709
Zachary Avatar asked Jan 31 '26 09:01

Zachary


1 Answers

On your main.xml change the image view src to android:src="@drawable/explosion"

then on your Main.java

change

explosionAnimation=(AnimationDrawable)((ImageView) explosionImage).getDrawable();

try it. it should work

like image 113
RoRo Avatar answered Feb 02 '26 23:02

RoRo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!