概述
補(bǔ)間動(dòng)畫(huà) pk 屬性動(dòng)畫(huà)
補(bǔ)間動(dòng)畫(huà)不足以覆蓋所有動(dòng)畫(huà)場(chǎng)景,在功能和擴(kuò)展方面存在缺陷;屬性動(dòng)畫(huà)則彌補(bǔ)了這些缺陷。
- 補(bǔ)間動(dòng)畫(huà)只對(duì)View或者繼承View的子View進(jìn)行操作;屬性動(dòng)畫(huà)也能對(duì)非View對(duì)象進(jìn)行操作(如自定義View中point對(duì)象,自定義View也就有了動(dòng)畫(huà)效果)。
- 補(bǔ)間動(dòng)畫(huà)只能夠?qū)崿F(xiàn)移動(dòng)、旋轉(zhuǎn)、縮放、透明度;屬性動(dòng)畫(huà)則不然。
- 補(bǔ)間動(dòng)畫(huà)只改變了View的顯示效果,不能改變View的屬性(如View的點(diǎn)擊事件還保留在原來(lái)位置);屬性動(dòng)畫(huà)則不然。
ValueAnimator
其實(shí)現(xiàn)原理是不斷的對(duì)值進(jìn)行操作,開(kāi)始值到結(jié)束值之間的過(guò)渡是通過(guò)ValueAnimator來(lái)計(jì)算的。以及負(fù)責(zé)播放次數(shù)、播放時(shí)長(zhǎng)、動(dòng)畫(huà)監(jiān)聽(tīng)等。
AnimatorUpdateListener 監(jiān)聽(tīng)數(shù)值的變化,從而完成動(dòng)畫(huà)的變換。
// ofFloat 值可以有多個(gè)
ValueAnimator animator = ValueAnimator.ofFloat(0.0f, 100.0f);
animator.setTarget(imageView);
animator.setDuration(1000);
// 可以在回調(diào)中不斷更新View的多個(gè)屬性,使用起來(lái)更加靈活
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
// 獲取當(dāng)前值
Float mValue = (Float) valueAnimator.getAnimatedValue();
// 設(shè)置橫向偏移量
imageView.setTranslationX(mValue);
// 設(shè)置縱向偏移量
imageView.setTranslationY(mValue);
}
});
animator.start();
ObjectAnimator
屬性動(dòng)畫(huà)最重要的一個(gè)類,繼承自 ValueAnimator。可以直接對(duì)任意對(duì)象的任意屬性進(jìn)行動(dòng)畫(huà)操作。
一些常用的可以直接使用的屬性動(dòng)畫(huà)的屬性值:
| 名稱 | 簡(jiǎn)述 |
|---|---|
| translationX、translationY | 沿著X軸或Y軸平移 |
| rotation、rotationX、rotationY | 圍繞View的支點(diǎn)進(jìn)行旋轉(zhuǎn) |
| PrivotX、PrivotY | 控制View的支點(diǎn)位置,圍繞這個(gè)支點(diǎn)進(jìn)行旋轉(zhuǎn)和縮放變換處理,默認(rèn)支點(diǎn)位置時(shí)View的中心 |
| alpha | 透明度,默認(rèn)是1(不透明),0是完全透明 |
| x、y | 描述View對(duì)象在其容器中的最終位置 |
AnimatorListener 監(jiān)聽(tīng)動(dòng)畫(huà)的各個(gè)時(shí)期階段。
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "alpha", 0.0f, 1.0f);
animator.setDuration(5000);
animator.setRepeatCount(2);
animator.setInterpolator(new LinearInterpolator());
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
// 動(dòng)畫(huà)開(kāi)始
}
@Override
public void onAnimationEnd(Animator animation) {
// 動(dòng)畫(huà)結(jié)束
}
@Override
public void onAnimationCancel(Animator animation) {
// 動(dòng)畫(huà)取消
}
@Override
public void onAnimationRepeat(Animator animation) {
// 動(dòng)畫(huà)重復(fù)
}
});
animator.start();
有時(shí)候我們只需要關(guān)心動(dòng)畫(huà)結(jié)束階段。提供有 AnimatorListenerAdapter 來(lái)讓我們選擇必要的階段監(jiān)聽(tīng)。
animator.addListener(new AnimatorListenerAdapter () {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation)
// 動(dòng)畫(huà)結(jié)束
}
});
AnimatorSet
- play(Animator anim) 執(zhí)行動(dòng)畫(huà)
- after(Animator anim) 將現(xiàn)有動(dòng)畫(huà)插入到傳入的動(dòng)畫(huà)之后執(zhí)行
- after(long delay) 將現(xiàn)有動(dòng)畫(huà)延遲指定毫秒后執(zhí)行
- before(Animator anim) 將現(xiàn)有動(dòng)畫(huà)插入到傳入的動(dòng)畫(huà)之前執(zhí)行
- with(Animator anim) 將現(xiàn)有動(dòng)畫(huà)和傳入的動(dòng)畫(huà)同時(shí)執(zhí)行