使用代码构建android基本动画,基本动画有以下:
AlphaAnimation:渐变透明度动画
RotateAnimation:旋转动画
ScaleAnimation:伸缩动画
TranslateAnimation:平移动画
可以定义出一些常用的动画效果,也可以自定义一些动画,把参数预留出来。
可以定义一些组合动画效果,例如:从里到外的效果。对于组合的动画效果,使用基本的动画效果构建起来。
使用AnimationSet添加到此集合中,让其里面的动画一起起效果,可以看到意想不到的效果。
代码工具类
/**
* 使用代码设计的动画
*
*/
public class AnimationCodeUtils {
private static Animation anim;
/**
* 默认的是: 1.透明度从1.0~0, 2.动画时间3000毫秒 3.不停在动画结束状态
*
* @return 渐变透明度动画
*/
public static AlphaAnimation alphaAnimation1To0() {
AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
alpha.setDuration(3000);
return alpha;
}
/**
* 默认的是: 1.透明度从0.0~1, 2.动画时间3000毫秒 3.不停在动画结束状态
*
* @return 渐变透明度动画
*/
public static AlphaAnimation alphaAnimation0To1() {
AlphaAnimation alpha = new AlphaAnimation(0.0f, 1.0f);
alpha.setDuration(3000);
return alpha;
}
/**
* @param fromAlpha
* 开始透明度(0~1.0)
* @param toAlpha
* 结束透明度(0~1.0)
* @param durationMillis
* 动画执行时间
* @param fillAfter
* 是否停留在动画结束状态
* @return
*/
public static AlphaAnimation alphaAnimationCustom(float fromAlpha,
float toAlpha, long durationMillis, boolean fillAfter) {
AlphaAnimation alpha = new AlphaAnimation(fromAlpha, toAlpha);
alpha.setDuration(durationMillis);
alpha.setFillAfter(fillAfter);
return alpha;
}
/**
* 旋转动画,顺时针360 默认:1.旋转角度:0~360 2.相对于自己的中心位置 3.旋转时间为30004.不停留在动画结束状态
*
* @return
*/
public static RotateAnimation rotateAnimation0to360() {
RotateAnimation rotate = new RotateAnimation(0f, 360f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(3000);
rotate.setFillAfter(false);
return rotate;
}
/**
* 旋转动画,逆时针360 默认:1.旋转角度:360-0 2.相对于自己的中心位置 3.旋转时间为30004.不停留在动画结束状态
*
* @return
*/
public static RotateAnimation rotateAnimation360to0() {
RotateAnimation rotate = new RotateAnimation(360f, 0f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(3000);
rotate.setFillAfter(false);
return rotate;
}
/**
*
* 旋转动画,自定义
*
* @return
*/
/**
* @param fromDegrees
* 开始旋转的角度
* @param toDegrees
* 结束的角度
* @param pivotXType
* X方向相对位置类型
* @param pivotXValue
* X方向相对位置的值
* @param pivotYType
* Y方向相对位置类型
* @param pivotYValue
* Y方向相对位置的值
* @param durationMillis
* 动画执行时间
* @param fillAfter
* 是否停留在动画结束时间
* @return
*/
public static RotateAnimation rotateAnimationCustom(float fromDegrees,
float toDegrees, int pivotXType, float pivotXValue, int pivotYType,
float pivotYValue, long durationMillis, boolean fillAfter) {
RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees,
pivotXType, pivotXValue, pivotYType, pivotYValue);
rotate.setDuration(durationMillis);
rotate.setFillAfter(fillAfter);
return rotate;
}
/**
* 伸缩动画 默认:相对于自己,向左上角缩小,从有到无,动画时间3000,不停留在动画结束状态
*/
public static ScaleAnimation scaleAnimation1to0() {
ScaleAnimation scale = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,
ScaleAnimation.RELATIVE_TO_SELF, 0f,
ScaleAnimation.RELATIVE_TO_SELF, 0f);
scale.setDuration(3000);
scale.setFillAfter(false);
return scale;
}
/**
* 伸缩动画 默认:相对于自己,向左上角放大,从无到有,动画时间3000,不停留在动画结束状态
*/
public static ScaleAnimation scaleAnimation0to1() {
ScaleAnimation scale = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
ScaleAnimation.RELATIVE_TO_SELF, 0f,
ScaleAnimation.RELATIVE_TO_SELF, 0f);
scale.setDuration(3000);
scale.setFillAfter(false);
return scale;
}
/**
* 伸缩动画
*/
/**
* @param fromX
* X方向开始缩放的角度(0-1) 0是不显示,1是全部显示
* @param toX
* X方向结束缩放的角度(0-1) 0是不显示,1是全部显示
* @param fromY
* Y方向开始缩放的角度(0-1) 0是不显示,1是全部显示
* @param toY
* Y方向结束缩放的角度(0-1) 0是不显示,1是全部显示
* @param pivotXType
* X方向相对类型
* @param pivotXValue
* X方向相对于的值
* @param pivotYType
* Y方向相对类型
* @param pivotYValue
* Y方向相对于的值
* @param durationMillis
* 动画执行时间
* @param fillAfter
* 是否停留在动画结束的状态
* @return
*/
public static ScaleAnimation scaleAnimationCustom(float fromX, float toX,
float fromY, float toY, int pivotXType, float pivotXValue,
int pivotYType, float pivotYValue, long durationMillis,
boolean fillAfter) {
ScaleAnimation scale = new ScaleAnimation(fromX, toX, fromY, toY,
pivotXType, pivotXValue, pivotYType, pivotYValue);
scale.setDuration(durationMillis);
scale.setFillAfter(fillAfter);
return scale;
}
/**
* 平移动画:从左向右 平移
*
* @return
*/
public static TranslateAnimation translateAnimationLeftToRight() {
TranslateAnimation translate = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF, 0.0f,
TranslateAnimation.RELATIVE_TO_SELF, 1.0f,
TranslateAnimation.RELATIVE_TO_SELF, 0.0f,
TranslateAnimation.RELATIVE_TO_SELF, 0.0f);
translate.setDuration(3000);
translate.setFillAfter(false);
return translate;
}
/**
* 平移动画:从右向左 平移
*
* @return
*/
public static TranslateAnimation translateAnimationRightToLeft() {
TranslateAnimation translate = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF, 1.0f,
TranslateAnimation.RELATIVE_TO_SELF, 0.0f,
TranslateAnimation.RELATIVE_TO_SELF, 0.0f,
TranslateAnimation.RELATIVE_TO_SELF, 0.0f);
translate.setDuration(3000);
translate.setFillAfter(false);
return translate;
}
/**
* 平移动画:自定义
*
* @return
*/
/**
* @param fromXType
* X方向开始平移相对类型
* @param fromXValue
* X方向开始平移相对值
* @param toXType
* X方向结束平移相对类型
* @param toXValue
* X方向结束平移相对值
* @param fromYType
* Y方向开始平移相对类型
* @param fromYValue
* Y方向开始平移相对值
* @param toYType
* Y方向结束平移相对类型
* @param toYValue
* Y方向结束平移相对值
* @param durationMillis
* 动画执行时间
* @param fillAfter
* 是否停留在动画结束状态
* @return
*/
public static TranslateAnimation translateAnimationCustom(int fromXType,
float fromXValue, int toXType, float toXValue, int fromYType,
float fromYValue, int toYType, float toYValue, long durationMillis,
boolean fillAfter) {
TranslateAnimation translate = new TranslateAnimation(fromXType,
fromXValue, toXType, toXValue, fromYType, fromYValue, toYType,
toYValue);
translate.setDuration(durationMillis);
translate.setFillAfter(fillAfter);
return translate;
}
/**
* 动画集合:渐变-缩放-旋转;效果,从里向外旋转放大
*
* @return
*/
public static Animation animationSet() {
AnimationSet aniset = new AnimationSet(true);// true表示一起起作用
aniset.addAnimation(alphaAnimation0To1());
aniset.addAnimation(AnimationCodeUtils.scaleAnimationCustom(0f, 1f, 0f,
1f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f, 3000, false));
aniset.addAnimation(rotateAnimation0to360());
return aniset;
}
}
源码下载:http://download.csdn.net/detail/forwardyzk/8317823
动画效果图: