Android屬性動(dòng)畫實(shí)踐

概述

在Android開發(fā)中,我們常用動(dòng)畫的實(shí)現(xiàn)方式有幾種:

  1. 幀動(dòng)畫(Gif的實(shí)現(xiàn)就是把動(dòng)畫拆分成一張張單獨(dú)的圖片,逐幀播放)
  2. 補(bǔ)間動(dòng)畫(用xml實(shí)現(xiàn)的一系列的動(dòng)畫,包括淡入淡出、縮放、平移、旋轉(zhuǎn))
  3. 屬性動(dòng)畫(Android 3.0版本引入的全新動(dòng)畫實(shí)現(xiàn),后面詳細(xì)說明)

屬性動(dòng)畫關(guān)鍵概念

插值器(TimeInterpolator)

學(xué)術(shù)點(diǎn)的說法就是根據(jù)時(shí)間流逝的百分比計(jì)算出當(dāng)前屬性值改變的百分比。是不是有點(diǎn)蒙,我們通俗點(diǎn)說吧,其實(shí)就是根據(jù)時(shí)間的流逝,計(jì)算出一個(gè)系數(shù),后面我們會(huì)根據(jù)這個(gè)系數(shù)去改變目標(biāo)屬性的值,實(shí)現(xiàn)我們的目的。再簡單的說就是控制動(dòng)畫快慢的東西,不知道這么說會(huì)不會(huì)被拍磚,不管了,也算是自己的理解吧。

系統(tǒng)中已經(jīng)實(shí)現(xiàn)了很多插值器,我們基本上是不需要自己去定義的,常用的有:
LinearInterpolator(勻速)
AccelerateInterpolator (加速)
DecelerateInterpolator(減速)
看到這里,就是控制動(dòng)畫的快慢嘛,哈!

估值器 (TypeEvaluator)

學(xué)術(shù)點(diǎn)的說法就是根據(jù)剛才時(shí)間插值器得出百分比計(jì)算得到目的的屬性值。其實(shí)就是根據(jù)剛才TimeInterpolator得到的系數(shù)去改變當(dāng)前的屬性值,從而更新View。

系統(tǒng)也已經(jīng)實(shí)現(xiàn)了很多的估值器,很多時(shí)候也是不需要自己去定義的,只有當(dāng)應(yīng)用到自定義對(duì)象的時(shí)候,會(huì)去自定義估值器。
IntEvaluator:針對(duì)整型屬性
FloatEvaluator:針對(duì)浮點(diǎn)型屬性
ArgbEvaluator:針對(duì)Color屬性

使用估值器我們一般直接就使用ValueAnimator的ofFloat(float... values),ofInt(int... values),ofArgb(int... values),不需要像插值器一樣需要通過setInterpolator去設(shè)置。

實(shí)例

先看下效果:

animation.gif

話不多說,直接上代碼,代碼中有相應(yīng)的注釋,這里就不多說明了。

package com.example.anker.demo;

import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;

/**
 * Created by dylan.huang on 17/4/21.
 */

public class MyView extends View {

    Paint mPaint;
    int lineWidth = 10;
    int ratio = 50;
    int mColor = Color.BLUE;
    float rotate;

    public MyView(Context context) {
        super(context);
        init();
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLUE);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(lineWidth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 由于動(dòng)畫中有更改顏色,所以這里每次onDraw都要重新設(shè)置下畫筆顏色
        mPaint.setColor(mColor);
        // 旋轉(zhuǎn)動(dòng)畫,直接旋轉(zhuǎn)畫布即可
        canvas.rotate(rotate, getWidth()/2, getHeight()/2);
        // 畫正方形,ratio為可邊長的一半
        canvas.drawRect(getWidth()/2-ratio/2, getHeight()/2-ratio/2, getWidth()/2+ratio/2, getHeight()/2+ratio/2, mPaint);
        // 畫圓形,ratio為半徑
        canvas.drawCircle(getWidth()/2  , getHeight()/2-2*ratio, ratio, mPaint);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void start() {
        // 旋轉(zhuǎn)動(dòng)畫,通過改變r(jià)otate值實(shí)現(xiàn)
        ValueAnimator rotateAni = ValueAnimator.ofFloat(0, 360);
        // 無限重復(fù)
        rotateAni.setRepeatCount(Animation.INFINITE);
        // 設(shè)置監(jiān)聽,賦值給rotate
        rotateAni.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                rotate = (float) animation.getAnimatedValue();
                invalidate();
            }
        });

        // 放大動(dòng)畫,通過改變r(jià)atio實(shí)現(xiàn)
        ValueAnimator ratioAnimator = ValueAnimator.ofInt(50, 100);
        ratioAnimator.setInterpolator(new LinearInterpolator());
        ratioAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ratio = (int) animation.getAnimatedValue();
            }
        });
        ratioAnimator.setRepeatCount(Animation.INFINITE);
        // 設(shè)置重復(fù)的模式為原樣恢復(fù),即放大后再按原路縮小,這樣才不會(huì)出現(xiàn)跳動(dòng)
        ratioAnimator.setRepeatMode(ValueAnimator.REVERSE);

        // 顏色變化動(dòng)畫
        ValueAnimator colorAni = ValueAnimator.ofArgb(Color.BLUE, Color.GREEN);
        colorAni.setInterpolator(new LinearInterpolator());
        colorAni.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mColor = (int)animation.getAnimatedValue();
            }
        });
        colorAni.setRepeatCount(Animation.INFINITE);
        colorAni.setRepeatMode(ValueAnimator.REVERSE);

        // 多個(gè)動(dòng)畫同時(shí)運(yùn)行,通過AnimatorSet進(jìn)行組合管理
        AnimatorSet set = new AnimatorSet();
        set.setDuration(1000);
        set.play(ratioAnimator).with(colorAni).with(rotateAni);
        set.start();
    }
}

我在例子中用的都是ValueAnimator,其實(shí)還有其它相關(guān)類,比如ObjectAnimator改變透明度:

ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);  
animator.setDuration(5000);  
animator.start();  

是不是很方便,可以直接改變控件的屬性,簡單明了。

另外還有ViewPropertyAnimator,用起來更方便,不過只有有限的方法,比如讓view在x軸y軸都平衡500:


textview.animate().x(500).y(500).setDuration(5000)  
        .setInterpolator(new BounceInterpolator());  

最后最后,再說一個(gè)PropertyValuesHolder,它保存了動(dòng)畫過程中所需要操作的屬性和對(duì)應(yīng)的值,通常和Keyframe一起使用。像實(shí)現(xiàn)一個(gè)View抖動(dòng)動(dòng)畫時(shí),你用上面的需要寫很多重復(fù)的動(dòng)畫進(jìn)行串聯(lián)起來,但用Keyframe就可以很好的一次性把動(dòng)畫描述清楚。Keyframe其實(shí)就是動(dòng)畫的關(guān)鍵幀。舉個(gè)抖動(dòng)的實(shí)現(xiàn)例子:

Keyframe frame0 = Keyframe.ofFloat(0f, 0);
Keyframe frame1 = Keyframe.ofFloat(0.1f, -20f);
Keyframe frame2 = Keyframe.ofFloat(1, 0);
PropertyValuesHolder frameHolder = PropertyValuesHolder.ofKeyframe("rotation",frame0,frame1,frame2);
 Animator animator = ObjectAnimator.ofPropertyValuesHolder(mImage,frameHolder);
animator.setDuration(1000);
animator.start();

其實(shí)最后還是用到ObjectAnimator.ofPropertyValuesHolder,是通過屬性動(dòng)畫來執(zhí)行的。

總結(jié)

其實(shí)屬性動(dòng)畫給了我們更大的自由度,接口也很友好,可以讓我們按自己的想法去實(shí)現(xiàn)更酷炫的動(dòng)畫。快投入屬性動(dòng)畫的懷抱吧,它真的可以帶你飛哦!!

最后編輯于
?著作權(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),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 【Android 動(dòng)畫】 動(dòng)畫分類補(bǔ)間動(dòng)畫(Tween動(dòng)畫)幀動(dòng)畫(Frame 動(dòng)畫)屬性動(dòng)畫(Property ...
    Rtia閱讀 6,382評(píng)論 1 38
  • 一: 傳統(tǒng) View 動(dòng)畫(Tween/Frame) 1.1 Tween 動(dòng)畫 主要有 4 中:縮放、平移、漸變、...
    dfg_fly閱讀 851評(píng)論 1 2
  • 對(duì)于android手機(jī)上的動(dòng)畫實(shí)現(xiàn)主要有三種,一種是幀動(dòng)畫,一種是View動(dòng)畫,以及3.0以上提供的屬性動(dòng)畫,所有...
    查理吃西瓜閱讀 6,608評(píng)論 1 39
  • Animation Animation類是所有動(dòng)畫(scale、alpha、translate、rotate)的基...
    四月一號(hào)閱讀 2,029評(píng)論 0 10
  • 本筆記的原文本鏈接 Property Animation Overview 屬性動(dòng)畫總覽 The property...
    Jaesoon閱讀 1,228評(píng)論 2 3

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