0
我正在努力與Android動畫系統。爲了測試\學習目的,我做了一個簡單的佈局如下: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>
我在做什麼是翻譯一下吧animation_object
按鈕的寬度當我點擊btn_animate
按鈕,如下:
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();
}
當我點擊第一個tim Ë我得到以下日誌:
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
後點擊它的當前位置,但我真的不知道該怎麼做。任何建議?
謝謝 盧卡