Android realizes the focus-independent and selected TextView racing lights _android

Source: Internet
Author: User


Before writing an article on the effect of TextView, then the actual project has found new problems, such as still can not run automatically, text over the display area on the interception of the problem, today changed a way of thinking to achieve, simpler and better use.


Code implementation:

public class Marqueetextview extends TextView {/** Stop scrolling */private Boolean mstopmarquee;
  Private String Mtext;
  private float Mcoordinatex;

  private float mtextwidth;
  Public Marqueetextview (context, AttributeSet attrs) {Super (context, attrs);
    public void SetText (String text) {this.mtext = text;
    Mtextwidth = Getpaint (). Measuretext (Mtext);
    if (mhandler.hasmessages (0)) mhandler.removemessages (0);
  Mhandler.sendemptymessagedelayed (0, 2000);
    @Override protected void Onattachedtowindow () {mstopmarquee = false; if (!
    Stringutils.isempty (Mtext)) mhandler.sendemptymessagedelayed (0, 2000);
  Super.onattachedtowindow ();
    @Override protected void Ondetachedfromwindow () {mstopmarquee = true;
    if (mhandler.hasmessages (0)) mhandler.removemessages (0);
  Super.ondetachedfromwindow ();
    } @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas); if (! Stringutils.isempty (MTEXT)) Canvas.drawtext (Mtext, Mcoordinatex, Getpaint ()); Private Handler Mhandler = new Handler () {@Override public void Handlemessage (msg) {switch (M
          Sg.what) {Case 0:if (Math.Abs (Mcoordinatex) > (mtextwidth +)) {Mcoordinatex = 0;
          Invalidate ();
          if (!mstopmarquee) {sendemptymessagedelayed (0, 2000);
          } else {Mcoordinatex = 1;
          Invalidate ();
          if (!mstopmarquee) {sendemptymessagedelayed (0, 30);
      }} break;
    Super.handlemessage (msg);

  }; }

Code Description:

1, 2000 indicates a delay of 2 seconds to start the effect of the happy lights

2, Mtextwidth + 100 means run out of screen 100 pixels and start running again.

3. Move 1 pixels every 30 milliseconds

4, the principle is very simple, is the timing brush, the use is very simple, direct settext on the line, and with the same system, but can not set the value of XML to run directly, this can modify their own.

5, pay attention to the OnDraw to determine whether the text is empty, here stringutils.isempty replaced to their own decision method can be.

Above is the Android implementation of the example code of the horse, there is a need for friends can refer to.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.