導(dǎo)讀
- 移動(dòng)開發(fā)知識體系總章(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)畫的分類及介紹說到在Android1.0版本的時(shí)候就有了,Tween動(dòng)畫一般直接作用頁面中的 View 上山宾,實(shí)現(xiàn)基本的動(dòng)畫效果:平移至扰、旋轉(zhuǎn)、縮放塌碌、透明度渊胸、或前幾者的組合,基本效果如下:
由于Tween動(dòng)畫的特性被屬性動(dòng)畫完美替代接剩,故此切厘,這里就不過多的進(jìn)行展開,并且Tween動(dòng)畫作用于視圖整體懊缺,只需設(shè)定初始狀態(tài)(關(guān)鍵幀)和結(jié)束狀態(tài)(關(guān)鍵幀)疫稿,中間的狀態(tài)(變化過程)則由系統(tǒng)計(jì)算計(jì)算并補(bǔ)齊**,由此可見Tween動(dòng)畫的使用核心就是設(shè)置開始狀態(tài)和結(jié)束狀態(tài)鹃两。
對應(yīng)的代碼(效果對應(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)開始的角度和旋轉(zhuǎn)結(jié)束的角度
透明度動(dòng)畫的構(gòu)造函數(shù)中需要fromAlpha、toAlpha參數(shù)馋记,即開始動(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)開始時(shí)的坐標(biāo)茸习,和結(jié)束時(shí)的坐標(biāo)
縮放度動(dòng)畫的構(gòu)造函數(shù)中需要pivotXType畜隶、pivotXValue、pivotYType号胚、pivotYValue參數(shù)籽慢,即縮放開始時(shí)的坐標(biāo),和結(jié)束時(shí)的坐標(biāo)
果然應(yīng)對了前面說的View動(dòng)畫作用于視圖整體涕刚,只需設(shè)定初始狀態(tài)(關(guān)鍵幀)和結(jié)束狀態(tài)(關(guān)鍵幀)嗡综,中間的狀態(tài)(變化過程)則由系統(tǒng)計(jì)算計(jì)算并補(bǔ)齊**,由此可見View動(dòng)畫的使用核心就是設(shè)置開始狀態(tài)和結(jié)束狀態(tài)杜漠。
無論是設(shè)置坐標(biāo)系极景,還是設(shè)置透明度察净,都是設(shè)置了開始和結(jié)束的狀態(tài),中間變化的過程由系統(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);//開啟動(dòng)畫
以及動(dòng)畫監(jiān)聽
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ù)會在補(bǔ)上