標籤:android 基本動畫 動畫集合 animation
使用代碼構建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
動畫:
android基本動畫,代碼構建動畫