介紹
原理:不斷更新View的屬性,讓它表現(xiàn)出動畫效果,這就是所謂的屬性動畫
包含兩種
1含末、ViewPropertyAnimator
2、ObjectAnimator
使用
共有:
設(shè)置時長
設(shè)置速度模型:Interpolater,內(nèi)插器(插值器)即舌,根據(jù)時間完成度去計算動畫完成度佣盒。默認AccelerateDecelerateInterpolator(先加速,后減速)
設(shè)置監(jiān)聽器
ViewPropertyAnimator使用方法
使用方式:View.animate()+相關(guān)系列方法
imageView.animate().translationX(500);//向右移動500像素顽聂。默認時長300ms沼撕,在View中會有對應(yīng)的setTranslationX()
同樣有setTranslationX()/setTranslationY()/setScaleX()/等等
ObjectAnimator使用方法
可自定義屬性宋雏,需要主動調(diào)用start
使用方式:
1、給自定義View添加setter/getter方法(屬性更新后需要注意自動重繪問題)
2务豺、ObjectAnimator.ofxxx()
3磨总、ObjectAnimator.start();
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView,"translateX",500);
objectAnimator.start();
ObjectAnimator.ofxxx創(chuàng)建ObjectAnimator對象。
參數(shù):目標對象笼沥,屬性名蚪燕,目標值(起始值-目標值,或者起始值--中間值-目標值)奔浅。
屬性名:可隨便寫馆纳,但是要保證目標對象里有setXXX的方法。比如translateX汹桦,保證目標對象里面有setTranslateX()方法鲁驶。因為最終是調(diào)用View.setTranslateX()更新屬性
估值器:TypeEvaluator,根據(jù)動畫完成度計算出具體屬性值
api有的:ArgbEvaluator舞骆,IntEvaluator等
自定義TypeEvaluator
1钥弯、實現(xiàn)TypeEvaluator接口
2、重寫evaludate方法督禽,實現(xiàn)自定義計算屬性值
3脆霎、animator.setEvaluator();
額外
PropertyValuesHolder:同一動畫中改變不同屬性值。進階:PropertyValuesHolder.ofKeyframe()把一個屬性拆分成多段狈惫,執(zhí)行更加精細的屬性動畫
ObjectAnimator.ofPropertyValuesHolder
AnimatorSet:多個動畫配合執(zhí)行
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether();
animatorSet.playSequentially();
animatorSet.play(animator1).with(animator2);
animatorSet.play(animator1).before(animator2);
animatorSet.play(animator1).after(animator2);
animatorSet.start();