Dipak Ahmedabad
Dipak Ahmedabad

Reputation: 55

Frame Animation on button click in Android

My Problem is I have Some Images & I used frame animation to display this images on click event of button but if i click button first time the image is display in sequence & if i click this button another time that time the image is not displayed. following is my code.

Animation.java file:-

public class Animation extends Activity {

    Button mBtnOK;
    AnimationDrawable frameAnimation;
    ImageView imgView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mBtnOK = (Button) findViewById(R.id.mBtnOK);
        mBtnOK.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                animate();
            }
        });

    }

    private void animate() {
        imgView = (ImageView) findViewById(R.id.simple_anim);
        imgView.setVisibility(ImageView.VISIBLE);
        imgView.setBackgroundResource(R.anim.simple_animation);
        AnimationDrawable frameAnimation = (AnimationDrawable) imgView
                .getBackground();
        frameAnimation.start();
        frameAnimation.setOneShot(true);
    }
}

Animation file:-

<?xml version="1.0" encoding="utf-8"?>
 <animation-list xmlns:android="http://schemas.android.com/apk/res/android" id="selected" android:oneshot="false">
  <item android:drawable="@drawable/monkey_1" android:duration="50" />
  <item android:drawable="@drawable/monkey_2" android:duration="50" />
  <item android:drawable="@drawable/monkey_3" android:duration="50" />
  <item android:drawable="@drawable/monkey_4" android:duration="50" />
  <item android:drawable="@drawable/monkey_5" android:duration="50" />
  <item android:drawable="@drawable/monkey_6" android:duration="50" />
  <item android:drawable="@drawable/monkey_7" android:duration="50" />
  <item android:drawable="@drawable/monkey_8" android:duration="50" />
  <item android:drawable="@drawable/monkey_9" android:duration="50" />
  <item android:drawable="@drawable/monkey_10" android:duration="50" />
 </animation-list>

Upvotes: 2

Views: 2865

Answers (2)

devunwired
devunwired

Reputation: 63303

The only way to restart a frame animation is to use the setVisible() which contains a flag to force the animation to reset to the first frame. If you modify the animating section of code like so: AnimationDrawable frameAnimation = (AnimationDrawable) imgView.getBackground(); frameAnimation.setOneShot(true); frameAnimation.setVisible(true, true); frameAnimation.start();

The animation should always start from the first frame and run to completion each time you click the button. The animation can also be reset by toggling visibility on the drawable itself, instead of the ImageView that contains it.

HTH

Upvotes: 1

Scorpion
Scorpion

Reputation: 6901

@Dipak,

I have done with the animation using the same way you have done. Try to add this code, hope your error will be resolved. And also one more thing is use thread to run the animation. This will surely run it nicely.

if(frameAnimation.isRunning()) {
    frameAnimation.stop();
    frameAnimation.start();
}

Upvotes: 0

Related Questions