2012-02-15 58 views
36

夥計們我有一個textview,我需要它閃爍,請幫助我吧。如何使textview閃爍

<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

太晚,但在這裏(https://開頭github上。com/hanks-zyh/HTextView)很多很酷的動畫 – 2016-09-09 14:25:21

回答

21

這是一個棄用的Android之前的答案3.0版本,請使用SolArabehety的答案或看看this線程。

原來的答案

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

謝謝你會嘗試 – Goofy 2012-02-15 13:22:31

+0

對不起,現在嘗試缺少一件 – ademar111190 2012-02-15 13:28:30

+0

對不起,我沒有找到你 – Goofy 2012-02-15 13:34:03

8

創建AlphaAnimation並將其應用到TextView的在活動中,你設置TextView的。閃爍將通過重複從1.0 alpha到0.0 alpha到1.0 alpha的動畫來完成。


編輯:谷歌provideth

+0

可以請給我一個例子或鏈接在 – Goofy 2012-02-15 13:17:38

+1

添加鏈接到谷歌搜索的結果。 – 2012-02-15 13:20:23

+0

你也可以請電話如何強調textview。請給我一個例子 – Goofy 2012-02-15 13:24:11

0

你可以製作一個動畫或者爲什麼不使用計時器製作View.VISIBLE和View.INVISIBLE?我認爲更好的方法是與阿爾法確實的動畫:)

133

您可以使用此:

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

希望這有助於!

+1

我部分同意PrabhuM,但Gaurav的答案更優雅:在XML中定義動畫可以說是更好的MVC風格。 – ATutorMe 2015-11-11 08:15:52

-1

下面是一個使用我的助手實現的奧飛動漫:

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

    } 
+0

謝謝你將會看到... – Goofy 2012-10-18 04:46:55

14

使用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) { 

    } 

} 

讓我知道如果您有任何疑問..

+1

這應該是被接受的答案!感謝分享。 – Sam 2017-06-22 10:38:39

2

不需要設置此。只是阿爾法:

動畫/ 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));