2012-07-06 33 views

回答

4

使用TranslateAnimation:

TranslateAnimation animation = new TranslateAnimation(start_x, start_y, end_x, end_y); 
animation.setDuration(1000); // duartion in ms 
animation.setFillAfter(false); 
button.startAnimation(animation); 

我不知道如何可以得到它的位置,button.getTop()和button.getLeft()可以工作...

+0

我不知道爲什麼,但button.getTop()和button.getLeft()都給出了值0 – 2012-07-06 19:31:12

+0

謝謝@ d-32 TranslateAnimation絕對精細的按鈕正在移動。 – 2012-07-06 19:32:23

+0

你可以把你的XML與按鈕到你的問題? – 2012-07-06 19:43:57

0
Solution for those who are looking for left to right animation) 

      TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 1500.0f); // new TranslateAnimation (float fromXDelta,float toXDelta, float fromYDelta, float toYDelta) 

       animation.setDuration(1500); // animation duration 
       animation.setRepeatCount(1); // animation repeat count 
      animation.setFillAfter(false); 
       your_view .startAnimation(animation);//your_view for mine is imageView  


Solution for those who are looking for repeated animation(for eg. left to right and right to left) 

      TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 1500.0f); // new TranslateAnimation (float fromXDelta,float toXDelta, float fromYDelta, float toYDelta) 
       animation.setDuration(1500); // animation duration 
       animation.setRepeatCount(4); // animation repeat count 
       animation.setRepeatMode(2); // repeat animation (left to right, right to left) 

       animation.setFillAfter(true); 
       your_view .startAnimation(animation);//your_view for mine is imageView