How to make the textview blinking

2019-01-10 09:33发布

Guys i have a textview which i need it to be blinking please help me with it.

<TextView 
       android:id="@+id/usage"
       android:layout_marginTop="220dip"
       android:layout_marginLeft="45dip"
       android:layout_marginRight="15dip"
       android:typeface="serif"            
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Google "
       android:textColor="#030900"/>

I want the google text to be blinking

9条回答
唯我独甜
2楼-- · 2019-01-10 10:10

Here is my helper implementation using an alpha animation:

    public void blinkText(final TextView text_to_animate, int durationMillis) {

        final AlphaAnimation fade_out = new AlphaAnimation(1.0f, 0.0f);
        //ScaleAnimation scale_it = new ScaleAnimation(1.0f, 1.25f, 1.0f, 1.25f);
        fade_out.setDuration(durationMillis);

        final AlphaAnimation fade_in = new AlphaAnimation(0.0f, 1.0f);
        //ScaleAnimation scale_it = new ScaleAnimation(1.0f, 1.25f, 1.0f, 1.25f);
        fade_in.setDuration(durationMillis);

        fade_out.setAnimationListener(new AnimationListener() {
            public void onAnimationEnd(Animation arg0) {
                // TODO Auto-generated method stub
            if (recording == true)
                text_to_animate.startAnimation(fade_in);
            }
            public void onAnimationRepeat(Animation arg0) {
                // TODO Auto-generated method stub              
            }

            public void onAnimationStart(Animation arg0) {
                // TODO Auto-generated method stub              
            }

        });

        fade_in.setAnimationListener(new AnimationListener() {
            public void onAnimationEnd(Animation arg0) {
                // TODO Auto-generated method stub
                if (recording == true)
                    text_to_animate.startAnimation(fade_out);
            }
            public void onAnimationRepeat(Animation arg0) {
                // TODO Auto-generated method stub              
            }

            public void onAnimationStart(Animation arg0) {
                // TODO Auto-generated method stub              
            }

        });

        text_to_animate.startAnimation(fade_out);       

    }
查看更多
\"骚年 ilove
3楼-- · 2019-01-10 10:11

You can use this:

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

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

It's the same answer I gave in this post Blinking Text in android view

Hope this helps!

查看更多
Animai°情兽
4楼-- · 2019-01-10 10:17
private fun blink() {
        val handler = Handler()
        Thread(Runnable {
            val timeToBlink = 500    //in milissegunds
            try {
                Thread.sleep(timeToBlink.toLong())
            } catch (e: Exception) {
            }

            handler.post(Runnable {

                if (usage.visibility == View.VISIBLE) {
                    usage.visibility = View.INVISIBLE
                } else {
                    usage.visibility = View.VISIBLE
                }
                blink()
            })
        }).start()
    }
查看更多
不美不萌又怎样
5楼-- · 2019-01-10 10:18

It is a deprecated answer to Android before 3.0 version, please uses SolArabehety's answer or look at this thread.

Original Answer

package teste.blink;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

public class TesteBlinkActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        blink();
}

private void blink(){
    final Handler handler = new Handler();
    new Thread(new Runnable() {
        @Override
        public void run() {
        int timeToBlink = 1000;    //in milissegunds
        try{Thread.sleep(timeToBlink);}catch (Exception e) {}
            handler.post(new Runnable() {
                @Override
                    public void run() {
                    TextView txt = (TextView) findViewById(R.id.usage);
                    if(txt.getVisibility() == View.VISIBLE){
                        txt.setVisibility(View.INVISIBLE);
                    }else{
                        txt.setVisibility(View.VISIBLE);
                    }
                    blink();
                }
                });
            }
        }).start();
    }

<TextView 
   android:id="@+id/usage"
   android:layout_marginTop="220dip"
   android:layout_marginLeft="45dip"
   android:layout_marginRight="15dip"
   android:typeface="serif"            
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Google "
   android:textColor="#030900"/>

查看更多
神经病院院长
6楼-- · 2019-01-10 10:18

Create an AlphaAnimation and apply it to the textview in the activity where you setup the textview. The blinking would be accomplished by repeating an animation from 1.0 alpha to 0.0 alpha to 1.0 alpha.


Edit: Google provideth.

查看更多
别忘想泡老子
7楼-- · 2019-01-10 10:19

You can make an animation or maybe why not making it View.VISIBLE and View.INVISIBLE with a timer? I think the better way is the animation with alpha indeed :)

查看更多
登录 后发表回答