Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android中设置按钮移动动画并设置新位置_Java_Android_Xml - Fatal编程技术网

Java 在Android中设置按钮移动动画并设置新位置

Java 在Android中设置按钮移动动画并设置新位置,java,android,xml,Java,Android,Xml,我有一个ImageButton,我想在按下时移动它,当动画结束时,我想让这个按钮停在新的位置 这是按钮代码: <ImageButton android:id="@+id/move_button" android:layout_width="120dp" android:layout_height="35dp" android:layout_centerInParent="true" android:layout_alignParentTop="tru

我有一个ImageButton,我想在按下时移动它,当动画结束时,我想让这个按钮停在新的位置

这是按钮代码:

<ImageButton
    android:id="@+id/move_button"
    android:layout_width="120dp"
    android:layout_height="35dp"
    android:layout_centerInParent="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="0dp"
    android:scaleType="fitCenter"
    android:background="@drawable/background_button"
    android:src="@drawable/move_button"
    android:onClick="MoveButton" />
当按下按钮时,它会启动动画,但当动画完成时,按钮会返回到初始位置,应用程序会崩溃


有什么问题吗?

这肯定是工作

Button im= (Button) findViewById(R.id.button);
//set position TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta
final Animation animation = new TranslateAnimation(0,100,0,0);
// set Animation for 5 sec
animation.setDuration(5000);
//for button stops in the new position.
animation.setFillAfter(true);
im.startAnimation(animation);

使用
anim.setFillAfter(true)
视图定位在动画结束的位置

还有一件事,您正在将
ImageButton
在Y坐标中从100设置为0,这就是为什么
ImageButton
位于初始位置,因为0是它的初始位置

在下面的代码中尝试我使用的
anim.setFillAfter(true)
animate
ImageButton
从Y坐标的0到100设置为动画

public void MoveButton(final View view) {
        TranslateAnimation anim = new TranslateAnimation(0,0,0,100);
        anim.setDuration(300);
        anim.setFillAfter(true);

        anim.setAnimationListener(new TranslateAnimation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }
        });

     view.startAnimation(anim);

 }

让我知道这是否对您有帮助。

使用
ObjectAnimator

        ObjectAnimator animation = ObjectAnimator.ofFloat(YOUR_VIEW, "translationX", 100f);
        animation.setDuration(2000);
        animation.start();
此代码将在2秒内向右移动
视图
100个pixles


如果您需要更多信息,请转到

,因为您正在将参数设置为“查看”,因此动画结束300毫秒后,它将变回初始位置,因此在末尾设置ImageButton参数对我不起作用。按钮被隐藏为给定的值time@Shubham您需要更改新TranslateImation(0100,0,0)的值;按照你的要求。
        ObjectAnimator animation = ObjectAnimator.ofFloat(YOUR_VIEW, "translationX", 100f);
        animation.setDuration(2000);
        animation.start();