Android 動(dòng)畫(huà)之屬性動(dòng)畫(huà)(Property Animation)

概述

補(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í)行

比較詳細(xì)的鏈接

http://www.itdecent.cn/p/2412d00a0ce4

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容