導(dǎo)讀
- 移動(dòng)開(kāi)發(fā)知識(shí)體系總章(Java基礎(chǔ)、Android、Flutter)
- Android 動(dòng)畫的分類及介紹
- Android中的屬性動(dòng)畫(Property Animation)
- Android中的逐幀動(dòng)畫(Drawable Animation)
- Android中的基礎(chǔ)動(dòng)畫 屬性動(dòng)畫(Property Animation)
- Android中的視圖動(dòng)畫總結(jié)
Android中的視圖動(dòng)畫(View Animation)(View動(dòng)畫、補(bǔ)間動(dòng)畫)
在Android 動(dòng)畫的分類及介紹說(shuō)到在Android1.0版本的時(shí)候就有了,Tween動(dòng)畫一般直接作用頁(yè)面中的 View 上,實(shí)現(xiàn)基本的動(dòng)畫效果:平移、旋轉(zhuǎn)、縮放、透明度、或前幾者的組合,基本效果如下:

由于Tween動(dòng)畫的特性被屬性動(dòng)畫完美替代,故此,這里就不過(guò)多的進(jìn)行展開(kāi),并且Tween動(dòng)畫作用于視圖整體,只需設(shè)定初始狀態(tài)(關(guān)鍵幀)和結(jié)束狀態(tài)(關(guān)鍵幀),中間的狀態(tài)(變化過(guò)程)則由系統(tǒng)計(jì)算計(jì)算并補(bǔ)齊**,由此可見(jiàn)Tween動(dòng)畫的使用核心就是設(shè)置開(kāi)始狀態(tài)和結(jié)束狀態(tài)。
對(duì)應(yīng)的代碼(效果對(duì)應(yīng)上述GIF):
RotateAnimation(旋轉(zhuǎn)動(dòng)畫)
private void showRotateAnimation () {
tv2Title.setText("RotateAnimation 旋轉(zhuǎn)動(dòng)畫");
RotateAnimation rotate = new RotateAnimation(0f, 360f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
LinearInterpolator lin = new LinearInterpolator();
rotate.setInterpolator(lin);
rotate.setDuration(3000);
rotate.setRepeatCount(-1);
tv2Show.setAnimation(rotate);
}
TranslateAnimation(平移動(dòng)畫)
private void showTranslateAnimation () {
tv3Title.setText("TranslateAnimation 平移動(dòng)畫");
Animation translateAnimation = new TranslateAnimation(0, 500, 0, 0);
translateAnimation.setDuration(3000);
translateAnimation.setRepeatCount(-1);
tv3Show.startAnimation(translateAnimation);
}
ScaleAnimation(縮放動(dòng)畫)
private void showScaleAnimation() {
tv4Title.setText("ScaleAnimation 縮放動(dòng)畫");
Animation scaleAnimation = new ScaleAnimation(0, 2, 0, 2,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(3000);
scaleAnimation.setRepeatCount(-1);
tv4Show.startAnimation(scaleAnimation);
}
AlphaAnimation(透明度動(dòng)畫)
private void showAlphaAnimation () {
tv5Title.setText("AlphaAnimation 透明度動(dòng)畫");
Animation alphaAnimation = new AlphaAnimation(1, 0);
alphaAnimation.setDuration(3000);
alphaAnimation.setRepeatCount(-1);
tv5Show.startAnimation(alphaAnimation);
}
AnimationSet (動(dòng)畫集合)
private void showAnimationSet() {
tv7Title.setText("組合 動(dòng)畫");
AnimationSet setAnimation = new AnimationSet(true);
setAnimation.setRepeatMode(Animation.RESTART);
setAnimation.setRepeatCount(1);
Animation rotate = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(1000);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
Animation translate = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_PARENT, -0.5f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.5f,
TranslateAnimation.RELATIVE_TO_SELF, 0
, TranslateAnimation.RELATIVE_TO_SELF, 0);
translate.setDuration(10000);
Animation alpha = new AlphaAnimation(1, 0);
alpha.setDuration(3000);
alpha.setStartOffset(7000);
Animation scale1 = new ScaleAnimation(1, 0.5f, 1, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scale1.setDuration(1000);
scale1.setStartOffset(4000);
setAnimation.addAnimation(alpha);
setAnimation.addAnimation(rotate);
setAnimation.addAnimation(translate);
setAnimation.addAnimation(scale1);
tv7Show.startAnimation(setAnimation);
}
這里就不貼上展示效果了。
Android中的視圖動(dòng)畫總結(jié)
先看看,旋轉(zhuǎn)動(dòng)畫和透明度動(dòng)畫構(gòu)造函數(shù):
public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {... }
public AlphaAnimation(float fromAlpha, float toAlpha) {... }
旋轉(zhuǎn)動(dòng)畫的構(gòu)造函數(shù)中需要fromDegrees、toDegrees參數(shù),即旋轉(zhuǎn)開(kāi)始的角度和旋轉(zhuǎn)結(jié)束的角度
透明度動(dòng)畫的構(gòu)造函數(shù)中需要fromAlpha、toAlpha參數(shù),即開(kāi)始動(dòng)畫時(shí)的透明度和結(jié)束時(shí)的透明度
再看平移動(dòng)畫、縮放動(dòng)畫的構(gòu)造函數(shù):
public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {...}
public ScaleAnimation(float fromX, float toX, float fromY, float toY,
int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {...}
平移動(dòng)畫的構(gòu)造函數(shù)中需要fromXDelta、toXDelta、fromYDelta、toYDelta參數(shù),即移動(dòng)開(kāi)始時(shí)的坐標(biāo),和結(jié)束時(shí)的坐標(biāo)
縮放度動(dòng)畫的構(gòu)造函數(shù)中需要pivotXType、pivotXValue、pivotYType、pivotYValue參數(shù),即縮放開(kāi)始時(shí)的坐標(biāo),和結(jié)束時(shí)的坐標(biāo)
果然應(yīng)對(duì)了前面說(shuō)的View動(dòng)畫作用于視圖整體,只需設(shè)定初始狀態(tài)(關(guān)鍵幀)和結(jié)束狀態(tài)(關(guān)鍵幀),中間的狀態(tài)(變化過(guò)程)則由系統(tǒng)計(jì)算計(jì)算并補(bǔ)齊**,由此可見(jiàn)View動(dòng)畫的使用核心就是設(shè)置開(kāi)始狀態(tài)和結(jié)束狀態(tài)。無(wú)論是設(shè)置坐標(biāo)系,還是設(shè)置透明度,都是設(shè)置了開(kāi)始和結(jié)束的狀態(tài),中間變化的過(guò)程由系統(tǒng)補(bǔ)齊。
當(dāng)然,Tween動(dòng)畫還有很多方法,比如上面通用的:
xxxAnimation.setDuration(3000);////設(shè)置動(dòng)畫持續(xù)時(shí)間
xxxAnimation.setRepeatCount(-1);//設(shè)置重復(fù)次數(shù)
xxxView.startAnimation(xxxAnimation);//開(kāi)啟動(dòng)畫
以及動(dòng)畫監(jiān)聽(tīng)
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {}
@Override
public void onAnimationRepeat(Animation animation) { }
});
最后,Tween動(dòng)畫還有許許多多的方法,由于Tween動(dòng)畫可被屬性動(dòng)畫完美替代,就不再繼續(xù)深入。Tween動(dòng)畫的xml方式后續(xù)會(huì)在補(bǔ)上