2016-07-27 1 views
1

私のボタンを画像のように1点からアニメーション化しようとしています。しかし、私は3つのボタンがあるので、常に3つの異なる点からアニメーションを取得します。画像を見てください、画像の最初の画像は私が望むもので、2番目の画像は私が得るものです! 助けてください!Androidは異なるボタンのアニメーションを単一の(同じ)ポイントから翻訳します

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

第二のアプローチを次のコードを使用して enter image description here

イム!

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

第3のアプローチ!

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

ありがとう!ここで

答えて

0

は、特定の場所にある3つのボタンが含まれ、私のレイアウトファイルである:ここで

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

を自分のJavaコードは、重要な部分は右でのx座標を置くことです

MainActivity.java

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); 
    } 
であります

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

一度、-150、次に0、そして+150を入力します。

0

ボタンを同じ場所に配置したら、この方法を使用できます。それはViewPropertyAnimatorを使用しており、ニーズに合わせなければならない:

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

は、すべての利用可能な方法のためdocsを確認してください。

関連する問題