How to make the textview blinking

前端 未结 11 2087
谎友^
谎友^ 2020-12-02 09:14

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



        
11条回答
  •  囚心锁ツ
    2020-12-02 09:28

    Edited

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

    The only reason I keep this answer is to historical reasons before android 3.0 Android animations had a lot of problems, this "bad" solution work at that time, nowadays it is unthinkable to use it, so just go for an animation solution, don't use this code.

    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();
        }
    

    
    

提交回复
热议问题