2016-07-27 17 views
1

Ich versuche, meine Tasten animieren von einem einzigen Punkt wie im Bild. Aber ich bekomme die Animation immer von drei verschiedenen Punkten, da es drei Tasten gibt. Bitte schau dir das Bild an, das erste Bild im Bild ist was ich will und das zweite Bild ist was ich bekomme! Bitte helfen! enter image description hereAndroid übersetzen Animation für verschiedene Tasten aus einzelnen (gleichen) Punkt

Im folgenden Codes:

TranslateAnimation tanim1 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, 
      Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, -1f, 
      Animation.RELATIVE_TO_PARENT, 0); 
    tanim1.setDuration(700); 

Zweiter Ansatz!

TranslateAnimation tanim = new TranslateAnimation(230, btn1.getX(), -height, btn1.getY()); 
    TranslateAnimation tanim2 = new TranslateAnimation(230, btn2.getX(), -height, btn2.getY()); 
    TranslateAnimation tanim3 = new TranslateAnimation(230, btn3.getX(), -height, btn3.getY()); 

Dritter Ansatz!

AnimatorSet animations = new AnimatorSet(); 
Animator xAnim = ObjectAnimator.ofFloat(button, "translationX", finalXValue); 
xAnim.setDuration(3000); 
Animator yAnim = ObjectAnimator.ofFloat(button, "translationY", finalYValue); 
yAnim.setDuration(3000); 
//Play all the animations together 
animations.play(xAnim).with(yAnim); 

Vielen Dank!

Antwort

0

Hier ist meine Layout-Datei, die 3 Tasten an einer bestimmten Stelle enthält:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:id="@+id/rootLayout" 
    tools:context="com.example.imcoder001.stackoverflow.MainActivity"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button 1" 
     android:id="@+id/button1" 
     android:layout_centerHorizontal="true"/> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button 2" 
     android:id="@+id/button2" 
     android:layout_centerHorizontal="true"/> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button 3" 
     android:id="@+id/button3" 
     android:layout_centerHorizontal="true"/> 
</RelativeLayout> 

Hier in MainActivity.java

mein Java-Code ist
protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     int height = 20; 
     Button btn1 = (Button) findViewById(R.id.button1); 
     Button btn2 = (Button)findViewById(R.id.button2); 
     Button btn3 = (Button)findViewById(R.id.button3); 

     moveViewToScreenCenter(btn1, -150, 100); 
     moveViewToScreenCenter(btn2, 0, 100); 
     moveViewToScreenCenter(btn3, 150, 100); 
    } 
    private void moveViewToScreenCenter(View view, int x, int y) 
    { 
     RelativeLayout root = (RelativeLayout) findViewById(R.id.rootLayout); 
     DisplayMetrics dm = new DisplayMetrics(); 
     this.getWindowManager().getDefaultDisplay().getMetrics(dm); 

     int originalPos[] = new int[2]; 
     view.getLocationOnScreen(originalPos); 

     TranslateAnimation anim = new TranslateAnimation(0, x , 0, y); 
     anim.setDuration(3000); 
     anim.setFillAfter(true); 
     view.startAnimation(anim); 
    } 

wichtiger Teil ist x zu setzen Koordinaten direkt in

TranslateAnimation anim = new TranslateAnimation(0, x , 0, y); 

Sobald ich -150, dann 0, und dann +150.

0

Sobald Sie Ihre Schaltflächen an der gleichen Stelle platziert haben, können Sie diese Methode verwenden. Es ist mit ViewPropertyAnimator und sollte Ihren Bedürfnissen passen:

void animateBtns(Button btn, float byX, float byY) { 
    btn.animate().translationXBy(byX).translationYBy(byY).setDuration(3000); 
} 

animateBtns(btn1, 0, finalYValue); 
animateBtns(btn2, -btn1.getWidth() * 2 , finalYValue); 
animateBtns(btn3, btn1.getWidth() * 2, finalYValue); 

Überprüfen Sie die docs für alle verfügbaren Methoden.