Private void animate

ImageView imgView = (ImageView)findViewByld(R.id.imageView);

imgView.setVisibility(ImageView.VISIBLE);

imgView.setBackgroundResource(R.drawable.frame_animation);

AnimationDrawable frameAnimation =

(AnimationDrawable) imgView.getBackground();

if (frameAnimation.isRunning()) {

frameAnimation.stop();

frameAnimation.stop(); frameAnimation.start();

The animate() method locates the ImageView in the current activity and sets its background to the AnimationDrawable identified by the resource R.drawable.frame_animation. The code then retrieves this object and performs the animation. The start/stop button is set up such that if the animation is running, clicking the button will stop it; if the animation is in a stopped state, clicking the button will start it.

As a note, if you set the OneShot parameter of the animation list to true, then the animation will stop after executing once. However, there is no clear-cut way to know when that happens. Although the animation ends when it plays the last picture, you have no callback telling you when it finishes. Because of this, there isn't a direct way to invoke another action in response to the completed animation.

That drawback aside, you can bring great visual effects to bear by drawing a number of images in succession through the simple process of frame-by-frame animation.

0 0

Post a comment