我必須通過更改其縮放比例,位置和滾動來將狀態A到視圖B的視圖動畫化。動畫Android中視圖的平移和縮放
下面的代碼幾乎是卓有成效:
AnimationSet animation = new AnimationSet(true);
int fromXDelta = view.getScrollX();
int fromYDelta = view.getScrollY();
view.scrollTo(0, 0);
float scale = (float) widthB/(float) widthA;
// Calculate toXDelta and toYDelta
TranslateAnimation translateAnimation = new TranslateAnimation(-fromXDelta, -toXDelta, -fromYDelta, -toYDelta);
translateAnimation.setDuration(duration);
animation.addAnimation(translateAnimation);
ScaleAnimation scaleAnimation = new ScaleAnimation(1, scale, 1, scale);
scaleAnimation.setDuration(duration);
animation.addAnimation(scaleAnimation);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation arg0) {
view.clearAnimation();
// Change view to state B by modifying its layout params and scroll
}
@Override public void onAnimationRepeat(Animation arg0) {}
@Override public void onAnimationStart(Animation arg0) {}
});
view.startAnimation(animation);
onAnimationEnd的邏輯使得視圖閃爍動畫結束之後。另外,如果我不調用clearAnimation(),則動畫無法按預期工作(爲什麼?)。
這是正確的方法嗎?
謝謝!