【问题标题】:Blinking Text in android view在android视图中闪烁文本
【发布时间】:2010-08-10 15:50:24
【问题描述】:

如何在android中显示闪烁的文字。

谢谢大家。

【问题讨论】:

  • 您是否尝试过例如每秒更改文本的颜色?因为我认为你不能用框架做到这一点。或者你可以使用带有闪烁标签的 webview...
  • 你可以创建一个线程在 View.VISIBLE 和 View.INVISIBLE 之间切换 textView 的可见性
  • 实际上,我认为您不能像 Sephy 建议的那样做 WebView; Webkit 不呈现 标记(至少在 Chrome 中)。因为在用户面前烦人地闪烁文字是吸引他们注意力的一种非常糟糕的方式,而且他们不会因此而喜欢你。
  • 令人不安的是,没有人否决这个问题。它没有显示任何研究工作或任何形式的代码来显示已尝试的内容。答案很有用,应该被赞成......但这个问题应该被忽视。
  • @STTLCU ,这个问题是3年前问的!当该功能没有太多在线谷歌文档时。在我用尽了所有选择之后,我来到了最后的手段,那就是你们专家。请不要有其他想法。谢谢。

标签: android android-animation textview


【解决方案1】:

实际上在 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>

【讨论】:

  • 对我不起作用:E/AndroidRuntime(19645): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class blink
  • 没有记录 - 我是通过源潜水 ICS 找到的。看起来更改是在 2011 年 5 月 17 日引入的。不确定也映射了哪个 SDK 编号。
  • 哦,如果这不是很明显 - 我很确定“blink”标签是一个复活节彩蛋。我并不是认真地建议这是正确的解决方案! :)
  • FC on Gingerbread(我猜也在下面。与 Jahufar 的消息相同。因此,如果您确实想使用它,请在不同版本上进行测试。
  • 哇!这个效果很好。有什么方法可以以编程方式使用它?
【解决方案2】:

为它创建一个视图动画。您可以在 0 秒内从 100% 到 0% 进行 alpha 渐变,然后在一个循环中再次返回。这样一来,Android 就可以智能地处理它,而您不必乱用线程和浪费 CPU。

在此处了解有关动画的更多信息:
http://developer.android.com/reference/android/view/animation/package-summary.html

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

【讨论】:

  • 保持文本闪烁的最佳方法是什么?
【解决方案3】:

在代码中使用线程总是会浪费 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) {

    }

}

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

【讨论】:

  • @We'reAllMadHere 我建议你使用 Value Animator 来完成这项任务,它非常易于使用。 stackoverflow.com/questions/15582434/…
  • 这是一种非常清晰的方法,但如果不透明,它会为整个 TextView 设置动画,包括其背景颜色。有没有办法只让text里面的TextView闪烁
【解决方案4】:

可以通过添加一个 ViewFlipper 来实现,该 ViewFlipper 交替两个 TextView 并且在它们切换时可以应用 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));

【讨论】:

    【解决方案5】:
    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();
     }
    
    }
    

    现在可以正常使用了

    【讨论】:

    • 我不认为这会做任何事情,除非你只是连续调用 update() (比如从一个线程)。
    【解决方案6】:
     public static void blinkText(TextView textView) {
        Animation animation = new AlphaAnimation(1, 0);         // Change alpha from fully visible to invisible
        animation.setDuration(300);                             // duration - half a second
        animation.setInterpolator(new LinearInterpolator());    // do not alter animation rate
        animation.setRepeatCount(-1);                            // Repeat animation infinitely
        animation.setRepeatMode(Animation.REVERSE);             // Reverse animation at the end so the button will fade back in
        textView.startAnimation(animation);
    }
    

    我不记得我在哪里找到的,但这是迄今为止我见过的最好的

    【讨论】:

      【解决方案7】:
          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 的

      【讨论】:

      • VoiceRecognizerActive = true; Task.Run(() => { 闪烁(); });
      猜你喜欢
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      • 2017-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多