Trying to Move a Button to Another Point on the Sc

2019-03-06 08:03发布

问题:

I am trying to move 3 buttons relative to their starting points:

The code for starting the animation is:

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

And the animation is defined as:

<?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>

All three animations follow the same format, with only the android:toDelta's being modified.

The issues is the animation runs, yet the buttons return to their original position at the end of the animation. I would like them to stay at the end point.

回答1:

showMore.setFillAfter(true); This will be placed in your java code. I hope it will be work.If you want to use the button after complete animation you will placed onAnimationEnd(Animation animation) in that place the button using your layout.



回答2:

You need two sets of buttons, and after the animation is played you have to make one set disappear, probably using View.setVisibility(View.GONE) on its parent or on each Button.



回答3:

Not sure if this will help you but i was struck with the same problem i was able to do this using these methods, setTranslationX(float) setTranslationY(float)

you can use it Like this

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

here's the android documentation that provide more information http://developer.android.com/reference/android/view/View.html#attr_android:translationX

also note that the minimum level of android SDK required is 11