Monday 11 November 2013

Animation sample program in Android


This sample android program shows you how to do simple animation in Android. In this program the xml file ani.xml is used to rotate the images in sequence. The images must be copied to the /res/drawable folder together with the ani.xml file.
The FrameAnimation1.java file is as follows:
package com.javasamples.ani;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class FrameAnimation1 extends Activity {
 Button b;

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

 private void setupButton() {
  b = (Button) this.findViewById(R.id.startFAButtonId);
  b.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    parentButtonClicked(v);
   }
  });
 }

 private void parentButtonClicked(View v) {
  animate();
 }

 private void animate() {
  ImageView imgView = (ImageView) findViewById(R.id.animationImage);
  // imgView.setVisibility(ImageView.VISIBLE);
  imgView.setBackgroundResource(R.drawable.ani);

  AnimationDrawable frameAnimation = (AnimationDrawable) imgView
    .getBackground();

  if (frameAnimation.isRunning()) {
   frameAnimation.stop();
   b.setText("Start");
  } else {
   frameAnimation.start();
   b.setText("Stop");
  }
 }
}

No comments:

Post a Comment