【问题标题】:Trying to Move a Button to Another Point on the Screen尝试将按钮移动到屏幕上的另一点
【发布时间】:2014-06-17 18:47:43
【问题描述】:

我正在尝试相对于它们的起点移动 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

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

【问题讨论】:

    标签: android android-animation


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 有没有更简单的方法可以将元素移动到屏幕上的不同点。
      【解决方案3】:

      不确定这是否会对您有所帮助,但我遇到了同样的问题,我可以使用这些方法做到这一点, setTranslationX(浮点数) setTranslationY(float)

      你可以这样使用它

      Button button = (button) findViewById(your id); 
      button.setTranslationX(a float value);
      

      这里是提供更多信息的 android 文档 http://developer.android.com/reference/android/view/View.html#attr_android:translationX

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

      【讨论】:

      • 当前版本的 Android 看起来不错,但在我问这个问题时,我使用的是 Android 2.x,很可能是 SDK 7 或 8 级。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多