Android TextView vertical auto-scroll subtitle implementation

Source: Internet
Author: User

Textview vertical auto-scroll subtitle implementation

Some time ago, I found a post on the Internet, and textview automatically rolled the subtitles horizontally. Today's project needs to scroll the subtitles vertically. Its implementation principle is the same as that of water products. Both override the onDraw method of textview.
Note the need to implement vertical auto-scroll subtitles
1. You need to calculate the number of rows of rolling subtitles based on the textview width and font size. To implement this function, you need to re-write two methods: 1. onMeasure, 2. onDraw. Because you need to obtain the textview width, you need to call the following code in the onMeasure method. The specific method is as follows:

[Java]
Package com. test;
Public VerticalScrollTextView extends TextView {
Private float step = 0f;
Private Paint mPaint;
Private String text;
Private float width;
Private List <String> textList = new ArrayList <String> (); // The branch stores the display information of textview.
 
Public VerticalScrollTextView (Context context, AttributeSet attrs ){
Super (context, attrs );
}

 
Public VerticalScrollTextView (Context context ){
Super (context );
}

@ Override
Protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec ){
Super. onMeasure (widthMeasureSpec, heightMeasureSpec );
Width = MeasureSpec. getSize (widthMeasureSpec );
Final int widthMode = MeasureSpec. getMode (widthMeasureSpec );
If (widthMode! = MeasureSpec. EXACTLY ){
Throw new IllegalStateException ("ScrollLayout only canmCurScreen run at EXACTLY mode! ");
}

Float length = 0;
If (text = null | text. length () = 0 ){
Return;
}

// The following code calculates the number of lines displayed in textview Based on the width and font size.
 
TextList. clear ();

StringBuilder builder = new StringBuilder ();
For (int I = 0; I <text. length (); I ++ ){
Log. e ("textviewscroll", "" + I + text. charAt (I ));
If (length <width ){
Builder. append (text. charAt (I ));
Length + = mPaint. measureText (text. substring (I, I + 1 ));
If (I = text. length ()-1 ){
Log. e ("textviewscroll", "" + I + text. charAt (I ));
TextList. add (builder. toString ());
}
} Else {
TextList. add (builder. toString (). substring (0, builder. toString (). length ()-1 ));
Builder. delete (0, builder. length ()-1 );
Length = mPaint. measureText (text. substring (I, I + 1 ));
I --;
}

}
}
 
 
// The following code uses the number of lines displayed above to draw the text on the canvas and update it in real time.
@ Override
Public void onDraw (Canvas canvas ){
If (textList. size () = 0) return;
For (int I = 0; I <textList. size (); I ++ ){
Canvas. drawText (textList. get (I), 0, this. getHeight () + (I + 1) * mPaint. getTextSize ()-step, getPaint ());
}

Invalidate ();
Step = step + 0.3f;
If (step> = this. getHeight () + textList. size () * mPaint. getTextSize ()){
Step = 0;
}
}
 
}
Some other extended functions are not added. For example, if the rolling speed is set, the scrolling will take a long time to stop.

Send this post. If you have any suggestions, please leave a message and discuss them together.
Write a layout file by calling the method. For example

[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "300dp"
Android: layout_height = "400dp"
Android: orientation = "vertical">
<Pre name = "code" class = "html"> <pre name = "code" class = "html"> <com. test. VerticalScrollTextView
<Pre name = "code" class = "html"> android: layout_width = "200dp"
Android: layout_height = "300dp"
Android: textSize = "20dp"
Android: text = "Good Rain Knows the season, when spring is happening. The wind sneaked into the night, moistening things silently. The wild paths are full of clouds, and the fire of the river boat is only known. Xiao looks at the Red and wet area, and the flowers weigh the golden city. "/>
</LinearLayout>

From fengyoujie's column
 

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: 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.