2009-11-06 18 views
5

Sto provando a scambiare la posizione di due pulsanti. Il mio codice scambio appare è:Posizione di scambio animata di due pulsanti

private void exchangeButtons(Button btn1, Button btn2) { 
    // Create the animation set 
    AnimationSet exchangeAnimation = new AnimationSet(true); 
    TranslateAnimation translate = new TranslateAnimation(
     Animation.RELATIVE_TO_SELF, btn2.getLeft(), 
     Animation.RELATIVE_TO_SELF, btn1.getLeft(), 
     Animation.RELATIVE_TO_SELF, btn2.getRight(), 
     Animation.RELATIVE_TO_SELF, btn1.getRight()); 
    translate.setDuration(500); 
    exchangeAnimation.addAnimation(translate); 
    //int fromX = btn1.getLeft(); 
    //int fromY = btn1.getRight(); 
    //int toX = btn2.getLeft(); 
    //int toY = btn2.getRight(); 
    Log.d("ArrangeMe", 
     "view1 pos:" + btn1.getLeft() + ", 
     " +btn1.getRight() + "view2 pos:" + 
     btn2.getLeft() + ", " + btn2.getRight()); 
    AnimationSet exchangeAnimation1 = new AnimationSet(true); 
    TranslateAnimation translate1 = new TranslateAnimation( 
     Animation.RELATIVE_TO_SELF, btn1.getLeft(), 
     Animation.RELATIVE_TO_SELF, btn2.getLeft(), 
     Animation.RELATIVE_TO_SELF, btn1.getRight(), 
     Animation.RELATIVE_TO_SELF, btn2.getRight()); 
    translate1.setDuration(500); 
    exchangeAnimation1.addAnimation(translate1); 
    // EXECUTE btn1.startAnimation(exchangeAnimation); 
    btn2.startAnimation(exchangeAnimation1); 
} 

io chiamo il codice come di seguito:

exchangeButtons(button1, button2); 

mio layout appare come di seguito:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <Button 
     android:text="One" 
     android:id="@+id/button1" 
     android:layout_height="70px" 
     android:layout_width="70px" 
     android:layout_weight="1"> 
    </Button> 
    <Button 
     android:text="Two" 
     android:id="@+id/button2" 
     android:layout_height="70px" 
     android:layout_width="70px" 
     android:layout_weight="1"> 
    </Button> 
</LinearLayout> 

Cosa succede quando eseguo il codice è:

Invece dei pulsanti che scambiano le loro posizioni, spariscono per un po '[ potrebbe essere 500 ms] e riapparire come erano originariamente.

Come risolvere questo problema? Funzionerà correttamente nel dispositivo?

+0

avete risolto questo problema? –

risposta

1

L'Android TranslateAnimation non riposiziona i componenti, ma solo la transizione che si desidera eseguire, dopodiché spetta a te modificare i parametri di layout del componente (dai un'occhiata a questo post).

P.S. Puoi utilizzare direttamente l'animazione di traduzione, senza utilizzare un set di animazione completamente nuovo. Un'altra cosa buona sarà creare le animazioni quando il layout è gonfiato e ignorare onSizeChanged(int, int, int, int) per ricrearle usando le nuove dimensioni.

Qual è il layout in uso?

3

Non si ottiene il valore getRight in onCreate perché l'interfaccia utente non è stata disegnata sullo schermo, quindi nessuno degli elementi dell'interfaccia utente è stato ancora misurato. Prova questa

ViewTreeObserver vto = btn1.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     btn1.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
     int left = btn1.getLeft(); 
    } 
}); 

Mettere il codice nel metodo onCreate