[英]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.