Android从单个(相同)点翻译不同按钮的动画
我试图使我的按钮从一个点动画,如在图像中。但是我总是从三个不同的点得到动画,因为有三个按钮。请看图片,图片中的第一张图片是我想要的,第二张图片是我得到的! 请帮忙! Im使用以下代码:Android从单个(相同)点翻译不同按钮的动画,android,android-animation,translate-animation,Android,Android Animation,Translate Animation,我试图使我的按钮从一个点动画,如在图像中。但是我总是从三个不同的点得到动画,因为有三个按钮。请看图片,图片中的第一张图片是我想要的,第二张图片是我得到的! 请帮忙! Im使用以下代码: TranslateAnimation tanim1 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PAREN
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);
第三种方法
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);
谢谢 这是我的布局文件,在特定位置包含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>
重要的是把x坐标放在正确的位置
TranslateAnimation anim = new TranslateAnimation( 0, x , 0, y );
一旦我输入-150,然后是0,然后是+150。这是我的布局文件,在特定位置包含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>
重要的是把x坐标放在正确的位置
TranslateAnimation anim = new TranslateAnimation( 0, x , 0, y );
一旦我把-150,然后是0,然后是+150。一旦你把按钮放在同一个位置,你就可以使用这个方法。它正在使用并且应该适合您的需要:
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);
检查以查找所有可用的方法 将按钮放置在同一位置后,即可使用此方法。它正在使用并且应该适合您的需要:
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);
检查以查找所有可用的方法