关于android移动动画和缩放动画的构造函数简介

来源:互联网 发布:java线程概念 编辑:程序博客网 时间:2024/06/05 07:59

首先贴上大神的一篇文章  http://www.360doc.com/content/13/0102/22/6541311_257754535.shtml

简单介绍一下在android里面的移动动画和缩放动画

一.移动动画,这个动画的作用是将控件在屏幕上移动,android对应的类是TranslateAnimation

这个类支持三个构造函数,分别是

1.TranslateAnimation(Context context,AttributeSet attr)

Constructor used when a TranslateAnimation is loaded from a resource.

Parameters:
context Application context to use
attrs Attribute set from which to read values
意思是这个构造器是当我们从资源文件中加载这个动画时候用的,一个是当前上下文,一个是资源文件的配置


2.TranslateAnimation (float fromXDelta,float toXDelta,float fromYDelta,float toYDelta)

Constructor to use when building a TranslateAnimation from code

Parameters:
fromXDelta Change in X coordinate to apply at the start of the animation
toXDelta Change in X coordinate to apply at the end of the animation
fromYDelta Change in Y coordinate to apply at the start of the animation
toYDelta Change in Y coordinate to apply at the end of the animation
这个构造器当我们使用代码构建一个动画时调用,四个参数的意思分别是

参数:

       fromXDelta 动画开始时在x轴上相对初始位置的偏移量单位是绝对单位(可能是像素点),显然,当我们设置为零的时候动画开始时控件的初始位置在x轴方向的位置和布局文件中的相同,设置为正数则会向右移动一些位置,反之向左

      toXDelta 动画播放过程中这个控件在x轴方向移动的距离,设为零则这个控件在动画播放过程中只是上下移动,左右不动(当然toYDelta不为零的时候上下才动)

      接下来的两个参数与上面是对应的,只是在y轴方向负数是指向上移动,正数是向下移动

3.TranslateAnimation(int fromXType,float from XValue,int toXType,float toXValue,int fromYType,float from YValue,int toYType,float toYValue,i)

Constructor to use when building a TranslateAnimation from code

Parameters:
fromXType Specifies how fromXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
fromXValue Change in X coordinate to apply at the start of the animation. This value can either be an absolute number if fromXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
toXType Specifies how toXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
toXValue Change in X coordinate to apply at the end of the animation. This value can either be an absolute number if toXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
fromYType Specifies how fromYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
fromYValue Change in Y coordinate to apply at the start of the animation. This value can either be an absolute number if fromYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
toYType Specifies how toYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
toYValue Change in Y coordinate to apply at the end of the animation. This value can either be an absolute number if toYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.

这个构造器是最长的,但是也是对称的,所以也是很好记的,这个构造器的参数的类型主要是这两样
Type和Value,其中Type指的是距离的计量单位取的方法,主要是三种,相对父控件的尺寸,Animation.RELATIVE_TO_PARENT,相对自身的尺寸,Animation.RELATIVE_TO_SELF,
采取这两种单位来算的话,接下来的参数的每一个单位长度就是父控件或者自身的长或宽的大小,绝对单位Animation.ABSOLUTE采取这两种方式的话还是之前的第二个构造方法的计量方法,这样的话其实可以发现如果四个单位计量方法参数值全部置为Animation.ABSOLUTE 的话。就和第二个构造器相同了


第二个构造器构造一个从屏幕右上角向屏幕左下角移动到中间的例子
<span style="font-size:18px;">animation = new TranslateAnimation( 0, -420, 0, 268);</span>

第三个构造器实现同样的效果
使用相对父控件尺寸计量距离的方法
<span style="font-size:18px;">animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0,Animation.RELATIVE_TO_PARENT, -0.4f,Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT,0.4f);//不设成一半(0.5)的原因是这个移动是将这个控件的左上角移动///那么多单位,一半就多了</span>


使用相对自己尺寸的计量方法
<span style="font-size:18px;">animation2 = new ScaleAnimation(1f, 3.4f, 1f, 4f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);</span>

使用绝对单位的方法
<span style="font-size:18px;">animation = new TranslateAnimation(Animation.ABSOLUTE, 0,Animation.ABSOLUTE, -420, Animation.ABSOLUTE, 0,Animation.ABSOLUTE, 268);</span>

二.缩放动画

这个动画可以实现在屏幕上某个位置开始对控件的大小进行缩放的操作,在android中对应的类是ScaleAnimation

1.这个类也有一个参数列表和上面第一个构造器相同的构造器,这里就不讲了

2.ScaleAnimation(float fromX,float toX,float fromY,float toY)

Constructor to use when building a ScaleAnimation from code

Parameters:
fromX Horizontal scaling factor to apply at the start of the animation
toX Horizontal scaling factor to apply at the end of the animation
fromY Vertical scaling factor to apply at the start of the animation
toY Vertical scaling factor to apply at the end of the animation

参数:
          动画开始时这个控件x轴方向的大小,就是宽度,单位是自身的长度,设为1表示不变,
          动画结束时这个控件x轴方向的大小,就是宽度,单位是自身的长度,设为1表示不变
  动画开始时这个控件Y轴方向的大小,就是宽度,单位是自身的长度,设为1表示不变,
  动画结束时这个控件Y轴方向的大小,就是宽度,单位是自身的长度,设为1表示不变这个构造方法构造出来的动画仅仅是将控件的大小改变了,没有移动控件的位置,尺寸改变方式是向右和向下扩展控件,也就是说左上角的那个点的位置没变

2.ScaleAnimation(float fromX,float toX,float fromY,float toY,float pivotX,float pivotY)
Constructor to use when building a ScaleAnimation from code
Parameters:
fromX Horizontal scaling factor to apply at the start of the animation
toX Horizontal scaling factor to apply at the end of the animation
fromY Vertical scaling factor to apply at the start of the animation
toY Vertical scaling factor to apply at the end of the animation
pivotX The X coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the left edge. (This point remains fixed while the object changes size.)
pivotY The Y coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the top edge. (This point remains fixed while the object changes size.)
参数:
       前四个参数的类型和意义与前一个构造器的完全相同,就不赘述了,后两个是指定这个动画的执行方式的
        描述起来比较麻烦,上图

了解到有一个点在变化过程中位置不变就好办了,就暂时称这个点为重心点吧,后两个参数就是指定这个重心点用的,例如我的控件大小是400dp宽,200dp长,为了让控件在展开时四边均匀增长,就是重心在控件的几何中心,矩形对角线的焦点,我要这样构造
animation2 = new ScaleAnimation(1, 2, 1, 2, 200, 100);
3.ScaleAnimation(float fromX,float toX,float fromY,float toY,int pivotXType,float pivotXValue,int pivotYType,float pivotYValue )
Constructor to use when building a ScaleAnimation from code
Parameters:
fromX Horizontal scaling factor to apply at the start of the animation
toX Horizontal scaling factor to apply at the end of the animation
fromY Vertical scaling factor to apply at the start of the animation
toY Vertical scaling factor to apply at the end of the animation
pivotXType Specifies how pivotXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
pivotXValue The X coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the left edge. (This point remains fixed while the object changes size.) This value can either be an absolute number if pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
pivotYType Specifies how pivotYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
pivotYValue The Y coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the top edge. (This point remains fixed while the object changes size.) This value can either be an absolute number if pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.

这个构造器的作用就是在之前的基础上增加了单位的选择方案,这样就可以不一定使用dp作为单位,可以和父控件和自身比较来做


0 0