屬性動畫核心API以及使用

屬性動畫的原理:初始化動畫值,然后把該值設(shè)置到屬性中,接著注冊垂直同步信號,最后進行View重繪。這個過程是一直循環(huán)進行的。動畫對值進行了修改,但是沒有涉及到重繪,是因為垂直同步信號,在安卓中每隔16ms發(fā)送一次,對View進行了重新繪制測量和布局。

API3.0之后提出的動畫模式,優(yōu)點如下

  • 不再局限于View對象,無對象也可以進行動畫處理。
  • 不再局限于4種基本變換:平移,旋轉(zhuǎn),縮放,透明度。
  • 可以靈活的操作任意對象屬性,根據(jù)自己業(yè)務(wù)來實現(xiàn)自己想要的結(jié)果。

核心API

  • ObjectAnimator 對象動畫
  • ValueAnimator 值動畫
  • PropertyValuesHolder 用于多個動畫同時執(zhí)行
  • TypeEvaluator 估值器
  • Interpolator 插值器
  • AnimatorSet 動畫集合

基礎(chǔ)使用

  • ObjectAnimator 操作對象屬性,不局限于對象,并且動畫可以一起執(zhí)行。
ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(v, "alpha", 1.0f, 0.3f,1.0f);
alphaAnim.setDuration(1000);//執(zhí)行時間
alphaAnim.setStartDelay(300);//延遲
alphaAnim.start();

“alpha”是透明度,除此之外,還有平移,縮放,旋轉(zhuǎn)。

  • AnimatorSet 動畫集合
    ObjectAnimator除了單個執(zhí)行外,還可以使用AnimatorSet一起執(zhí)行動畫。
ObjectAnimator animator1 = ObjectAnimator.ofFloat(iv, "translationX", 0f, 500F);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(iv, "alpha", 0f, 1f);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(iv, "scaleX", 0f, 2f);

AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(500);

//animatorSet.play(animator3).with(animator2).before(animator1);//鏈?zhǔn)秸{(diào)用順序
//animatorSet.play(animator3).with(animator2).after(animator1);//鏈?zhǔn)秸{(diào)用順序      
//animatorSet.playTogether(animator1,animator2,animator3);//一起執(zhí)行
animatorSet.playSequentially(animator2, animator1, animator3);//順序執(zhí)行
animatorSet.start();
  • ValueAnimator 值動畫
    上面的ObjectAnimator設(shè)置的屬性的名字都是系統(tǒng)要求固定的,還可以使用其它任意的名字來進行操作,需要配置ValueAnimator
ObjectAnimator animator = ObjectAnimator.ofFloat(v, "hello", 0f, 100f,50f);
animator.setDuration(300);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
     @Override
     public void onAnimationUpdate(ValueAnimator animation) {
           float value = (float) animation.getAnimatedValue();//百分比的所對應(yīng)的值
           Log.d("dn_alan", value + "");
           v.setScaleX(0.5f + value / 200);
           v.setScaleY(0.5f + value / 200);
     }
});
animator.start();
//animator.setRepeatCount(2);//重復(fù)次數(shù)
//animator.setRepeatCount(ValueAnimator.INFINITE);//無限次數(shù)
  • Interpolator 插值器

插值器是由API提供的一組算法,用來操作動畫執(zhí)行是的變換規(guī)則,省去了一些自己寫算法的麻煩,大致分為九種

ValueAnimator animator = new ValueAnimator();
animator.setDuration(3000);
animator.setObjectValues(new PointF(0, 0));
final PointF point = new PointF();
//估值
animator.setEvaluator(new TypeEvaluator() {
    @Override
    public Object evaluate(float fraction, Object startValue, Object endValue) {
        point.x = 100f * (fraction * 5);
        // y=vt=1/2*g*t*t(重力計算)
        point.y = 0.5f * 98f * (fraction * 5) * (fraction * 5);
        return point;
    }
});

animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        v.setX(point.x);
        v.setY(point.y);
    }
});


//加速插值器,公式: y=t^(2f) (加速度參數(shù). f越大,起始速度越慢,但是速度越來越快)
animator.setInterpolator(new AccelerateInterpolator(10));

//減速插值器公式: y=1-(1-t)^(2f) (描述: 加速度參數(shù). f越大,起始速度越快,但是速度越來越慢)
//animator.setInterpolator(new DecelerateInterpolator());

//先加速后減速插值器 y=cos((t+1)π)/2+0.5
//animator.setInterpolator(new AccelerateDecelerateInterpolator());

//張力值, 默認(rèn)為2,T越大,初始的偏移越大,而且速度越快 公式:y=(T+1)×t3–T×t2
//animator.setInterpolator(new AnticipateInterpolator());

//張力值tension,默認(rèn)為2,張力越大,起始和結(jié)束時的偏移越大,
//而且速度越快;額外張力值extraTension,默認(rèn)為1.5。公式中T的值為tension*extraTension
//animator.setInterpolator(new AnticipateOvershootInterpolator());

//彈跳插值器
//animator.setInterpolator(new BounceInterpolator());

//周期插值器 y=sin(2π×C×t)  周期值,默認(rèn)為1;2表示動畫會執(zhí)行兩次
//animator.setInterpolator(new CycleInterpolator(2));

//線性插值器,勻速公式:Y=T
//animator.setInterpolator(new LinearInterpolator());

//公式: y=(T+1)x(t1)3+T×(t1)2 +1
//描述: 張力值,默認(rèn)為2,T越大,結(jié)束時的偏移越大,而且速度越快
//animator.setInterpolator(new OvershootInterpolator());

animator.start();
  • PropertyValuesHolder 用于多個動畫同時執(zhí)行
PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("alpha", 1f, 0.5f);
PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("scaleX", 1f, 0.5f);
PropertyValuesHolder holder3 = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.5f);
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(v, holder1, holder2, holder3);
animator.setDuration(200);
animator.start();
  • TypeEvaluator 估值器的核心目的:自定義變換規(guī)則
ValueAnimator animator = new ValueAnimator();
animator.setDuration(3000);
animator.setObjectValues(new PointF(0, 0));
final PointF point = new PointF();
//估值
animator.setEvaluator(new TypeEvaluator() {
     @Override
     public Object evaluate(float fraction, Object startValue, Object endValue) {
        point.x = 100f * (fraction * 5);
        // y=vt=1/2*g*t*t(重力計算)
        point.y = 0.5f * 130f * (fraction * 5) * (fraction * 5);
            return point;
     }
});

animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
     @Override
     public void onAnimationUpdate(ValueAnimator animation) {
         PointF p = (PointF) animation.getAnimatedValue();
         Log.d("dn_alan", p.x + "===" + p.y);
         v.setX(p.x);
         v.setY(p.y);
     }
});
animator.start();

附有源碼兩篇文章:
屬性動畫源碼分析-ofFloat方法
屬性動畫源碼分析-start方法

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

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

  • 【Android 動畫】 動畫分類補間動畫(Tween動畫)幀動畫(Frame 動畫)屬性動畫(Property ...
    Rtia閱讀 6,437評論 1 38
  • 1 背景 不能只分析源碼呀,分析的同時也要整理歸納基礎(chǔ)知識,剛好有人微博私信讓全面說說Android的動畫,所以今...
    未聞椛洺閱讀 2,861評論 0 10
  • Animation Animation類是所有動畫(scale、alpha、translate、rotate)的基...
    四月一號閱讀 2,035評論 0 10
  • 本筆記的原文本鏈接 Property Animation Overview 屬性動畫總覽 The property...
    Jaesoon閱讀 1,233評論 2 3
  • 一: 傳統(tǒng) View 動畫(Tween/Frame) 1.1 Tween 動畫 主要有 4 中:縮放、平移、漸變、...
    dfg_fly閱讀 855評論 1 2

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