• 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

  • 相关阅读:
    (转)深入理解C语言指针的奥秘
    (转)C语言在哪里?
    [转]12个摄影基本法则
    光圈使用
    ArcIMS 连接器.NET Link 使用方法
    在asp.net中使用xml文件的两种类型及用法
    利用XSL双向转换XML文档
    光圈 暴光 快门对比
    保护眼睛的电脑颜色设置
    vb.net2005动态添加网页控件的事件
  • 原文地址:https://www.cnblogs.com/qingblog/p/2629837.html
Copyright © 2020-2023  润新知