简体   繁体   中英

how to move a buttonView from its original position to the new position in Android?

我在(0,0)位置有一个按钮,当我单击该按钮时,应将其移到(-,x)位置,例如ex(-0,-2)位置,或(x,-y)位置,例如ex( 0,-2)位置。如何实现此逻辑。

you can use translate animation for that

 Animation animation= AnimationUtils.loadAnimation( this, R.anim.animation);
 button.startAnimation( animation);

create amin folder in res and in amin folder create xml named as animation.xml

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

now you can play with translation

try this:

        float fromX=0;
        float toX=0;
        float fromY=40;
        float toY=40;
        TranslateAnimation animation = new TranslateAnimation(fromX, toX, fromY, toY);
        animation.setDuration(300);
        animation.setFillAfter(true);
        yourView.startAnimation(animation);

If u use Android 3.0+

ObjectAnimator animation = ObjectAnimator.ofFloat(yourButton, "x", newX);
animation.setDuration(animTime); //In milliseconds
animation.start();

if u dont use Android 3.0+ u cant use the library: http://nineoldandroids.com/ to backport this funcionality.

cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM