歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android移動view動畫問題

Android移動view動畫問題

日期:2017/3/1 9:51:29   编辑:Linux編程

Android寫動畫效果不是一般的麻煩,網上找了好久,終於解決了動畫的問題,總結記錄以共勉。

僅以水平方向移動效果做說明,垂直方向類似。

完整動畫函數代碼:

public void slideview(final float p1, final float p2) {
TranslateAnimation animation = new TranslateAnimation(p1, p2, 0, 0);
animation.setInterpolator(new OvershootInterpolator());
animation.setDuration(durationMillis);
animation.setStartOffset(delayMillis);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}

@Override
public void onAnimationRepeat(Animation animation) {
}

@Override
public void onAnimationEnd(Animation animation) {
int left = view.getLeft()+(int)(p2-p1);
int top = view.getTop();
int width = view.getWidth();
int height = view.getHeight();
view.clearAnimation();
view.layout(left, top, left+width, top+height);
}
});
view.startAnimation(animation);
}

調用示例:

移動到目標位置

slideview(0, distance);

從目標位置移回原位

slideview(0, -distance);

過程中遇到的問題:

1、動畫執行完成後,view回到原位

TranslateAnimation animation = new TranslateAnimation(p1, p2, 0, 0);
animation.setInterpolator(new OvershootInterpolator());
animation.setDuration(durationMillis);
animation.setStartOffset(delayMillis);
view.startAnimation(animation);

開始時動畫效果只寫了這麼多,發現動畫執行完,view會回到原位。

經過查資料嘗試使用animation.setFillAfter(true); view不再返回原位,但又出現了第2個問題

更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2013-11/92571p2.htm

Copyright © Linux教程網 All Rights Reserved