I’m trying to do several translations simultaneously on Android.
I have 2 or more buttons on a layout (all the same size), and when I press one I want the others to move out of the screen.
I’ve done a test app to try to implement this behaviour.
On it I’ve set a listener on click of one button to test, something like:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Button toMove = (Button) findViewById(R.id.button_test2);
Button toMove2 = (Button) findViewById(R.id.button_test3);
AnimationSet set = new AnimationSet(true);
TranslateAnimation anim = new TranslateAnimation(0, -toMove
.getWidth(), 0, 0);
anim.setFillAfter(true);
anim.setDuration(1000);
toMove.setAnimation(anim);
toMove2.setAnimation(anim);
set.addAnimation(anim);
set.startNow();
}
The view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/button_test" android:layout_width="200px"
android:layout_height="50px" android:text="@string/hello" />
<Button android:id="@+id/button_test2" android:layout_width="200px"
android:layout_height="50px" android:text="@string/hello"/>
<Button android:id="@+id/button_test3" android:layout_width="200px"
android:layout_height="50px" android:text="@string/hello"/>
</LinearLayout>
The thing is that the two buttons start the animation, one slightly after the other. I’ve read that it is due to getDelayForView() that returns different delays of each. Is there any way to move 2 or more buttons simultaneously?
Google was not very helpful :-\
Issue:
It seems that
setAnimationwill acutally start the animation and probably asynchronously. However there might be a lock on setting the animation for the second view. There must be a dispatcher because setting the animation for the buttons in different order does not affect the fact that bottom one is faster.The solution is to prevent this hypothetical lock by creating two individual animations.
Code:
Note:
In the
//THERE IS ONE MORE TRICK, you could add the following code to ensure that they move together. There still must be a lag of 1 millisecond or so.