目的
區(qū)分Animation(補(bǔ)間動(dòng)畫)和Animator(屬性動(dòng)畫)的區(qū)別,加強(qiáng)對(duì)屬性動(dòng)畫的學(xué)習(xí)
相關(guān)技術(shù)、及其使用
1、補(bǔ)間動(dòng)畫Animation
Animation
-TranslateAnimation
-AlphaAnimation
-ScaleAnimation
-RotateAnimation
缺點(diǎn):補(bǔ)間動(dòng)畫只是視覺(jué)效果改變,并沒(méi)有改變控件的屬性,所以盡量少用
2、屬性動(dòng)畫Animator
Animator
-ValueAnimator 屬性的值變了 視覺(jué)沒(méi)變
-ObjectAnimator 屬性、視覺(jué)都改變了
-TimeAnimator
屬性動(dòng)畫是真正的改變了屬性的值
例如:
1、系統(tǒng)屬性alpha 透明度 scalex橫向拉伸 scaley縱向拉伸
translationx 橫向移動(dòng) translationy 縱向移動(dòng)
rotation,rotationx rotationy 旋轉(zhuǎn)
2、自定義控件屬性 這個(gè)屬性必須實(shí)現(xiàn)set get方法
3、使用屬性動(dòng)畫改變控件透明度
//透明度alpha
public void test1(){
/*
1.target 需要?jiǎng)赢嫷目丶? 2、propertyName 控件的那個(gè)屬性
3、...values 變化的值 start - end
*/
ObjectAnimator alphaAni = ObjectAnimator.ofFloat(v,"alpha",1,0);
alphaAni.setDuration(1000);
alphaAni.start();
}
4、使用屬性動(dòng)畫使控件旋轉(zhuǎn)
//旋轉(zhuǎn)
public void test2(){
ObjectAnimator rotateAni = ObjectAnimator.ofFloat(v,"rotation",0,360);
rotateAni.setDuration(1000);
rotateAni.start();
}
5、使用屬性動(dòng)畫進(jìn)行縮放
public void test3(){
ObjectAnimator scaleAniX = ObjectAnimator.ofFloat(v,"scaleX",1,1.1f,1.1f,1);
scaleAniX.setDuration(1000);
scaleAniX.setRepeatCount(-1);
scaleAniX.setRepeatMode(ValueAnimator.RESTART);
ObjectAnimator scaleAniY = ObjectAnimator.ofFloat(v,"scaleY",1,1.1f,1.1f,1);
scaleAniY.setDuration(1000);
scaleAniY.setRepeatCount(-1);
scaleAniY.setRepeatMode(ValueAnimator.RESTART);
注意:屬性動(dòng)畫中,
with 同時(shí)執(zhí)行
after 后面執(zhí)行
before 前面執(zhí)行
playTogether 同時(shí)執(zhí)行
playSequence 順序執(zhí)行
與補(bǔ)間動(dòng)畫相同的是,同樣使用AnimatorSet方法給同一控件添加多個(gè)動(dòng)畫
AnimatorSet aSet = new AnimatorSet();
aSet.playTogether(scaleAniX,scaleAniY);
aSet.start();
PS
Animator(屬性動(dòng)畫)和Animation(補(bǔ)間動(dòng)畫)的主要區(qū)別在于前者是改變所需要?jiǎng)赢嬁丶膶傩灾担笳呔褪歉淖円曈X(jué)效果,并沒(méi)有改變控件的屬性值。通過(guò)這個(gè)項(xiàng)目對(duì)補(bǔ)間動(dòng)畫和屬性動(dòng)畫進(jìn)一步的區(qū)別和加深印象,能夠熟練的掌握動(dòng)畫的創(chuàng)建和實(shí)現(xiàn)。