2012-07-06 65 views

Trả lời

4

Sử dụng một 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); 

Tôi không chắc chắn làm thế nào bạn có thể nhận được vị trí của nó, button.getTop() và button.getLeft() có thể làm việc ...

+0

Tôi không biết tại sao nhưng button.getTop() và button.getLeft() cả cho giá trị 0. –

+0

Cảm ơn bạn @ D-32 TranslateAnimation hoạt động nút hoàn toàn tốt là di chuyển. –

+0

Bạn có thể đặt xml của mình bằng nút vào câu hỏi của bạn không? –

3

Không chắc chắn nếu điều này sẽ giúp bạn nhưng tôi bị ấn tượng với cùng một vấn đề tôi đã có thể làm điều này bằng phương pháp này, setTranslationX (float) setTranslationY (float)

bạn có thể sử dụng nó như thế này

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

đây là tài liệu hướng dẫn android cung cấp thêm thông tin http://developer.android.com/reference/android/view/View.html#attr_android:translationX

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 
Các vấn đề liên quan