如何让textview闪烁

发布于 2025-01-06 00:38:38 字数 446 浏览 2 评论 0原文

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

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

我希望谷歌文本闪烁

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

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

I want the google text to be blinking

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

書生途 2025-01-13 00:38:39

这是我使用 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);       

    }

Here is my helper implementation using an alpha animation:

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

    }
蝶…霜飞 2025-01-13 00:38:38

您可以使用这个:

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

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

╰◇生如夏花灿烂 2025-01-13 00:38:38

为此目的使用 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>

Blink Activity:像这样使用它:-

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

    }

}

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

Use XML Animations for this purpose :

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>

Blink Activity: use it like this :-

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

    }

}

Let me know if you have any queries..

霓裳挽歌倾城醉 2025-01-13 00:38:38

已编辑

这是对3.0 honeycomb版本之前的Android的已弃用答案,请使用SolArabehety的回答或查看此线程。

我保留这个答案的唯一原因是在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"/>

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

执笔绘流年 2025-01-13 00:38:38

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

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

Don't need set for this. Just alpha:

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

And in code:

mTextView.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.flash_leave_now));
云淡风轻 2025-01-13 00:38:38

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


编辑:Google 提供

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.

请持续率性 2025-01-13 00:38:38

只需使用 标签即可。

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

Just use the <blink/> tag.

<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>
仅一夜美梦 2025-01-13 00:38:38

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

 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 秒的闪烁效果。如果您不想要淡入淡出效果,建议使用。

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.

终难遇 2025-01-13 00:38:38
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()
    }
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()
    }
书信已泛黄 2025-01-13 00:38:38
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();

    }

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

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文