标签:
img_change.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub final String startname = tv_start.getText().toString(); final String finalname = tv_final.getText().toString(); //设置平移动画 TranslateAnimation translateAnimation = new TranslateAnimation(0, 150,0, 0); //采用加速方式 translateAnimation.setInterpolator(new AccelerateInterpolator()); //耗时 translateAnimation.setDuration(3000); translateAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub tv_start.setText(finalname); } }); TranslateAnimation translateAnimation1 = new TranslateAnimation(0, -150,0, 0); //采用加速方式 translateAnimation1.setInterpolator(new AccelerateInterpolator()); //耗时 translateAnimation1.setDuration(300); translateAnimation1.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub tv_final.setText(startname); } }); tv_final.startAnimation(translateAnimation1); tv_start.startAnimation(translateAnimation); } });
首先要把对象创建出来,参数为表示动画开始离当前view的x值,动画结束里当前view的x值,动画开始离当前view的y值,动画结束离当前view的y值
TranslateAnimation translateAnimation = new TranslateAnimation(0, 150,0, 0);
设置移动的方式,加速 匀速
translateAnimation.setInterpolator(new AccelerateInterpolator());
设置动画用时
translateAnimation.setDuration(300);
最后实现监听,在监听里有三个方法,分别是动画开始,动画重叠,动画的结束
translateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
tv_start.setText(finalname);
}
最后开始动画就可以了
tv_final.startAnimation(translateAnimation1);
标签:
原文地址:http://www.cnblogs.com/84126858jmz/p/4913866.html