下面的代码展示了一个类似心跳的循环缩放动画:
ScaleAnimation animation = new ScaleAnimation(1, (float)0.93, 1, (float)0.93, Animation.RELATIVE_TO_SELF, 0.5f,1, 0.5f);
/**
* @param fromX 起始x轴位置,0为最小,1为原始,float形
* @param toX 同上
* @param fromY 同上T
* @param toY 同上
* @param pivotXType 用来约束pivotXValue的取值。取值有三种:Animation.ABSOLUTE,Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_PARENT
* Type:Animation.ABSOLUTE:绝对,如果设置这种类型,后面pivotXValue取值就必须是像素点;比如:控件X方向上的中心点,pivotXValue的取值mIvScale.getWidth() / 2f
* Animation.RELATIVE_TO_SELF:相对于控件自己,设置这种类型,后面pivotXValue取值就会去拿这个取值是乘上控件本身的宽度;比如:控件X方向上的中心点,pivotXValue的取值0.5f
* Animation.RELATIVE_TO_PARENT:相对于它父容器(这个父容器是指包括这个这个做动画控件的外一层控件), 原理同上,
* @param pivotXValue 配合pivotXType使用,原理在上面
* @param pivotYType 同from/to
* @param pivotYValue 原理同上
*/
animation.setDuration(700);
//设置持续时间
animation.setFillAfter(false);
//设置动画结束之后的状态是否是动画的最终状态,true,表示是保持动画结束时的最终状态
animation.setRepeatCount(99999);
//设置循环次数
animation.setRepeatMode(Animation.REVERSE);
//设置循环方式,REVERSE代表往复循环
xxx.startAnimation(animation);
//开始动画,xxx为你要设置动画的控件对象。