2011-09-27 18 views
34

Actualmente uso una solución importante y tengo dos actividades cambiando cada vez que cambio el texto en una Vista de texto. Estoy usando este código:Animación al cambiar la vista de texto

Weeklytext.this.overridePendingTransition( 
        R.anim.slide_in_left, 
        R.anim.slide_out_right 
      ); 

¿Es posible hacer esto en una actividad? Es un poco molesto tener dos actividades con exactamente el mismo contenido solo para que pueda usar animaciones;)

¡Gracias! ¡Por favor, pregunta si no entiendes mi pregunta!

Respuesta

66

Puede usar un TextSwitcher para tener animaciones al cambiar el texto en un TextView.

Un TextSwitcher es simplemente un tipo especial de ViewSwitcher, y como tal, le permite proporcionar dos Vistas desde las cuales animar. Cuando llama a setText(), actualiza el texto del siguiente TextView y luego lo anima en la pantalla y sale el actual. El antiguo TextView se designa como el 'siguiente' TextView y el proceso se repite.

Puede especificar las Vistas usando setFactory(...) o simplemente agregarle dos TextViews con addView(...).

// get a TextSwitcher view; instantiate in code or resolve from a layout/XML 
TextSwitcher textSwitcher = new TextSwitcher(context); 

// specify the in/out animations you wish to use 
textSwitcher.setInAnimation(context, R.anim.slide_in_left); 
textSwitcher.setOutAnimation(context, R.anim.slide_out_right); 

// provide two TextViews for the TextSwitcher to use 
// you can apply styles to these Views before adding 
textSwitcher.addView(new TextView(context)); 
textSwitcher.addView(new TextView(context)); 

// you are now ready to use the TextSwitcher 
// it will animate between calls to setText 
textSwitcher.setText("hello"); 
... 
textSwitcher.setText("goodbye"); 
+0

¡Eso suena exactamente a lo que necesito! Solo necesito descubrir cómo usarlo. – Lorof

+1

@Lorenz He añadido un código para ayudarte :) – antonyt

+0

Buen ejemplo aquí: http://www.learn-android-easily.com/2013/06/android-textswitcher.html –

Cuestiones relacionadas