Animation
android上X、Y上坐标
手机屏幕的左上角为原心,分别向右和向下为正方向。由此为各种animation方向的标准
总的来说Animation分为两类:
- Tweened Animations
- TranslateAnimation类,移动效果
-
/** * Constructor to use when building a TranslateAnimation from code * * @param fromXType Specifies how fromXValue should be interpreted. One of * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or * Animation.RELATIVE_TO_PARENT. * @param 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. * @param toXType Specifies how toXValue should be interpreted. One of * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or * Animation.RELATIVE_TO_PARENT. * @param 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. * @param fromYType Specifies how fromYValue should be interpreted. One of * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or * Animation.RELATIVE_TO_PARENT. * @param 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. * @param toYType Specifies how toYValue should be interpreted. One of * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or * Animation.RELATIVE_TO_PARENT. * @param 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. */ public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) { mFromXValue = fromXValue; mToXValue = toXValue; mFromYValue = fromYValue; mToYValue = toYValue; mFromXType = fromXType; mToXType = toXType; mFromYType = fromYType; mToYType = toYType; }
-
- RotateAnimation类,旋转效果。
-
/** * Constructor to use when building a RotateAnimation from code * * @param fromDegrees Rotation offset to apply at the start of the * animation. * * @param toDegrees Rotation offset to apply at the end of the animation. * * @param pivotX The X coordinate of the point about which the object is * being rotated, specified as an absolute number where 0 is the left * edge. * @param pivotY The Y coordinate of the point about which the object is * being rotated, specified as an absolute number where 0 is the top * edge. */
//旋转表示一个控件绕着一个点做旋转(平面旋转)。这个旋转是围绕一个点,而这个点是由百分比决定的,比如相对于自身时x为0.5,y为0.5,那么这个点就是该控件的中心;如果x为1,y为1,那么这个点就是控件的右下角;相对父控件时x为0.5,y为0.5,那么这个点就是该父控件的中心。
public RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY) { mFromDegrees = fromDegrees; mToDegrees = toDegrees; mPivotXType = ABSOLUTE; mPivotYType = ABSOLUTE; mPivotXValue = pivotX; mPivotYValue = pivotY; initializePivotPoint(); } - ScaleAnimation类,缩放效果
-
/** * Constructor to use when building a ScaleAnimation from code * * @param fromX Horizontal scaling factor to apply at the start of the * animation * @param toX Horizontal scaling factor to apply at the end of the animation * @param fromY Vertical scaling factor to apply at the start of the * animation * @param toY Vertical scaling factor to apply at the end of the animation */ public ScaleAnimation(float fromX, float toX, float fromY, float toY) { mResources = null; mFromX = fromX; mToX = toX; mFromY = fromY; mToY = toY; mPivotX = 0; mPivotY = 0; }
-
- AlphaAnimation类,透明度变化效果
-
/** * Constructor to use when building an AlphaAnimation from code * * @param fromAlpha Starting alpha value for the animation, where 1.0 means * fully opaque and 0.0 means fully transparent. * @param toAlpha Ending alpha value for the animation. */
//fromAlpha:开始时刻的透明度,取值范围0~1。
//toAlpha:结束时刻的透明度,取值范围0~1。public AlphaAnimation(float fromAlpha, float toAlpha) { mFromAlpha = fromAlpha; mToAlpha = toAlpha; }
-
- TranslateAnimation类,移动效果
- Frame-by-Frame Animations,创建一个Drawable序列,通过按照一定的顺序轮换达到动画的效果
四个TweenAnimation的通用方法
- setDuration(long durationMills) 设置动画的持续时间
- setFillAfter(Boolean fillAfter) ,如果设为true,则动画执行结束完,控件停留在执行结束的状态
- setFillBefore(Boolean fillAfter),如果设为true,则动画执行结束后,控件回到动画执行前的状态
- setStartOffSet(long startOffSet) ,设置动画开始前等待的时间
- setRepeatCount(int repeatCount),设置动画重复的次数
当然以上的动画都是单个的效果,如果想要混合两个以上的动画效果,就要用到AnimationSet