¿Cómo animar el botón en android?

Estoy haciendo una aplicación para Android, y tengo un botón que conduce a un lugar de mensajería. En la actividad con el botón, comprobar si hay mensajes no leídos, y si es así quiero hacer algo al botón para que el usuario sepa que hay algo no leído.

Yo estaba pensando en tener el botón sorta vibrar horizontalmente como 3 batidos cada 2 o 3 segundos.

Sé cómo ejecutar un hilo en el fondo que hace algo cada milisegundos x. Pero lo que no sé qué hacer es sacudirlo horizontalmente 3 veces.

¿Alguien puede ayudarme con esto?

Yo estaba pensando en usar la función sin, para la animación, puedo utilizar la salida de una función de pecado para obtener valores que van hacia arriba y hacia abajo, que puedo establecer la posición horizontal del botón … Pero esto parece demasiado extremo, es Hay una mejor manera?

Gracias.

Crear shake.xml en la carpeta anim

<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="10" android:duration="1000" android:interpolator="@anim/cycle" /> 

Y cycle.xml en la carpeta anim

 <?xml version="1.0" encoding="utf-8"?> <cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="4" /> 

Ahora agrega animación en tu código

 Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake); anyview.startAnimation(shake); 

Si desea una animación vertical, cambie de valor de Xdelta y de Xdelta a valor de Ydelta y de Ydelta

No puedo comentar sobre el comentario de @ omega porque no tengo suficiente reputación pero la respuesta a esa pregunta debería ser algo como:

Shake.xml

 <?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="100" <!-- how long the animation lasts --> android:fromDegrees="-5" <!-- how far to swing left --> android:pivotX="50%" <!-- pivot from horizontal center --> android:pivotY="50%" <!-- pivot from vertical center --> android:repeatCount="10" <!-- how many times to swing back and forth --> android:repeatMode="reverse" <!-- to make the animation go the other way --> android:toDegrees="5" /> <!-- how far to swing right --> 

Class.java

 Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake); view.startAnimation(shake); 

Esta es sólo una manera de hacer lo que quieras, puede haber mejores métodos por ahí.

Echa un vistazo a este tutorial. Tiene formas de animaciones diferentes

http://android-er.blogspot.com/2012/02/apply-animation-on-button.html


Clase java

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_with_the_button); final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.milkshake); Button myButton = (Button) findViewById(R.id.new_game_btn); myButton.setAnimation(myAnim); } 

Para onClick del botón

 myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.startAnimation(myAnim); } }); 

Cree la carpeta anim en el directorio res

Haga clic derecho en, res -> Nuevo -> Directorio

Nombre del nuevo directorio anim

Crear un nuevo nombre de archivo xml milkshake


Milkshake.xml

 <?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="100" android:fromDegrees="-5" android:pivotX="50%" android:pivotY="50%" android:repeatCount="10" android:repeatMode="reverse" android:toDegrees="5" /> 

 import android.view.View; import android.view.animation.Animation; import android.view.animation.Transformation; public class HeightAnimation extends Animation { protected final int originalHeight; protected final View view; protected float perValue; public HeightAnimation(View view, int fromHeight, int toHeight) { this.view = view; this.originalHeight = fromHeight; this.perValue = (toHeight - fromHeight); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { view.getLayoutParams().height = (int) (originalHeight + perValue * interpolatedTime); view.requestLayout(); } @Override public boolean willChangeBounds() { return true; } } 

Uss a:

 HeightAnimation heightAnim = new HeightAnimation(view, view.getHeight(), viewPager.getHeight() - otherView.getHeight()); heightAnim.setDuration(1000); view.startAnimation(heightAnim); 
  • Android: Cambiar las acciones del botón Atrás
  • Botón Atrás en ActionBar en MainActivity
  • ¿Todos los dispositivos Android tienen botones "opción" y "atrás"?
  • No se puede encontrar el método onClick
  • El botón de creación de Android emite programaticamente
  • Escribir texto en la pista en el botón de conmutación en Android
  • Cambia dinámicamente el ancho de un botón en Android
  • El botón no se muestra en LinearLayout
  • ListView no responde a los eventos de clics en Android
  • Botón de acción flotante Ser un cuadrado
  • Botón bajó cuando le puse un largo texto
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.