[英]Android Translation Animation
我正在努力使用Android動畫系統。 為了進行測試\\學習,我做了一個簡單的布局,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:background="#ffcccccc">
<Button
android:id="@+id/animation_object"
android:layout_width="44dp"
android:layout_height="44dp"
android:background="#ffff0000"
android:text="Click me!"
android:onClick="onClickAnimatingObject">
</Button>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2">
<Button
android:id="@+id/btn_animate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Animate!"
android:onClick="onClick"/>
</LinearLayout>
當我單擊btn_animate
按鈕時,我正在做的是翻譯animation_object
按鈕的寬度,如下所示:
public void onClick(View view) {
ObjectAnimator animator = ObjectAnimator.ofFloat(animatingObject, "x", animatingObject.getLeft(), animatingObject.getWidth()).setDuration(300);
animator.setRepeatMode(ObjectAnimator.INFINITE);
animator.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Log.v("ACN", "Start! l = "+animatingObject.getLeft()+" || r = "+animatingObject.getRight()+" || t = "+animatingObject.getTop()+" || b = " + animatingObject.getBottom());
}
@Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animator animation) {
Log.v("ACN", "End! l = "+animatingObject.getLeft()+" || r = "+animatingObject.getRight()+" || t = "+animatingObject.getTop()+" || b = " + animatingObject.getBottom());
}
@Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
});
animator.start();
}
當我第一次單擊時,將獲得以下日志:
04-29 17:33:03.249: V/ACN(22061): Start! l = 0 || r = 88 || t = 0 || b = 88
04-29 17:33:03.554: V/ACN(22061): End! l = 0 || r = 88 || t = 0 || b = 88
因此,當我再次單擊btn_animate
時, animation_object
按鈕將從其起始位置“重新啟動”動畫。
我想要的是在每次單擊btn_animate
之后從當前位置轉換按鈕,但是我真的不知道該怎么做。 有什么建議嗎?
謝謝盧卡
我建議在對象動畫設計器中使用getX()而不是getLeft(),尤其是在更新x屬性時。 然后每次使用另一個變量更新另一個值,例如:offset + animatingObject.getWidth()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.