2010-08-10 29 views
30

Cómo mostrar Texto parpadeante en android.Texto parpadeante en la vista de Android

Gracias a todos.

+0

Ha intentado cambiar el color del texto cada segundo, por ejemplo? porque no creo que puedas hacer eso con el marco. o tal vez puede usar una vista web con una etiqueta parpadeante ... – Sephy

+0

puede crear un hilo que alterne la visibilidad de textView entre View.VISIBLE y View.INVISIBLE –

+0

En realidad, no creo que puedas hacer una WebView como sugirió Sephy; Webkit no representa la etiqueta (al menos en Chrome). Debido a que el molesto parpadeo del texto a los usuarios es una manera realmente mala de llamar su atención, y no les agradarán por ello. –

Respuesta

11

Crea una animación de la vista para él. Puede hacer un fundido alfa del 100% al 0% en 0 segundos y viceversa en un ciclo. De esta forma, Android lo maneja de forma inteligente y no tiene que preocuparse por el uso de la CPU.

Más sobre animaciones aquí:
http://developer.android.com/reference/android/view/animation/package-summary.html

Tutorial:
http://developerlife.com/tutorials/?p=343

+0

¿Cuál es la mejor manera de mantener el texto parpadeando? –

26

En realidad no es una etiqueta de abrir y cerrar de huevos de Pascua para esto en ICS! :) En realidad, no recomiendo usarlo, ¡aunque me divirtió mucho encontrarlo en la fuente!

<blink xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="I'm blinking" 
     /> 
</blink> 
+0

No funcionó para mí: 'E/AndroidRuntime (19645): Causado por: android.view.InflateException: archivo XML binario línea n. ° 9: error al inflar la clase blink ' –

+0

No está documentado, lo encontré por ICS de buceo en la fuente. Parece que el cambio se introdujo el 17-5-2011. No estoy seguro de qué número de SDK también se mapea. –

+1

Ah, y si esto no fuera obvio, estoy bastante seguro de que la etiqueta "parpadear" es un huevo de Pascua. ¡No estoy sugiriendo seriamente que sea la solución correcta! :) –

5

Se puede hacer mediante la adición de un ViewFlipper que alterna dos TextViews y animación fadeIn y Fadeout se puede aplicar cuando cambian.

Archivo de diseño: Código

<ViewFlipper android:id="@+id/flipper" android:layout_width="fill_parent" android:layout_height="wrap_content" android:flipInterval="1000" > 

<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="TEXT THAT WILL BLINK"/> 

<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="" /> 

</ViewFlipper> 

Actividad:

private ViewFlipper mFlipper; 
mFlipper = ((ViewFlipper)findViewById(R.id.flipper)); 
mFlipper.startFlipping(); 
mFlipper.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_in)); 
mFlipper.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_out)); 
96

Se puede utilizar esta:

TextView myText = (TextView) findViewById(R.id.myText); 

Animation anim = new AlphaAnimation(0.0f, 1.0f); 
anim.setDuration(50); //You can manage the time of the blink with this parameter 
anim.setStartOffset(20); 
anim.setRepeatMode(Animation.REVERSE); 
anim.setRepeatCount(Animation.INFINITE); 
myText.startAnimation(anim); 

Espero que esto ayude!

+1

realmente simple y realmente útil –

+2

Copiar y pegar y ¡Bingo! ¡Gracias por compartir esto! –

5

Usar subprocesos en el código siempre desperdicia tiempo de CPU y disminuye el rendimiento de la aplicación. No deberías usar hilos todo el tiempo. Úselo si fuera necesario.

Uso Animaciones XML para este propósito:

R.anim.blink

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <alpha android:fromAlpha="0.0" 
     android:toAlpha="1.0" 
     android:interpolator="@android:anim/accelerate_interpolator" 
     android:duration="600" 
     android:repeatMode="reverse" 
     android:repeatCount="infinite"/> 
</set> 

Actividad de parpadeo: utilizar de esta manera: -

public class BlinkActivity extends Activity implements AnimationListener { 

    TextView txtMessage; 
    Button btnStart; 

    // Animation 
    Animation animBlink; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_blink); 

     txtMessage = (TextView) findViewById(R.id.txtMessage); 
     btnStart = (Button) findViewById(R.id.btnStart); 

     // load the animation 
     animBlink = AnimationUtils.loadAnimation(getApplicationContext(), 
       R.anim.blink); 

     // set animation listener 
     animBlink.setAnimationListener(this); 

     // button click event 
     btnStart.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       txtMessage.setVisibility(View.VISIBLE); 

       // start the animation 
       txtMessage.startAnimation(animBlink); 
      } 
     }); 

    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     // Take any action after completing the animation 

     // check for blink animation 
     if (animation == animBlink) { 
     } 

    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 

    } 

    @Override 
    public void onAnimationStart(Animation animation) { 

    } 

} 

Let Me saber si tiene alguna pregunta ..

+0

cómo puedo hacer que parpadee con 3 colores gracias de antemano! –

+0

@ We'reAllMadHere Le sugiero que use Value Animator para esta tarea, es muy fácil de usar. http://stackoverflow.com/questions/15582434/using-a-valueanimator-to-make-a-textview-blink-different-colors –

+0

Esta es una manera muy clara de hacerlo, pero anima el todo TextView, incluido su color de fondo si no es transparente. ¿Hay alguna manera de parpadear solo el 'text' dentro de' TextView' –

0
If you want to make text blink on canvas in bitmap image so you can use this code 
we have to create two methods 
So first, let's declare a blink duration and a holder for our last update time: 
private static final int BLINK_DURATION = 350; // 350 ms 
private long lastUpdateTime = 0; private long blinkStart=0; 

ahora crear método

public void render(Canvas canvas) { 
    Paint paint = new Paint(); 
    paint.setTextSize(40); 
    paint.setColor(Color.RED); 
    canvas.drawBitmap(back, width/2 - back.getWidth()/2, height/2 
      - back.getHeight()/2, null); 
    if (blink) 
     canvas.drawText(blinkText, width/2, height/2, paint); 
} 

Ahora cree método de actualización a parpadear

public void update() { 
if (System.currentTimeMillis() - lastUpdateTime >= BLINK_DURATION 
     && !blink) { 
    blink = true; 
    blinkStart = System.currentTimeMillis(); 
} 
if (System.currentTimeMillis() - blinkStart >= 150 && blink) { 
    blink = false; 
    lastUpdateTime = System.currentTimeMillis(); 
} 

} 

Ahora que funcionan bien

Cuestiones relacionadas