前言:只有比牛人跑得更快,纔有可能追上他的腳步
一、概述
這篇將爲大家講述如何使用代碼動態生成動畫以及插值器。先簡單列出各個便籤對應的類,方便大家理解:
scale | ScaleAnimation | 漸變尺寸伸縮動畫效果 |
alpha | AlphaAnimation | 漸變透明動畫效果 |
rotate | RotateAnimation | 畫面轉移旋轉動畫效果 |
translate | TranslateAnimation | 畫面轉移位置移動動畫效果 |
set | AnimationSet | 動畫集合 |
二、Animation公共類
上一篇文章中提到,Animation是所有動畫的基類(scale、alpha、rotate、translate),他所具有的屬性以及函數如下:
- android:duration setDuration(long) 動畫持續時間,以毫秒爲單位
- android:fillAfter setFillAfter(boolean) 如果設置爲true,控件動畫結束時,將保持動畫最後時的狀態
- android:fillBefore setFillBefore(boolean) 如果設置爲true,控件動畫結束時,將還原到動畫開始時的狀態
- android:fillEnabled setFillEnabled(boolean) 效果與fillBefore一致
- android:repeatCount setRepeatCount(int) 重複次數,如果設置爲infinite,則表示無限重複
- android:repeatMode setRepeatMode(int) 重複類型,有reverse和restart兩個值,取值爲REVERSE和RESTART,必須與repeatCount一起使用才能看到效果,因爲這裏是重複的類型
- android:interpolator setInterpolator(Interpolator) 插值器,其實就是指定的動作效果,比如彈跳,加速等
在第一篇《Android動畫篇(一)—— alpha、scale、translate、rotate、set的xml屬性及用法》已經講解了每個便籤的具體所有功能,這裏就不一一細講了,對於使用方法會在下面的個標籤中使用。
三、ScaleAnimation
1、自身屬性
在Scale標籤中,它有幾個自身屬性,這部分的屬性都是在scaleAnimation構造函數中添加的,我們來展示一下:
- android:fromXScale 起始X方向相對自身的縮放比例,浮點值,比如1.0表示無變化,0.5表示縮小一倍,2.0表示放大一倍
- android:toXScale 結尾X方向相對自身的縮放比例,浮點值;
- android:fromYScale 起始Y方向相對自身的縮放比例,浮點值;
- android:toYScale 結尾Y方向相對自身的縮放比例,浮點值;
- android:pivotX 縮放起點X軸座標,有數值,百分數,百分數p三種,比如:50,50%,50%p;當爲數值時,表示當前view左上角,即原點處加上50px,作爲起點縮放點X軸座標;當爲百分比時,表示當前view的左上角加上該控件寬的50%作爲縮放起點X軸座標;當爲50%p時,表示當前view的左上角加上父控件寬度50%作爲縮放起點X軸座標;
- android:pivotY 縮放起點Y軸座標,意義同上(如果有不理解的可參考我的第一篇文章《Android動畫篇(一)》)
2、構造函數
- ScaleAnimation(Context context, AttributeSet attrs) 從xml文件中加載動畫,基本用不到
- ScaleAnimation(float fromX, float toX, float fromY, float toY)
- ScaleAnimation(float fromX, float toX, float fromY, float toY,float pivotX, float pivotY)
- ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
第一個構造函數是從xml中加載動畫,基本上用不到,我們主要講述下下面三個構造方法
標籤屬性android:pivotX有三種取值,數值,百分數,百分數p,這個是關係到縮放起點的取值;體現在構造函數中,就是最後一個構造函數的pivotXType,它的取值有三種,Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF和Animation.RELATIVE_TO_PARENT;
android:pivotX的數值對應Animation.ABSOLUTE,百分數對應Animation.RELATIVE_TO_SELF,百分數p對應Animation.RELATIVE_TO_PARENT;
3、代碼使用
//初始化縮放動畫對象
ScaleAnimation scaleAnimation = new ScaleAnimation(0f, 2f, 0f, 2f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
//設置動畫時間
scaleAnimation.setDuration(700);
//設置動畫結束後還原動畫開始前的狀態
scaleAnimation.setFillBefore(true);
//控件使用動畫
mTextView.startAnimation(scaleAnimation);
效果如下:
四、AlphaAnimation
1、自身屬性
- android:fromAlpha 動畫開始的透明度,取值範圍0.0-1.0,0.0表示完全透明,1.0表示完全不透明
- android:toAlpha 動畫結束的透明度,同上
2、構造函數
- AlphaAnimation(Context context, AttributeSet attrs) 同樣的,從本地xml文件加載動畫,基本不使用
- AlphaAnimation(float fromAlpha, float toAlpha)
3、代碼使用
//初始化透明度動畫對象,傳入開始和結束的透明度值
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
//設置動畫時長2秒
alphaAnimation.setDuration(2000);
//設置動畫結束後還原動畫最開始時的狀態
alphaAnimation.fillBefore(true);
//控件啓動動畫
mTextView.startAnimation(alphaAnimation);
效果如下:
五、RotateAnimation
1、自身屬性
- android:fromDegrees 開始旋轉的角度度數,正值表示順時針方向度數,負值表示逆時針方向度數
- android:toDegrees 結束旋轉時的角度度數,正值表示順時針方向度數,負值表示逆時針方向度數
- android:pivotX 縮放起點X軸座標,可以是數值,百分比,百分比p,比如50,50%,50%p,含義已經在scale標籤中講述,這裏就不多描述了
- android:pivotY 縮放起點Y軸的座標,同上
2、構造函數
- RotateAnimation(Context context, AttributeSet attrs) 從本地xml文件中加載動畫,基本不用
- RotateAnimation(float fromDegrees, float toDegrees)
- RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
- RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
rotateAnimation和scaleAnimation差不多,關鍵是對於屬性anroid:pivotX的取值,同樣有三個選擇:Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF和Animation.RELATIVE_TO_PARENT;
3、代碼使用
RotateAnimation rotateAnimation = new RotateAnimation(0f, -650f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(2000);
rotateAnimation.setFillAfter(true);
mTextView.startAnimation(rotateAnimation);
效果如下:
六、TranslateAnimation
1、自身屬性
- android:fromXDelta 起始點的X軸座標,可以是數值,百分數,百分數p,比如50,50%,50%p ;具體意義已經在scale標籤中講述,這裏就不再重複講了;
- android:fromYDelta 起始點Y軸座標,同上
- android:toXDelta 結束點X軸座標,同上
- android:toYDelta 結束點Y軸座標,同上
2、構造函數
- TranslateAnimation(Context context, AttributeSet attrs) 從本地xml文件中加載動畫,基本不用
- RotateAnimation(float fromDegrees, float toDegrees)
- TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
- TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
由於fromXDelta,toXDelta,fromYDelta,toYDelta這四個屬性都有三種狀態,上述構造函數中,只有第三個能指定每個值得類型,可以指定百分數和相對於父控件的百分數值,第二個構造函數使用的是絕對數值。
3、代碼使用
TranslateAnimation translateAnimation = new TranslateAnimation(0, 100, 0, 100);
translateAnimation.setDuration(2000);
//設置重複次數,INFINITE表示無限重複
translateAnimation.setRepeatCount(Animation.INFINITE);
//設置重複類型,每次重複都重新開始動畫
translateAnimation.setRepeatMode(Animation.RESTART);
mTextView.startAnimation(translateAnimation);
效果如下:
七、AnimationSet
1、自身屬性
AnimationSet類對用Set便籤,定義動作類的合集,他自己是沒有xml屬性的,但是它具有Animation工具類的屬性,具體已經在上面的Animation公共類中做了講述,就不解釋了,這裏講述一下他的常用的幾個函數:
- addAnimation(Animation a) 增加動畫
- setStartOffset(long) 延時啓動動畫,時間單位爲毫秒
- cancel() 退出動畫 在動畫不需要的時候退出動畫
- reset() 釋放資源
- setAnimationListener(AnimationListener listener) 設置動畫監聽,它有三個函數回調,分別監聽動畫開始,結束,重複(下面會講到)
上面的函數中只有addAnimation(Animation a)是AnimationSet獨有的,cancel()、reset()、setAnimationListener(AnimationListener listener) 這些函數都是所有動畫效果都具有的屬性函數
2、構造函數
- AnimationSet(Context context, AttributeSet attrs) 基本不用
- AnimationSet(boolean shareInterpolator) shareInterpolator取值true和false,當爲true時,指在animationSet中指定一個插值器,它下面的動畫都使用這個插值器;如果設置爲false,則它下面的動畫使用自己定義的插值器。
3、代碼使用
ScaleAnimation scale = new ScaleAnimation(0.0f, 1.5f, 0.0f, 1.5f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scale.setRepeatMode(Animation.REVERSE);
scale.setRepeatCount(Animation.INFINITE);
AlphaAnimation alpha = new AlphaAnimation(0.2f, 0.8f);
alpha.setRepeatCount(Animation.INFINITE);
alpha.setRepeatMode(Animation.REVERSE);
RotateAnimation rotate = new RotateAnimation(0f,
360f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
TranslateAnimation translate = new TranslateAnimation(Animation.ABSOLUTE,
0,
Animation.ABSOLUTE, 300, Animation.ABSOLUTE, 0,
Animation.ABSOLUTE, 300);
translate.setRepeatCount(Animation.INFINITE);
translate.setRepeatMode(Animation.REVERSE);
//構建動畫合集
AnimationSet animationSet = new AnimationSet(true);
//設置動畫合集重複的次數,在AnimationSet設置是無效的
//animationSet.setRepeatCount(6);
//設置動畫合集重複類型,優先使用AnimationSet設置的,如果AnimationSet沒有設置
則使用子動畫中的
//animationSet.setRepeatMode(Animation.RESTART);
//添加動畫
animationSet.addAnimation(scale);
animationSet.addAnimation(alpha);
animationSet.addAnimation(rotate);
animationSet.addAnimation(translate);
//設置動畫合集時間,如果animationSet中有設置則使用animationSet中設置的時長,否
則使用子動畫中的時長
animationSet.setDuration(1000);
//設置動畫合集結束時保持動畫最後的狀態,只animationSet中設置有效,如果
animationSet中沒有設置,則使用默認的setFillBefore(true)
animationSet.setFillAfter(true);
//設置動畫集合監聽
animationSet.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Log.e(TAG, "Set:onAnimationStart");
}
@Override
public void onAnimationEnd(Animation animation) {
Log.e(TAG, "Set:onAnimationEnd");
}
@Override
public void onAnimationRepeat(Animation animation) {
Log.e(TAG, "Set:onAnimationRepeat");
}
});
//退出動畫
//animationSet.cancel();
//animationSet.reset();
mTextView.startAnimation(animationSet);
如圖:
注意:如果將部分動畫添加到AnimationSet中
- 在AnimationSet中設置setRepeatCount()是無效的,只能在子動畫中設置重複次數纔有效果。
- 對於setDuration()和setRepeatMode()則優先使用AnimationSet中設置的屬性,如果AnimationSet中沒有設置就使用子控件中的(AnimationSet沒有設置動畫時長,子動畫一定要設置時長)。
- setFillAfter()或者setFillBefore()屬性只能在AnimationSet中設置,如果沒有設置則使用默認的setFillBefore(true),在子動畫中設置是無效的。
具體效果已經在《第一篇動畫》中講解過,這裏就不一一講述了!
八、監聽器
animation.setAnimationListener設置動畫監聽,這個是Animation基類的屬性,所有子類動畫都有這個監聽
/**
* 監聽動畫變化時三個狀態
*/
public static interface AnimationListener {
void onAnimationStart(Animation animation);
void onAnimationEnd(Animation animation);
void onAnimationRepeat(Animation animation);
}
在AnimationListener中主要是監聽三個狀態,start、end、repeat;動畫開始會調用onAnimationStart(Animation animation)方法,動畫結束會調用onAnimationEnd(Animation animation)方法,動畫重複執行會調用onAnimationRepeat(Animation animation)方法。
九、插值器
Interpolator是Animation類的一個xml屬性,所以scale、rotate、alpha、translate、set都會繼承得到這個屬性。
Interpolator的系統值有下面幾個:
Interpolator class | Resource ID | |
AccelerateDecelerateInterpolator | @android:anim/accelerate_decelerate_interpolator | 在動畫開始和結束的時候比較慢,中間比較快 |
AccelerateInterpolator | @android:anim/accelerate_interpolator | 動畫開始時比較慢,然後加速 |
AnticipateInterpolator | @android:anim/anticipate_interpolator | 開始的時候向後然後向前甩 |
AnticipateOvershootInterpolator | @android:anim/anticipate_overshoot_interpolator | 開始的時候向後然後向前甩一定值後返回最後的值 |
BounceInterpolator | @android:anim/bounce_interpolator | 動畫結束的時候彈起 |
CycleInterpolator | @android:anim/cycle_interpolator | 動畫循環播放特定的次數,速率改變沿着正弦曲線 |
DecelerateInterpolator | @android:anim/decelerate_interpolator | 在動畫開始的地方快然後慢 |
LinearInterpolator | @android:anim/linear_interpolator | 以常量速率改變 |
OvershootInterpolator | @android:anim/overshoot_interpolator | 向前甩一定值後再回到原來位置 |
代碼設置插值器:
TranslateAnimation translateAnim = new TranslateAnimation(0, 300, 0, 300);
translateAnim.setDuration(1000);
//設置插值器
translateAnim.setInterpolator(new BounceInterpolator());
mTextView.startAnimation(translateAnim);
這裏僅僅演示位移時的插值器效果,其他的就不一一演示了,效果如下:
至此,本文結束!
源碼下載地址:https://github.com/FollowExcellence/AndroidAnimation
請大家尊重原創者版權,轉載請標明出處:https://blog.csdn.net/m0_37796683/article/details/90376533 謝謝!
動畫系列文章:
1、 Android動畫篇(一)—— alpha、scale、translate、rotate、set的xml屬性及用法
- 補間動畫的XML用法以及屬性詳解
2、Android動畫篇(二)—— 代碼實現alpha、scale、translate、rotate、set及插值器動畫
- 代碼動態實現補間動畫以及屬性詳解
3、 Android動畫篇(三)—— 屬性動畫ValueAnimator的使用
- ValueAnimator的基本使用
4、 Android動畫篇(四)—— 屬性動畫ValueAnimator的高級進階
- 插值器(Interpolator)、計算器(Evaluator)、ValueAnimator的ofObject用法等相關知識
5、 Android動畫篇(五)—— 屬性動畫ObjectAnimator基本使用
- ObjectAnomator的基本使用以及屬性詳解
6、 Android動畫篇(六)—— 組合動畫AnimatorSet和PropertyValuesHolder的使用
- AnimatorSet動畫集合和PropertyValuesHolder的使用
以上幾篇動畫文章是一定要掌握的,寫的不好請多多指出!