【问题标题】:How to make the textview blinking如何使TextView闪烁
【发布时间】:2012-03-06 20:06:19
【问题描述】:

伙计们,我有一个文本视图,我需要它闪烁,请帮助我。

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

我希望 google 文本闪烁

【问题讨论】:

标签: android android-animation textview


【解决方案1】:

你可以用这个:

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

这与我在这篇帖子 Blinking Text in android view 中给出的答案相同

希望这会有所帮助!

【讨论】:

  • 我部分同意 PrabhuM,但是 Gaurav 下面的回答更优雅:用 XML 定义动画可以说是更好的 MVC 风格。
【解决方案2】:

为此目的使用 XML 动画:

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>

闪烁活动:像这样使用它:-

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(this,
                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) {

    }

}

如果您有任何疑问,请告诉我..

【讨论】:

  • 这应该是公认的答案!感谢分享。
【解决方案3】:

已编辑

这是对3.0蜂窝之前版本的Android的弃用答案,请使用SolArabehety's answer或查看this线程。

我保留这个答案的唯一原因是android 3.0之前的历史原因,Android动画有很多问题,这个“糟糕”的解决方案在当时有效,现在使用它是不可想象的,所以只需要一个动画解决方案,不要使用这个代码。

原答案

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

【讨论】:

  • 对不起,现在尝试缺少一件事
  • 注意文字颜色是深色的,所以我想底部是清晰的
  • @sany 还有其他方式,例如 AlphaAnimation,线程只是众多方式中的一种。
  • 如何不应该从主线程更新所有视图(TextView)?
  • 这是一个糟糕的答案。使用安卓动画!请参阅以下 SolArabehety 的答案
【解决方案4】:

不需要为此设置。只是阿尔法:

动画/flash_leave_now.xml

    <?xml version="1.0" encoding="utf-8"?>
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:duration="900"
       android:fromAlpha="1.0"
       android:repeatCount="infinite"
       android:repeatMode="reverse"
       android:toAlpha="0.2"/>

在代码中:

mTextView.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.flash_leave_now));

【讨论】:

    【解决方案5】:

    创建一个AlphaAnimation 并将其应用于设置文本视图的活动中的文本视图。闪烁将通过重复从 1.0 alpha 到 0.0 alpha 到 1.0 alpha 的动画来完成。


    编辑:谷歌provideth

    【讨论】:

    • 你能给我一个例子或链接吗
    • 您能否也请电话如何在文本视图下划线。请给我一个例子
    • @Goofy 这太荒谬了。使用谷歌。在将文本插入 textview 之前,您可能需要对文本应用 UnderlineSpan。
    • android:textStyle="bold" 这将使文本变为粗体,但我告诉你关于下划线文本
    【解决方案6】:

    只需使用&lt;blink/&gt; 标签。

    <blink
         android:layout_width="wrap_content"
         android:layout_height="wrap_content">
    
      <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"/>
    
    </blink>
    

    【讨论】:

    • 这个闪烁标签还存在吗?
    • Intellij IDE 报告 标签在 xml 中不受支持
    【解决方案7】:

    您可以制作动画,或者为什么不使用计时器制作 View.VISIBLE 和 View.INVISIBLE?我认为更好的方法确实是带有 alpha 的动画:)

    【讨论】:

      【解决方案8】:

      感谢最佳答案,这就是我所做的:

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

      然后在代码的某处:

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

      这将产生 5 秒的闪烁效果。如果您不想要 fadeIn-fadeOut 效果,推荐使用。

      【讨论】:

        【解决方案9】:
        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()
            }
        

        【讨论】:

          【解决方案10】:
          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();
          
              }
          
          }
          

          【讨论】:

            【解决方案11】:

            这是我使用 alpha 动画的助手实现:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-09-19
              • 1970-01-01
              • 1970-01-01
              • 2018-01-30
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多