Android动画之TranslateAnimation用法案例详解

  public class MainActivity extends Activity {

  ImageView image;

  Button start;

  Button cancel;

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  image = (ImageView) findViewById(R.id.main_img);

  start = (Button) findViewById(R.id.main_start);

  cancel = (Button) findViewById(R.id.main_cancel);

  /** 设置位移动画 向右位移150 */

  final TranslateAnimation animation = new TranslateAnimation(0, 150,0, 0);

  animation.setDuration(2000);//设置动画持续时间

  animation.setRepeatCount(2);//设置重复次数

  animation.setRepeatMode(Animation.REVERSE);//设置 反方向执行

  start.setOnClickListener(new OnClickListener() {

  public void onClick(View arg0) {

  image.setAnimation(animation);

  /** 开始动画 */

  animation.startNow();

  }

  });

  cancel.setOnClickListener(new OnClickListener() {

  public void onClick(View v) {

  /** 结束动画 */

  animation.cancel();

  }

  });

  }

  }