Android: How to Write a circular text scrolling TextView and androidtextview

Source: Internet
Author: User
Tags drawtext

Android: How to Write a circular text scrolling TextView and androidtextview

:


In layout, declare:

<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"/>

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 () ;}< 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 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; imp Ort 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 abscissa private float y = 0f of the text; // The ordinate private float te of the text Mp_view_plus_text_length = 0.0f; // The Temporary Variable private float temp_view_plus_two_text_length = 0.0f; // The Temporary Variable private boolean isStarting = false for calculation; // whether to start rolling 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 FontMetrics fontMetrics; private Timer timer = new Timer (); private ArrayList <LinkInfo> 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 <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; 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); // <span style = "font-family: Arial, Helvetica, sans-serif; "> txtcanvasearly drawtext.txt Bmp also contains the 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



I would like to ask how to enable automatic cyclic conversion of TextView text in android

You need to update the UI through asynchronous threads. There are too many methods to Implement Asynchronous threads in Android, such as AsyncTask. Here is an example:
Private AsyncTask <Object, Integer, Double> mTask = new AsyncTask <Object, Integer, Double> (){

Private int count = 0;

@ Override
Protected Double doInBackground (Object... params ){

// Update once per second
While (mRun ){
Try {
Thread. sleep (1000 );
PublishProgress (++ count );
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
Return null;
}

@ Override
Protected void onProgressUpdate (Integer... values ){
Super. onProgressUpdate (values );
MTextView. setText (String. valueOf (values [0]);
}

@ Override
Protected void onPostExecute (Double result ){
// Asynchronous task execution ends
}
};

How to Implement vertical text scrolling in android textView

It seems that textview does not have the vertical scroll attribute. I didn't try it out anyway. On the same building, we recommend using animation to achieve this effect, but it won't be enough. You can use either of them, I don't know if the landlord wants to implement it on the top of the screen or in other places? I just made a simple example. If you don't need to be so troublesome on the top, you can write all the attributes of animation, so that you can modify them based on your needs.
Package com. mygd;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. util. Log;
Import android. view. animation. AlphaAnimation;
Import android. view. animation. Animation;
Import android. view. animation. Animation. AnimationListener;
Import android. view. animation. AnimationSet;
Import android. view. animation. TranslateAnimation;
Import android. widget. LinearLayout;

Public class gundongActivity extends Activity implements AnimationListener {
/** Called when the activity is first created .*/
Int I = 0;
Handler handler = new Handler ();

// LinearLayout linearLayout1, linearLayout2, linearLayout3;
// AnimationSet animationSet1, animationSet2, and animationSet3;
// TranslateAnimation
// TranslateAnimation1, translateAnimation2, and translateAnimation3;
// AlphaAnimation alphaAnimation1, alphaAnimation2;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
LinearLayout linearLayout1 = (LinearLayout) findViewById (R. id. linearLayout1 );
LinearLayout linearLayout2 = (LinearLayout) findViewById (R. id. linearLayout2 );
LinearLayout linearLayout3 = (LinearLayout) findViewById (R. id. linearLayout3 );
AnimationSet animationSet1 = new AnimationSet (true );
AnimationSet animation... the remaining full text>

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.