【问题标题】:Android translate animation for different buttons from single(same) pointAndroid从单个(相同)点翻译不同按钮的动画
【发布时间】:2016-12-02 02:00:23
【问题描述】:

我试图让我的按钮从图像中的一个点开始动画。但我总是从三个不同的点获取动画,因为有三个按钮。请看图片,图片中的第一张图片是我想要的,第二张图片是我得到的! 请帮忙!

我使用以下代码:

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

第二种方法!

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

第三种方法!

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

谢谢!

【问题讨论】:

    标签: android android-animation translate-animation


    【解决方案1】:

    这是我的布局文件,其中包含特定位置的 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>
    

    这是我在 MainActivity.java 中的 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);
        }
    

    重要的部分是将x坐标正确放入

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

    一旦我输入 -150,然后是 0,然后是 +150。

    【讨论】:

      【解决方案2】:

      将按钮放置在同一位置后,您就可以使用此方法。它正在使用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 了解所有可用方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多