在android视图中闪烁文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3450839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:14:54  来源:igfitidea点击:

Blinking Text in android view

androidandroid-animationtextview

提问by David Prun

How do I display Blinking Textin android.

如何在android中显示闪烁文本

Thank you all.

谢谢你们。

回答by Bill Phillips

Actually there is an Easter egg blink tag for this in ICS! :) I don't actually recommend using it - was REALLY amused to find it in the source, though!

实际上在 ICS 中有一个复活节彩蛋闪烁标签!:) 我实际上并不推荐使用它 - 不过在源代码中找到它真的很有趣!

<blink xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I'm blinking"
        />
</blink>

回答by CodeFusionMobile

Create a view animation for it. You can do an alpha fade from 100% to 0% in 0 seconds and back again on a cycle. That way, Android handles it inteligently and you don't have to mess arround with threading and waste CPU.

为它创建一个视图动画。您可以在 0 秒内从 100% 到 0% 进行 alpha 淡入淡出,然后再循环一次。这样,Android 会以智能方式处理它,而您不必搞乱线程和浪费 CPU。

More on animations here:
http://developer.android.com/reference/android/view/animation/package-summary.html

更多关于动画的信息:http:
//developer.android.com/reference/android/view/animation/package-summary.html

Tutorial:
http://developerlife.com/tutorials/?p=343

教程:http: //developerlife.com/tutorials/?p=
343

回答by Gaurav Arora

Using Threads in your code always wastes CPU time and decreases performance of the application. You should not use threads all the time. Use if wherever neccessary.

在代码中使用线程总是会浪费 CPU 时间并降低应用程序的性能。你不应该一直使用线程。必要时使用。

Use XML Animations for this purpose :

为此目的使用 XML 动画:

R.anim.blink

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(getApplicationContext(),
                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..

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

回答by Anenth

It can be done by adding a ViewFlipper that alternates two TextViews and Fadein and Fadeout animation can be applied when they switch.

这可以通过添加一个 ViewFlipper 来完成,该 ViewFlipper 交替使用两个 TextView,并且在它们切换时可以应用淡入淡出动画。

Layout File:

布局文件:

<ViewFlipper android:id="@+id/flipper" android:layout_width="fill_parent" android:layout_height="wrap_content" android:flipInterval="1000" >

<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="TEXT THAT WILL BLINK"/>

<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="" />

</ViewFlipper>

Activity Code:

活动代码:

private ViewFlipper mFlipper;
mFlipper = ((ViewFlipper)findViewById(R.id.flipper));
mFlipper.startFlipping();
mFlipper.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_in));
mFlipper.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_out));

回答by Anenth

If you want to make text blink on canvas in bitmap image so you can use this code
we have to create two methods 
So first, let's declare a blink duration and a holder for our last update time: 
private static final  int BLINK_DURATION = 350; // 350 ms
private long lastUpdateTime = 0; private long blinkStart=0;

now create method

现在创建方法

    public void render(Canvas canvas) {
    Paint paint = new Paint();
    paint.setTextSize(40);
    paint.setColor(Color.RED);
    canvas.drawBitmap(back, width / 2 - back.getWidth() / 2, height / 2
            - back.getHeight() / 2, null);
    if (blink)
        canvas.drawText(blinkText, width / 2, height / 2, paint);
}

Now create update method to blink

现在创建更新方法来闪烁

public void update() {
if (System.currentTimeMillis() - lastUpdateTime >= BLINK_DURATION
        && !blink) {
    blink = true;
    blinkStart = System.currentTimeMillis();
}
if (System.currentTimeMillis() - blinkStart >= 150 && blink) {
    blink = false;
    lastUpdateTime = System.currentTimeMillis();
 }

}

Now it work fine

现在它工作正常

回答by droidfd droidfd

    private bool _blink; 
    private string _initialtext;
    private async void Blink()
    {
        _initialtext = _txtView.Text;
        _txtView.Text = Resources.GetString(Resource.String.Speak_now);
        while (VoiceRecognizerActive)
        {
            await Task.Delay(200);
            _blink = !_blink;
            Activity.RunOnUiThread(() =>
            {
                if (_blink) 
                    _txtView.Visibility = ViewStates.Invisible; 
                else 
                    _txtView.Visibility = ViewStates.Visible; 
            }); 
        }
        _txtView.Text = _initialtext;
        _txtView.Visibility = ViewStates.Visible;
    }

//this is for Xamarin android

//这是用于Xamarin android