当前位置: 动力学知识库 > 问答 > 编程问答 >

Android translate animation for different buttons from single(same) point

问题描述:

Im trying to make my buttons animate from a single point as in the image. But I always get the animation from three different points as there are three buttons. Please have a look at the picture, the first image in the picture is what i want and the second image is what i get!

Please help!

Im using the following 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);

Second approach!

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

Third approach!

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

Thanks!

网友答案:

Here is my Layout File which contains 3 buttons at a specific location:

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

Here is my java code in 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);
    }

Important Part is to put x coordinates right in

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

Once I put -150, then 0, and then +150.

网友答案:

Once you have placed your buttons at the same location you can use this method. It is using ViewPropertyAnimator and should fit your needs :

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

Check the docs for all available methods.

分享给朋友:
您可能感兴趣的文章:
随机阅读: