Android 尝试将按钮移动到屏幕上的另一点

Android 尝试将按钮移动到屏幕上的另一点,android,android-animation,Android,Android Animation,我尝试相对于其起点移动3个按钮: 启动动画的代码是: protected void showMoreBtns() { Button btn1 = (Button)this.findViewById( R.id.more1btn ); Button btn2 = (Button)this.findViewById( R.id.more2btn ); Button btn3 = (Button)this.findViewById( R.id.more3btn );

我尝试相对于其起点移动3个按钮:

启动动画的代码是:

protected void showMoreBtns() {
    Button btn1 = (Button)this.findViewById( R.id.more1btn );
    Button btn2 = (Button)this.findViewById( R.id.more2btn );
    Button btn3 = (Button)this.findViewById( R.id.more3btn );

    Animation showMore = AnimationUtils.loadAnimation( this, R.anim.optionsinup1 );
    btn1.startAnimation( showMore );

    showMore = AnimationUtils.loadAnimation( this, R.anim.optionsinup2 );
    btn2.startAnimation( showMore );

    showMore = AnimationUtils.loadAnimation( this, R.anim.optioninup3 );
    btn3.startAnimation( showMore );
}
动画定义为:

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="-60"
        android:toYDelta="-30" />
</set>

所有三个动画都遵循相同的格式,只修改了
android:toDelta


问题在于动画运行,但按钮在动画结束时返回其原始位置。我希望它们停留在终点。

您需要两组按钮,在播放动画后,您必须使一组消失,可能需要在其父按钮或每个按钮上使用
View.setVisibility(View.GONE)

您需要两组按钮,在播放动画后,您必须使一组消失,可能在其父项或每个按钮上使用
View.setVisibility(View.GONE)

showMore.setFillAfter(true); 这将放在java代码中。 我希望它能起作用。如果你想在完成动画后使用该按钮,你将使用你的布局将该按钮放置在AnimationEnd(动画)上

showMore.setFillAfter(true); 这将放在java代码中。
我希望它能起作用。如果你想在完成动画后使用该按钮,你将使用你的布局将该按钮放置在AnimationEnd(动画)上

我不确定这是否对你有帮助,但我遇到了同样的问题,我可以用这些方法来解决这个问题, setTranslationX(浮动) 设置平移Y(浮动)

你可以这样使用它

Button button = (button) findViewById(your id); 
button.setTranslationX(a float value);
以下是提供更多信息的android文档


还请注意,所需的android SDK最低级别为11

不确定这是否会对您有所帮助,但我遇到了同样的问题,我可以使用这些方法来实现这一点, setTranslationX(浮动) 设置平移Y(浮动)

你可以这样使用它

Button button = (button) findViewById(your id); 
button.setTranslationX(a float value);
以下是提供更多信息的android文档


还要注意的是,android SDK的最低要求是11

是否有更简单的方法将元素移动到屏幕上的不同点。是否有更简单的方法将元素移动到屏幕上的不同点。对于当前版本的android来说,这看起来不错,但在我问这个问题时,我正在使用android 2.x,最有可能是SDK级别7或8。对于当前版本的Android来说,它看起来不错,但在我问这个问题时,我正在使用Android 2.x,最有可能是SDK级别7或8。