Android 動(dòng)畫(huà)
由于產(chǎn)品需求,需要呈現(xiàn)出一個(gè)listView中item的翻轉(zhuǎn)動(dòng)畫(huà)固阁。于是便開(kāi)始接觸實(shí)現(xiàn)動(dòng)畫(huà)效果。
先來(lái)說(shuō)說(shuō)想要實(shí)現(xiàn)的效果:
1.在item中,點(diǎn)擊按鈕進(jìn)行動(dòng)畫(huà)翻轉(zhuǎn)锐秦。
2.翻轉(zhuǎn)過(guò)程:指定view沿X軸進(jìn)行向上翻轉(zhuǎn)90°消失,指定view繼續(xù)向上翻轉(zhuǎn)90°顯示盗忱。(歸納為:一個(gè)view翻轉(zhuǎn)180°后顯示另外一個(gè)view)
實(shí)現(xiàn)方案:使用屬性動(dòng)畫(huà)酱床。
public static void FlipAnimatorXViewShow(final View oldView, final View newView, final long time) {
ObjectAnimator animator1 = ObjectAnimator.ofFloat(oldView, "rotationX", 0, 90);
final ObjectAnimator animator2 = ObjectAnimator.ofFloat(newView, "rotationX", -90, 0);
animator1.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
oldView.setVisibility(View.GONE);
animator2.setDuration(time).start();
newView.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
animator1.setDuration(time).start();
}
本以為自己寫(xiě)的沒(méi)有問(wèn)題(其實(shí)確實(shí)沒(méi)問(wèn)題),但是由于是用在listview中趟佃,每當(dāng)數(shù)據(jù)一刷新斤葱,需要顯示新的數(shù)據(jù),并且需要還原到oldView的內(nèi)容揖闸。于是揍堕,我便只在數(shù)據(jù)刷新重新繪制界面時(shí),讓oldView.setVisibility(View.VISIBLE)汤纸。卻在demo中的顯示效果并不理想衩茸,oldView中并不是顯示之前的內(nèi)容。
根本原因:原因在于oldView向上翻轉(zhuǎn)動(dòng)畫(huà)90°結(jié)束后贮泞,將oldView的RotationX屬性設(shè)置為了90楞慈,此時(shí)oldView的視圖僅僅只是條線段幔烛。所以,根本不會(huì)顯示之前的內(nèi)容數(shù)據(jù)囊蓝。
解決方案:只需要在動(dòng)畫(huà)結(jié)束時(shí)饿悬,將oldView的RotationX屬性設(shè)置回0就可以了。
oldView.setRotationX(0);//這行代碼要求在api level在11以上
ViewCompat.setRotationX(oldView, 0);//因此想要兼容11一下的版本聚霜,使用v4包中的ViewCompat