Question

I'm struggling with Android animation system. For testing\learning purpose, I've made a simple layout as follows:

<?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>

What I'm doing is translating the animation_objectbutton about it's width when I click on btn_animate button, like this:

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();

}

When I click the first time I get the following logs:

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

therefore, when I click btn_animate again the animation_object button "restarts" the animation from its starting position.

What I'd want is that the button is translated from its current position after each click of btn_animate, but I really don't know how to do that. Any suggestion?

Thank you Luca

Was it helpful?

Solution

I recommend using getX() instead of of getLeft() in your object animator, especially as you are updating the x property. Then update the other value each time with another variable like: offset+animatingObject.getWidth().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top