2012-07-06 36 views

Respuesta

4

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

No estoy seguro de cómo puede obtener su posición, button.getTop() y button.getLeft() podría funcionar ...

+0

No sé por qué, pero button.getTop() y button.getLeft() tanto da el valor 0. –

+0

Gracias @ D-32 TranslateAnimation funciona absolutamente bien botón se está moviendo. –

+0

¿Puedes poner tu xml con el botón en tu pregunta? –

3

No estoy seguro si esto le ayudará, pero me llamó la atención con el mismo problema que yo era capaz de hacer esto utilizando estos métodos, setTranslationX (float) setTranslationY (float)

se puede utilizar de esta manera

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

aquí está la documentación androide que proporcionan más información 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 
Cuestiones relacionadas