AndroidAniamtion之AlphaAnimation和TranslateAniamtion详细用法
上一篇《Android Animation之ScaleAnimation用法详解》介绍了ScaleAniamtion的用法,这一篇因为AlphaAnimation和TranslateAniamtion的用法相对简单一些,所以一块讲一下了,不再进行单独讲解了,下一篇单独讲一下RotationAniamtion。
一、 AlphaAniamtion
其构造方法为public AlphaAnimation(float fromAlpha, float toAlpha)
fromAlpha和toAlpha分别表示动画开始和结束时的透明度,取值范围为【0,1.0】,0表示完全透明,1表示完全不透明。代码举例:
AnimationalphaAniamtion = new AlphaAnimation(0.1f,0.9f);
alphaAniamtion.setFillAfter(true);
alphaAniamtion.setDuration(1000);
imageView.setAnimation(alphaAniamtion);
AlphaAniamtion用法很简单,这里不再详细解说了。
二、 TranslateAniamtion
TranslateAnimation有两个构造方法:
1、 public TranslateAnimation(floatfromXDelta, float toXDelta, float fromYDelta, float
toYDelta)
fromXDelta表示动画开始是控件左上角的x坐标,toXDelta表示动画结束时控件左上角的x坐标,fromYDelta表示动画开始是控件左上角的y坐标,toYDelta表示动画结束时控件左上角的y坐标.代码举例:
Animation translateAniamtion = newTranslateAnimation(100,100f,0,200f);
translateAniamtion.setFillAfter(true);
translateAniamtion.setDuration(1000);
imageView.setAnimation(translateAniamtion);
代码解读:这个平移动画的初始位置是(100,0),终止位置是(100,200),我们可以这样理解,就是播放动画的控件的左上角从(100,0)平移到(100,200),坐标系是相对于控件本身来说的,即坐标原点是控件的左上角。
2、 public TranslateAnimation(intfromXType, float fromXValue public , int toXType, float toXValue, intfromYType, float fromYValue, int toYType, float toYValue)
这个构造方法一共有8个参数,作用也是用来指定动画的起始位置和结束位置,不过这个构造方法可以用3中形式来指定,具体由哪种形式来指定,我们以fromXValue为例,如果其前面的参数fromXType的值为Animation.ABSOLUTE,那么fromXValue取值的意义和构造方法1一样,都是绝对坐标值,而如果fromXType的值为Animation.RELATIVE_TO_SELF或者Animation.RELATIVE_TO_PARENT,则fromXValue的值不再表示绝对值,而是一个百分比,分情况讨论一下:如果fromXType = Animation.RELATIVE_TO_SELF,那么控件的宽度乘以fromXValue就是起始位置的很坐标,控件的高度乘以fromYValue就是起始位置的纵坐标,结束位置的坐标同理可得;如果fromXType= Animation.RELATIVE_TO_PARENT,那么父容器的宽度乘以fromXValue就是起始位置的很坐标,父容器的高度乘以fromYValue就是起始位置的纵坐标,结束位置的坐标同理可得。代码举例:
Animation translateAniamtion = newTranslateAnimation(Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,1.0f,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1.0f);
translateAniamtion.setFillAfter(true);
translateAniamtion.setDuration(1000);
imageView.setAnimation(translateAniamtion);