Android How to write a circular text scrolling TextView

Source: Internet
Author: User

:


This is stated in layout:

<com.kaixin001.view.scrolltext android:id= "@+id/news_statustxt"     android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:paddingleft= "10DP" android:paddingright= "10DP" Android:layout_ Gravity= "center_vertical" android:textcolor= "#000000" android:textsize= "16sp" android:hint= "@string/news_state_ Hint "android:inputtype=" text "/>

This is called by the activity:

   private void Initstatus () {    ivstate = (ImageView) Findviewbyid (r.id.news_statusinput);    The text length must be greater than the width of the control    String s = "dsafsdfsdf (#开心) FSGFDG (#闭嘴) FDGDFGDFGDFGDFGDFGFDGFDGFDGFDGDFG";    Newsmodel.setstatus (s);    String Strmodel = Newsmodel.getstatus ();        Setstatetext (Strmodel);        }    private void Setstatetext (String strmodel) {if (! Textutils.isempty (Strmodel)) {tvstate.setstatelist (Newsmodel.getstatelist ()); Tvstate.settext (StrModel);            Tvstate.init (Getwindowmanager (), handler);            Tvstate.startscroll ();            Tvstate.start ();        }} <pre name= "code" class= "java" > <span style= "white-space:pre" ></span> public void SetStatus (String Status) {    this.status = status;    if (! Textutils.isempty (status) {    statelist = parsenewsinfoutil.parsestr (status);    }   <span style= "White-space:pre" ></SPAN>}

Private Handler Handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {switch (msg.what) {case Scrollte Xt. Text_timer:if (tvstate! = null) {Tvstate.scrolltext ();} Break;default:break;}};

Import Java.util.arraylist;import java.util.timer;import Java.util.timertask;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmap.config;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.paint.fontmetrics;import Android.graphics.paint.style;import Android.os.handler;import Android.os.message;import Android.util.AttributeSet ; Import Android.view.windowmanager;import Android.widget.textview;import Com.kaixin001.item.linkinfo;import Com.kaixin001.model.statefacemodel;public class Scrolltext extends TextView {public static final int text_timer = 100;pri vate float textLength = 0f;//text length private float viewwidth = 0f;private float step = 0f;//Text's horizontal axis private float y = 0f;//text The ordinate private float Temp_view_plus_text_length = 0.0f;//is used to calculate the temporary variable private float temp_view_plus_two_text_length = 0.0f;// Temporary variable for calculation private Boolean isstarting = false;//whether to start scrolling private int left = 0;private int right = 0;privAte paint paint = null;//drawing style private String text = "";//text content private Bitmap txtbmp;private Canvas txtcanvas;private Fontm Etrics fontmetrics;private Timer timer = new timer ();p rivate arraylist<linkinfo> statelist; Handler Handler; TimerTask task = new TimerTask () {public void run () {if (handler! = null && isstarting) {Message msg = MESSAGE.OBT Ain (); msg.what = Text_timer;handler.sendmessage (msg);}}; Public Scrolltext (Context context) {super (context);} Public Scrolltext (context context, AttributeSet Attrs) {Super (context, attrs);} Public Scrolltext (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);} /** *//** * Text initialization, each time you change text content or text effects, you need to reinitialize */public void init (WindowManager windowmanager, Handler Handler) {try {this . Handler = Handler;paint = new Paint ();p Aint.setantialias (True);p Aint.setstyle (Style.stroke);p aint.settextsize ( GetTextSize ());p Aint.setcolor (Getcurrenttextcolor ()), Text = GetText (). toString (); textLength = 0;//TextLength = Paint.meAsuretext (text); int len = Statelist.size (); for (int i = 0; i < len; i++) {LinkInfo info = statelist.get (i); if (INFO.ISF Ace ()) {//emoji typesetting bitmap facebmp = Statefacemodel.getinstance (). Getsmallfaceicon (Info.getcontent ()); int Xlen = Facebmp.getwidth (); TextLength + = Xlen + 4;continue;} String strcontent = info.getcontent (), Float Xlen = Paint.measuretext (strcontent); TextLength + = Xlen;} left = This.getpaddingleft (), right = This.getpaddingright (); step = Textlength;fontmetrics = Paint.getfontmetrics (); y = Getpaddingtop ();//GetTextSize () + getpaddingtop (); txtbmp = null;} catch (Exception ex) {ex.printstacktrace ();}} public void Scrolltext () {if (!isstarting) {return;} Invalidate (), if (Viewwidth < textLength) {step + = 0.5;if (Step > Temp_view_plus_two_text_length) {step = TextLength; }}}public void Setstatelist (arraylist<linkinfo> statelist) {this.statelist = statelist;} private void Settxtbmp () {if (txtbmp = = NULL && FontMetrics! = null) {y =-paint.ascent ();//Fontmetrics.bottOm-//fontmetrics.ascent;//(this.getheight ()-//(int) fontmetrics.ascent)/2;viewwidth = getwidth ()-Left-right;temp_  View_plus_text_length = viewwidth + textlength;temp_view_plus_two_text_length = viewwidth + textLength * 2;txtCanvas = new Canvas (); int width = (int) viewwidth;float height = getheight (); txtbmp = Bitmap.createbitmap (width, (int) height, Config. argb_8888); Txtcanvas.setbitmap (txtbmp);}} /** *//** * starts rolling */public void Startscroll () {isstarting = true;} /** *//** * Stop scrolling */public void Stopscroll () {isstarting = false;//invalidate ();} public void Start () {Timer.schedule (task, 10, 20);} public void Stop () {timer.cancel ();} @Overridepublic void OnDraw (canvas canvas) {try {settxtbmp (); if (txtbmp = = null) {return;} Paint Txtpaint = new paint (); Txtpaint.setcolor (Color.White); Txtpaint.setstyle (Style.fill); txtcanvas.drawrect (0, 0, Txtbmp.getwidth (), Txtbmp.getheight (), txtpaint); Txtpaint.setantialias (true); Txtpaint.setstyle (Style.STROKE); Txtpaint.settextsize (GetTextSize ()); txtpaint.setcOlor (Getcurrenttextcolor ()); float x = 0;//width of step as text if (Viewwidth < textLength) {x = Temp_view_plus_text_length-st EP;} int len = statelist.size (), Float Curlen = x;for (int i = 0; i < len; i++) {LinkInfo info = statelist.get (i); if (info.is Face ()) {//emoji typesetting bitmap facebmp = Statefacemodel.getinstance (). Getsmallfaceicon (Info.getcontent ()); int Xlen = Facebmp.getwidth (); Txtcanvas.drawbitmap (facebmp, Curlen + 2, 0, txtpaint); Curlen + = Xlen + 4;continue;} String strcontent = Info.getcontent (), strcontent = Strcontent.replaceall ("\ n", ""); float Xlen = Txtpaint.measuretext ( strcontent);//Because the value of x has been reduced, the text can be scrolled txtcanvas.drawtext (strcontent, Curlen, y, txtpaint); <span style= "font-family:arial, Helvetica, Sans-serif;" >txtcanvas early drawtext.txtbmp also naturally with these Text</span>curlen + + Xlen;} Canvas.drawbitmap (Txtbmp, left, 0, paint);} catch (Exception ex) {ex.printstacktrace ();}} @Overrideprotected void onsizechanged (int w, int h, int oldw, int oldh) {super.onsizechanged (W, H, OLDW, OLDH); TXTBMP = Null;settxtbmp ();}} 


Code: http://download.csdn.net/detail/baidu_nod/7736733


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: info-contact@alibabacloud.com 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.