概述
在Android開發(fā)中,我們常用動(dòng)畫的實(shí)現(xiàn)方式有幾種:
- 幀動(dòng)畫(Gif的實(shí)現(xiàn)就是把動(dòng)畫拆分成一張張單獨(dú)的圖片唯笙,逐幀播放)
- 補(bǔ)間動(dòng)畫(用xml實(shí)現(xiàn)的一系列的動(dòng)畫,包括淡入淡出萄窜、縮放诈泼、平移、旋轉(zhuǎn))
- 屬性動(dòng)畫(Android 3.0版本引入的全新動(dòng)畫實(shí)現(xiàn)捅儒,后面詳細(xì)說(shuō)明)
屬性動(dòng)畫關(guān)鍵概念
插值器(TimeInterpolator)
學(xué)術(shù)點(diǎn)的說(shuō)法就是根據(jù)時(shí)間流逝的百分比計(jì)算出當(dāng)前屬性值改變的百分比液样。是不是有點(diǎn)蒙振亮,我們通俗點(diǎn)說(shuō)吧,其實(shí)就是根據(jù)時(shí)間的流逝鞭莽,計(jì)算出一個(gè)系數(shù)坊秸,后面我們會(huì)根據(jù)這個(gè)系數(shù)去改變目標(biāo)屬性的值,實(shí)現(xiàn)我們的目的澎怒。再簡(jiǎn)單的說(shuō)就是控制動(dòng)畫快慢的東西褒搔,不知道這么說(shuō)會(huì)不會(huì)被拍磚,不管了喷面,也算是自己的理解吧星瘾。
系統(tǒng)中已經(jīng)實(shí)現(xiàn)了很多插值器,我們基本上是不需要自己去定義的惧辈,常用的有:
LinearInterpolator(勻速)
AccelerateInterpolator (加速)
DecelerateInterpolator(減速)
看到這里琳状,就是控制動(dòng)畫的快慢嘛,哈咬像!
估值器 (TypeEvaluator)
學(xué)術(shù)點(diǎn)的說(shuō)法就是根據(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í)例
先看下效果:
話不多說(shuō)创淡,直接上代碼痴晦,代碼中有相應(yīng)的注釋,這里就不多說(shuō)明了琳彩。
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為可邊長(zhǎng)的一半
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);
// 無(wú)限重復(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();
是不是很方便幢哨,可以直接改變控件的屬性赡勘,簡(jiǎn)單明了嫂便。
另外還有ViewPropertyAnimator捞镰,用起來(lái)更方便,不過只有有限的方法毙替,比如讓view在x軸y軸都平衡500:
textview.animate().x(500).y(500).setDuration(5000)
.setInterpolator(new BounceInterpolator());
最后最后岸售,再說(shuō)一個(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)起來(lái)袱院,但用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)畫來(lái)執(zhí)行的腻惠。
總結(jié)
其實(shí)屬性動(dòng)畫給了我們更大的自由度,接口也很友好欲虚,可以讓我們按自己的想法去實(shí)現(xiàn)更酷炫的動(dòng)畫集灌。快投入屬性動(dòng)畫的懷抱吧复哆,它真的可以帶你飛哦P佬!