Blinking Text in android view

2019-01-08 20:12发布

How do I display Blinking Text in android.

Thank you all.

7条回答
迷人小祖宗
2楼-- · 2019-01-08 20:47

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

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));
查看更多
【Aperson】
3楼-- · 2019-01-08 20:49

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.

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

查看更多
手持菜刀,她持情操
4楼-- · 2019-01-08 20:53
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

查看更多
Explosion°爆炸
5楼-- · 2019-01-08 20:59

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.

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

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

查看更多
三岁会撩人
6楼-- · 2019-01-08 21:00

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!

<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>
查看更多
仙女界的扛把子
7楼-- · 2019-01-08 21:04

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 time of the blink with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
myText.startAnimation(anim);

Hope this helps!

查看更多
登录 后发表回答