只是簡單記錄一下,動畫的實(shí)現(xiàn)很簡單,步驟不多,在Android 3.0版本之前,我們使用的是逐幀動畫(frame-by-frame animation)和補(bǔ)間動畫(tweened animation),不過這兩種方式靈活性很差,只能移動view,而且移動后只是繪制的位置變了,實(shí)際所在的位置仍然在原來的地方,所以你點(diǎn)擊移動后的view是沒效果的,因?yàn)閷?shí)際上這個(gè)view還在移動前的位置?,F(xiàn)在我們用的都是屬性動畫(Property Animator)了。屬性動畫用到的就這幾個(gè):
- ValueAnimator
- ObjectAnimator
- Interpolator
- TypeEvaluator
- AnimatorSet
- ViewPropertyAnimator
下面一個(gè)個(gè)的記錄一下:
1. ValueAnimator
ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
anim.setDuration(300);
anim.start();
用法很簡單,就是ofFloat、ofInt或者ofObject,其中ofObject方法的第一個(gè)參數(shù)是一個(gè)TypeEvaluator的子類,使用方法見第4條
2. ObjectAnimator
該類繼承自ValueAnimator,多了一種方法比如
float curTranslationX = textview.getTranslationX();
ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "translationX", curTranslationX, -500f, curTranslationX);
animator.setDuration(5000);
animator.start();
第一個(gè)參數(shù)是要變化的view,第二個(gè)參數(shù)是要變化的屬性,如果該屬性這view沒有,有一個(gè)辦法是可以繼承該view然后寫一個(gè)setXXX,然后方法內(nèi)部去操作XXX屬性,如上面的translationX,系統(tǒng)會去調(diào)用這個(gè)view的setTranslationX方法
3. Interpolator
這個(gè)控制動畫播放時(shí)的播放速度,Interpolator并不是屬性動畫中新增的技術(shù),實(shí)際上從Android 1.0版本開始就一直存在Interpolator接口了,而之前的補(bǔ)間動畫當(dāng)然也是支持這個(gè)功能的。只不過在屬性動畫中新增了一個(gè)TimeInterpolator接口,這個(gè)接口是用于兼容之前的Interpolator的,這使得所有過去的Interpolator實(shí)現(xiàn)類都可以直接拿過來放到屬性動畫當(dāng)中使用。TimeInterpolator有很多系統(tǒng)自帶的實(shí)現(xiàn)類,如下

比如先減速在加速之類的效果,有系統(tǒng)自帶的實(shí)現(xiàn)了,如果有其他復(fù)雜的,就自己繼承Interpolator重寫就好了
Interpolator是控制動畫速度的,TypeEvaluator是使用ofObject方法時(shí)獲取該速度下的值的
4. TypeEvaluator
//調(diào)用方式
Point point1 = new Point(0, 0);
Point point2 = new Point(300, 300);
ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), point1, point2);
anim.setDuration(5000);
anim.start();
public class PointEvaluator implements TypeEvaluator<Point>{
@Override
public Object evaluate(float fraction, Point startValue, Point endValue) {
Point startPoint = startValue;
Point endPoint = endValue;
float x = startPoint.getX() + fraction * (endPoint.getX() - startPoint.getX());
float y = startPoint.getY() + fraction * (endPoint.getY() - startPoint.getY());
Point point = new Point(x, y);
return point;
}
因?yàn)槿绻荗bject的話系統(tǒng)肯定是不知道如何更新動畫進(jìn)度的,所以得自己去實(shí)現(xiàn)如何更新,所以要傳入自己的TypeEvaluator
5. AnimatorSet
該類可以實(shí)現(xiàn)組合動畫,可以讓多個(gè)動畫一起執(zhí)行,或者A在B前面,等等
ObjectAnimator anim1 = ...;
ObjectAnimator anim2 = ...;
AnimatorSet animSet = new AnimatorSet();
animSet.play(anim).with(anim2);
animSet.setDuration(5000);
animSet.start();
6. ViewPropertyAnimator
ViewPropertyAnimator其實(shí)算不上什么高級技巧,它的用法格外的簡單,只不過和前面所學(xué)的所有屬性動畫的知識不同,它并不是在3.0系統(tǒng)當(dāng)中引入的,而是在3.1系統(tǒng)當(dāng)中附增的一個(gè)新的功能,因此這里我們把它作為整個(gè)屬性動畫系列的收尾部分。
那我們先來回顧一下之前的用法吧,比如我們想要讓一個(gè)TextView從常規(guī)狀態(tài)變成透明狀態(tài),就可以這樣寫:
ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "alpha", 0f);
animator.start();
那么下面我們就來看一下如何使用ViewPropertyAnimator來實(shí)現(xiàn)同樣的效果,ViewPropertyAnimator提供了更加易懂、更加面向?qū)ο蟮腁PI,如下所示(兩個(gè)例子不對應(yīng),就是舉個(gè)例子。。。):
textview.animate().x(500).y(500).setDuration(5000)
.setInterpolator(new BounceInterpolator());
在使用ViewPropertyAnimator時(shí),我們自始至終沒有調(diào)用過start()方法,這是因?yàn)樾碌慕涌谥惺褂昧穗[式啟動動畫的功能,只要我們將動畫定義完成之后,動畫就會自動啟動。
我這部分看的郭霖的博客
https://blog.csdn.net/guolin_blog/article/details/43536355
https://blog.csdn.net/guolin_blog/article/details/43816093
https://blog.csdn.net/guolin_blog/article/details/44171115
就是我總結(jié)了一下,方便回顧