How to make the textview blinking

前端 未结 11 2058
谎友^
谎友^ 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();
        }
    

    <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"/>
    

    0 讨论(0)
  • 2020-12-02 09:30
    public final class BlinkEffectUtils {
    
        private static BlinkEffectUtils blinkEffect;
    
        public enum PROPERTY_TYPE {
            BACKGROUND_COLOR,
            TEXT_COLOR
        }
    
        private BlinkEffectUtils() {
        }
    
        public static BlinkEffectUtils getInstance(Context context) {
            if (blinkEffect == null) {
                blinkEffect = new BlinkEffectUtils();
            }
    
            return blinkEffect;
        }
    
        public void setBlinkEffect(Object targetView, PROPERTY_TYPE property_type, int duration, int defaultColor, int effectColor) {
            String propertyName = "";
    
            switch (property_type) {
    
                case TEXT_COLOR:
                    propertyName = "textColor";
                    break;
    
                case BACKGROUND_COLOR:
                    propertyName = "backgroundColor";
                    break;
            }
    
            @SuppressLint("ObjectAnimatorBinding")
            ObjectAnimator anim = ObjectAnimator.ofInt(targetView, propertyName,
                    effectColor,
                    defaultColor);
            anim.setDuration(duration);
            anim.setEvaluator(new ArgbEvaluator());
            anim.setRepeatMode(ValueAnimator.REVERSE);
            anim.setRepeatCount(ValueAnimator.INFINITE);
            anim.start();
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-02 09:35

    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!

    0 讨论(0)
  • 2020-12-02 09:36
    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()
        }
    
    0 讨论(0)
  • 2020-12-02 09:37

    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.

    0 讨论(0)
  • 2020-12-02 09:39

    Courtesy to the top answer, this is what i did:

     textBlink = new TimerTask() {
            int countdown = 10;
    
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (countdown <= 0) {
                            timer.cancel();
                            textview.setVisibility(View.GONE);
                        } else {
                                textview.setVisibility(textview.getVisibility() == View.VISIBLE?View.GONE:View.VISIBLE);
                            countdown--;
                        }
                    }
                });
            }
        };
    

    then somewhere on the code:

      timer = new Timer();
      timer.scheduleAtFixedRate(textBlink,0,500);
    

    This will do a blink effect for 5 seconds. Recommended for if you don't want the fadeIn-fadeOut effect.

    0 讨论(0)
提交回复
热议问题