2010-11-16 23 views
7

Estoy tratando de hacer varias traducciones simultáneamente en Android.Traducciones simultáneas en Android

Tengo 2 o más botones en un diseño (todos del mismo tamaño), y cuando presiono uno quiero que los otros se salgan de la pantalla.

He hecho una aplicación de prueba para tratar de implementar este comportamiento.

Por lo que he establecido un oyente en el clic de un botón para poner a prueba, algo así como:

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

La vista:

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

El caso es que los dos botones de inicio de la animación , uno poco después del otro. He leído que se debe a getDelayForView() que devuelve diferentes retrasos de cada uno. ¿Hay alguna manera de mover 2 o más botones simultáneamente?

Google no era muy útil: - \

Respuesta

11

Edición:

Parece que setAnimation comenzará acutally la animación y, probablemente, de forma asíncrona. Sin embargo, puede haber un bloqueo en la configuración de la animación para la segunda vista. Debe haber un despachador porque establecer la animación para los botones en diferente orden no afecta el hecho de que el de abajo es más rápido.

La solución es evitar este bloqueo hipotético mediante la creación de dos animaciones individuales.

Código:

public void onClick(View view) { 
    Button toMove = (Button) findViewById(R.id.button_test2); 
    Button toMove2 = (Button) findViewById(R.id.button_test3); 

    TranslateAnimation anim = new TranslateAnimation(0, -toMove 
      .getWidth(), 0, 0); 
    anim.setFillAfter(true); 
    anim.setDuration(1000); 

    TranslateAnimation anim2 = new TranslateAnimation(0, -toMove 
      .getWidth(), 0, 0); 
    anim2.setFillAfter(true); 
    anim2.setDuration(1000); 

    //THERE IS ONE MORE TRICK 

    toMove.setAnimation(anim); 
    toMove2.setAnimation(anim2); 
} 

Nota:

En el //THERE IS ONE MORE TRICK, se podría añadir el siguiente código para garantizar que se mueven juntos. Todavía debe haber un desfase de 1 milisegundo o menos.

long time =AnimationUtils.currentAnimationTimeMillis(); 

//This invalidate is needed in new Android versions at least in order for the view to be refreshed. 
toMove.invalidate(); 
toMove2.invalidate(); 
anim.setStartTime(time); 
anim2.setStartTime(time); 
+0

this not work form me ??? –

Cuestiones relacionadas