如何使textview闪烁

Goo*_*ofy 52 android textview android-animation

伙计们,我有一个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"/>
Run Code Online (Sandbox Code Playgroud)

我希望谷歌文本闪烁

Sol*_*ety 181

你可以用这个:

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);
Run Code Online (Sandbox Code Playgroud)

这是我在这个帖子中给出的相同答案.在android视图中闪烁文本

希望这可以帮助!

  • 我部分同意 PrabhuM 的观点,但是 Gaurav 下面的回答更加优雅:在 XML 中定义动画可以说是更好的 MVC 风格。 (2认同)

Gau*_*ora 27

为此目的使用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>
Run Code Online (Sandbox Code Playgroud)

眨眼活动:像这样使用它: -

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

    }

}
Run Code Online (Sandbox Code Playgroud)

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

  • 这应该是公认的答案!感谢分享. (2认同)

ade*_*190 23

这是Android 3.0版本之前的弃用答案,请使用SolArabehety的答案或查看主题.

原始答案

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();
    }
Run Code Online (Sandbox Code Playgroud)

<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"/>
Run Code Online (Sandbox Code Playgroud)


Nic*_*ion 9

创建AlphaAnimation并将其应用于设置textview的活动中的textview.闪烁将通过重复从1.0 alpha到0.0 alpha到1.0 alpha的动画来完成.


编辑:Google提供.


Pra*_*ash 5

不需要为此设置。只是Alpha:

动画/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"/>
Run Code Online (Sandbox Code Playgroud)

并在代码中:

mTextView.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.flash_leave_now));
Run Code Online (Sandbox Code Playgroud)