如何在android中显示闪烁文字。
谢谢大家。
答案 0 :(得分:114)
您可以使用:
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);
希望这有帮助!
答案 1 :(得分:27)
实际上在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>
答案 2 :(得分:11)
为它创建一个视图动画。您可以在0秒内从100%到0%进行Alpha淡入淡出,然后在一个循环中再次返回。这样,Android就可以处理它,你不必乱用线程并浪费CPU。
有关动画的更多信息:
http://developer.android.com/reference/android/view/animation/package-summary.html
答案 3 :(得分:5)
可以通过添加一个交替显示两个TextView的ViewFlipper来完成,并且可以在切换时应用Fadein和Fadeout动画。
布局文件:
<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>
活动代码:
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));
答案 4 :(得分:5)
在代码中使用线程总是会浪费CPU时间并降低应用程序的性能。你不应该一直使用线程。如果需要,请使用。
为此目的使用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(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) {
}
}
如果您有任何疑问,请告诉我。
答案 5 :(得分:0)
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;
现在创建方法
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);
}
现在创建闪烁的更新方法
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();
}
}
现在工作正常
答案 6 :(得分:-1)
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;
}
//这是针对Xamarin android