我是靠谱客的博主 心灵美跳跳糖,最近开发中收集的这篇文章主要介绍TranslateAnimation类:位置变化动画类,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

http://book.51cto.com/art/201204/328247.htm

9.2  TranslateAnimation类:位置变化动画类

TranslateAnimation类是Android系统中的位置变化动画类,用于控制View对象的位置变化,该类继承于Animation类。TranslateAnimation类中的很多方法都与Animation类一致,该类中最常用的方法便是TranslateAnimation构造方法。

【基本语法】public TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

参数说明

fromXDelta:位置变化的起始点X坐标。

toXDelta:位置变化的结束点X坐标。

fromYDelta:位置变化的起始点Y坐标。

toYDelta:位置变化的结束点Y坐标。

【实例演示】下面通过代码来演示如何设置一个简单的位置变化动画效果。

  1. public class firstActivity extends Activity {  
  2. /** Called when the activity is first created. */  
  3. @Override  
  4. public void onCreate(Bundle savedInstanceState) {               //重载onCreate方法  
  5.     super.onCreate(savedInstanceState);  
  6.     setContentView(R.layout.main);  
  7.  
  8.     final ImageView image=(ImageView)findViewById(R.id.imageView1); //ImageView对象  
  9.     Button btn1=(Button)findViewById(R.id.button1);             //按钮对象  
  10.     Button btn2=(Button)findViewById(R.id.button2);  
  11.     final Animation translateAnimation=new TranslateAnimation(0,300,0,300);                                                                 //位置变化动画效果  
  12.  
  13.     btn1.setOnClickListener(new View.OnClickListener() {            //设置监听器  
  14.           
  15.         @Override  
  16.         public void onClick(View v) {  
  17.             // TODO Auto-generated method stub  
  18.             translateAnimation.setDuration(3000);               //设置动画持续时间  
  19.             translateAnimation.setRepeatCount(2);               //设置重复次数  
  20.             translateAnimation.setRepeatMode(Animation.REVERSE);    //反方向执行  
  21.             image.setAnimation(translateAnimation);             //设置动画效果  
  22.             translateAnimation.startNow();                      //启动动画  
  23.         }  
  24.     });  
  25.     btn2.setOnClickListener(new View.OnClickListener() {            //设置监听器  
  26.           
  27.         @Override  
  28.         public void onClick(View v) {  
  29.             // TODO Auto-generated method stub  
  30.             translateAnimation.cancel();                        //取消动画执行  
  31.         }  
  32.     });  
  33. }  
  34. }  

在这段代码中,首先通过TranslateAnimation构造方法创建了一个位置变化的动画对象。然后,在第一个按钮监听器中设置了动画的持续时间、重复次数和重复模式等,然后启动该动画。在第二个按钮监听器中取消该动画。读者运行这段代码,将看到图片沿如图9.7所示的路径往返运动。

经典集合:http://book.51cto.com/art/201204/328247.htm

最后

以上就是心灵美跳跳糖为你收集整理的TranslateAnimation类:位置变化动画类的全部内容,希望文章能够帮你解决TranslateAnimation类:位置变化动画类所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部