How does android write a TextView with circular text scrolling?
:
In layout, declare: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;">
The activity is called as follows:
Private void initStatus () {ivState = (ImageView) findViewById (R. id. news_statusinput); // The text length must be greater than the control width. String s = "dsafsdfsdf (# Happy) fsgtpd (# Shut up) 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 ();}} public void setStatus(String status){ this.status = status; if(!TextUtils.isEmpty(status)){ stateList = ParseNewsInfoUtil.parseStr(status); } }
Private Handler handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {switch (msg. what) {case ScrollText. 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; private float textLength = 0f; // text length private float viewWidth = 0f; private float step = 0f; // The text's abscissa private float y = 0f; // the text's ordinate private float temp_view_plus_text_length = 0.0f; // The Temporary Variable private float temp_view_plus_two_text_length = 0.0f; // The temporary variable for calculation: private boolean isStarting = false; // whether to start rolling private int left = 0; private int right = 0; private Paint = null; // drawing style private String text = ""; // text content private Bitmap txtBmp; private Canvas txtCanvas; private FontMetrics fontMetrics; private Timer timer = new Timer (); private ArrayList
StateList; Handler handler; TimerTask task = new TimerTask () {public void run () {if (handler! = Null & isStarting) {Message msg = Message. obtain (); 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, every time you change the text content or text effect, you need to reinitialize */public void in It (WindowManager windowManager, Handler handler) {try {this. handler = handler; paint = new Paint (); paint. setAntiAlias (true); paint. setStyle (Style. STROKE); paint. setTextSize (getTextSize (); paint. 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. isFace () {// emoticons typographical 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 = getPadding Top (); // 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
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; bytes = 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) ;}}/*** // *** start rolling */public void startScroll () {isStarting = true ;} /*** // *** stop rolling */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; // if (viewWidth <textLength) {x = temp_view_plus_text_length-step;} int len = stateList. size (); float curLen = x; for (int I = 0; I <len; I ++) {LinkInfo info = stateList. get (I); if (info. isFace () {// emoticons typographical 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 scroll txtCanvas. drawText (strContent, curLen, y, txtPaint); // These textcurLen ++ = xLen;} canvas are also included in the early drawtext.txt Bmp file. 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