繁体   English   中英

如何将buttonView从其原始位置移动到Android中的新位置?

[英]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)位置。如何实现此逻辑。

您可以为此使用翻译动画

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

res中创建一个amin文件夹,在amin文件夹中创建一个名为animation.xml的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> 

现在你可以玩翻译了

尝试这个:

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

如果您使用Android 3.0+

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

如果您不使用Android 3.0+,则不能使用该库: http ://nineoldandroids.com/反向移植此功能。

干杯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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