Use custom Textview with vertical scrolling to develop two-color ball scrolling Effect
First look:
Here. We have customized a Textview
Package com. example. myscroltextview. view;
Import java. util. ArrayList;
Import android. content. Context;
Import android. graphics. Canvas;
Import android. OS. Handler;
Import android. OS. Message;
Import android. util. AttributeSet;
Import android. view. MotionEvent;
Import android. widget. TextView;
Public class ScrollTextView extends TextView {
/**
* Strings in each line
*/
ArrayList LineStrings;
/**
* Current displacement
*/
Float currentY;
/**
* Process rolling messages
*/
Handler handler;
/**
* Text to be displayed
*/
String scrollText =;
/**
* The actual width is used in the configuration width = xxdp.
*/
Private int exactlyWidth =-1;
/**
* Real height, which works in the configured height = xxdip
*/
Private int exactlyHeight =-1;
Public String getScrollText (){
Return scrollText;
}
Public void setScrollText (String scrollText ){
This. scrollText = scrollText;
Reset ();
}
/**
* Reset
*/
Private void reset (){
RequestLayout ();
Invalidate ();
CurrentY = 0;
}
Public ScrollTextView (Context context, AttributeSet attrs ){
Super (context, attrs );
Init ();
}
Public ScrollTextView (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
Init ();
}
Public ScrollTextView (Context context ){
Super (context );
Init ();
}
/**
* Rolling?
*/
Boolean scrolling = false;
/**
* Actual height: The Desired height is displayed for all words.
*/
Float absloutHeight = 0;
/**
* Handler message sending Interval
*/
Int delayTime = 10;
/**
* The distance of each scroll
*/
Float speed = 15.5f;
/**
* Initialization
*/
Void init (){
Handler = new Handler (){
@ Override
Public void handleMessage (Message msg ){
If (absloutHeight <= getHeight ()){
CurrentY = 0;
Stop ();
Return;
}
Switch (msg. what ){
Case 0 :{
CurrentY = currentY-speed;
ResetCurrentY ();
Invalidate ();
Handler. sendEmptyMessageDelayed (0, delayTime );
Break;
}
Case 1 :{
CurrentY + = msg. arg1;
ResetCurrentY ();
Invalidate ();
}
}
}
/**
* Reset currentY (set currentY to 0 when currentY exceeds absloutHeight)
*/
Private void resetCurrentY (){
If (currentY> = absloutHeight | currentY <=-absloutHeight | getHeight () <= 0 ){
CurrentY = 0;
}
}
};
}
/**
* Y coordinate of the finger of the last trigger event
*/
Float lastY = 0;
/**
* If it is true, it indicates that it is a scroll.
*/
Boolean needStop;
Public void pause (){
If (scrolling ){
Stop ();
NeedStop = true;
}
}
Public void goOn (){
If (needStop ){
Play ();
NeedStop = false;
}
}
@ Override
Public boolean onTouchEvent (MotionEvent event ){
Switch (event. getAction ()){
Case MotionEvent. ACTION_DOWN:
DistanceY = lastY = event. getY ();
DistanceX = event. getX ();
Pause ();
Return true;
Case MotionEvent. ACTION_MOVE:
Float dy = event. getY ()-lastY;
LastY = event. getY ();
// CurrentY = currentY + dy;
Message msg = Message. obtain ();
Msg. what = 1;
Msg. arg1 = (int) dy;
Handler. sendMessage (msg );
Return true;
Case MotionEvent. ACTION_UP:
Case MotionEvent. ACTION_CANCEL:
GoOn ();
Float y = event. getY ()-distanceY;
Float x = event. getX ()-distanceX;
If (Math. sqrt (y * y + x * x) <performUpScrollStateDistance ){
UpdateScrollStatus ();
}
Return true;
}
Return super. onTouchEvent (event );
}
/**
* When the finger moves within the distance of this size, it is considered to be an event of rolling status change. A value greater than this value indicates finger movement
*/
Public static final long extends mupscrollstatedistance = 5;
Public float distanceY = 0;
Public float distanceX = 0;
/**
* Changing the rolling status
*/
Public void updateScrollStatus (){
If (scrolling ){
Stop ();
} Else {
Play ();
}
}
/**
* Start rolling
*/
Public void play (){
If (! Scrolling ){
Handler. sendEmptyMessage (0 );
Scrolling = true;
}
}
/**
* Stop rolling
*/
Public void stop (){
If (scrolling ){
Handler. removeMessages (0 );
Scrolling = false;
}
}
@ Override
Protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec ){
Int width = MeasureWidth (widthMeasureSpec );
Int height = MeasureHeight (width, heightMeasureSpec );
SetMeasuredDimension (width, height );
CurrentY = 0;
If (height <absloutHeight ){
Play ();
} Else {
Stop ();
}
}
/**
* Measurement width
*
* @ Param widthMeasureSpec
* @ Return
*/
Private int MeasureWidth (int widthMeasureSpec ){
Int mode = MeasureSpec. getMode (widthMeasureSpec );
Int width = MeasureSpec. getSize (widthMeasureSpec );
// If it is wrap_content
If (mode = MeasureSpec. AT_MOST ){
Double abwidth = getPaint (). measureText (scrollText );
Width = Math. min (int) Math. rint (abwidth), width );
ExactlyWidth =-1;
}
If (mode = MeasureSpec. EXACTLY ){
ExactlyWidth = width;
}
Return width;
}
/**
* Height Measurement
*
* @ Param width: width
* @ Param heightMeasureSpec
* @ Return
*/
Private int MeasureHeight (int width, int heightMeasureSpec ){
Int mode = MeasureSpec. getMode (heightMeasureSpec );
Int height = MeasureSpec. getSize (heightMeasureSpec );
GenerateTextList (width );
Int lines = lineStrings. size ();
AbsloutHeight = lines * getLineHeight () + getPaddingBottom () + getPaddingTop ();
// If it is wrap_content
If (mode = MeasureSpec. AT_MOST ){
Height = (int) Math. min (absloutHeight, height );
ExactlyHeight =-1;
} Else if (mode = MeasureSpec. EXACTLY ){
ExactlyHeight = height;
}
Return height;
}
/**
* Is it the first letter of an English word?
*
* @ Param str
* @ Param I
* @ Return
*/
Boolean isENWordStart (String str, int I ){
If (I = 0 ){
Return true;
} Else if (str. charAt (I-1) = ''){
Return true;
}
Return false;
}
/**
* Get the characters of a row
*
* @ Param MaxWidth
* @ Param str
* @ Return
*/
Private String getLineText (int MaxWidth, String str ){
// Real row
StringBuffer trueStringBuffer = new StringBuffer ();
// Temporary row
StringBuffer tempStringBuffer = new StringBuffer ();
For (int I = 0; I <str. length (); I ++ ){
Char c = str. charAt (I );
String add =;
// If c is a letter, you need to consider the line feed function.
If (! IsChinese (c) & isENWordStart (str, I )){
Int place = getNextSpecePlace (I, str );
// Locate the next space
If (place>-1 ){
Add = str. substring (I, place) +;
If (getPaint (). measureText (add)> MaxWidth ){
Add = + c;
} Else {
I = place;
}
} Else {
Add = + c;
}
} Else {
Add = + c;
}
TempStringBuffer. append (add );
String temp = tempStringBuffer. toString ();
Float width = getPaint (). measureText (temp. toString ());
If (width <= MaxWidth ){
TrueStringBuffer. append (add );
} Else {
Break;
}
}
Return trueStringBuffer. toString ();
}
/**
* Locate the next space
*
* @ Param I
* @ Param str
* @ Return
*/
Int getNextSpecePlace (int I, String str ){
For (int j = I; j <str. length (); j ++ ){
Char c = str. charAt (j );
If (c = ''){
Return j;
}
}
Return-1;
}
/**
* Generate a multi-line string list
*
* @ Param MaxWidth
*/
Public void generateTextList (int MaxWidth ){
LineStrings = new ArrayList ();
String remain = scrollText;
While (! Remain. equals ()){
String line = getLineText (MaxWidth, remain );
LineStrings. add (line );
Remain = remain. substring (line. length (), remain. length ());
}
};
@ Override
Protected void onDraw (Canvas canvas ){
Super. onDraw (canvas );
Float x = getPaddingLeft ();
Float y = getPaddingTop ();
Float lineHeight = getLineHeight ();
Float textSize = getPaint (). getTextSize ();
For (int I = 0; I <lineStrings. size (); I ++ ){
Y = lineHeight * I + textSize + currentY;
Float min = 0;
If (exactlyHeight>-1 ){
Min = Math. min (min, exactlyHeight-absloutHeight );
}
If (y <min ){
Y = y + absloutHeight;
} Else if (y> = min & y <textSize + min ){
// If the top text has reached the point where it needs to be rolled out from the bottom of the loop
Canvas. drawText (lineStrings. get (I), x, y + absloutHeight, getPaint ());
}
If (y> = absloutHeight ){
// If the bottom-end text has reached and needs to be rolled out cyclically from the top
Canvas. drawText (lineStrings. get (I), x, y, getPaint ());
Y = y-absloutHeight;
}
Canvas. drawText (lineStrings. get (I), x, y, getPaint ());
}
}
/**
* Judge whether it is Chinese
*
* @ Param c
* @ Return
*/
Private static final boolean isChinese (char c ){
Character. UnicodeBlock ub = Character. UnicodeBlock. of (c );
If (ub = Character. UnicodeBlock. cjk_uniied_ideographs
| Ub = Character. UnicodeBlock. CJK_COMPATIBILITY_IDEOGRAPHS
| Ub = Character. UnicodeBlock. cjk_uniied_ideographs_extension_a
| Ub = Character. UnicodeBlock. GENERAL_PUNCTUATION
| Ub = Character. UnicodeBlock. CJK_SYMBOLS_AND_PUNCTUATION
| Ub = Character. UnicodeBlock. HALFWIDTH_AND_FULLWIDTH_FORMS ){
Return true;
}
Return false;
}
}
Our Activity:
Package com. example. myscroltextview;
Import java. util. ArrayList;
Import java. util. List;
Import java. util. Random;
Import java. util. Timer;
Import java. util. TimerTask;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import com. example. myscroltextview. view. MyScrollTextView;
Public class MyMainActivity extends Activity {
Private MyScrollTextView ball1, ball2, ball3, ball4, ball5, ball6, ball7;
Private Button changeButton;
Private TimerTask task;
Private Timer timer = new Timer ();
Private final Boolean [] isUp = {false, true };
Private int time;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. my_activity_main );
Ball1 = (MyScrollTextView) findViewById (R. id. myscroll1 );
Ball2 = (MyScrollTextView) findViewById (R. id. myscroll2 );
Ball3 = (MyScrollTextView) findViewById (R. id. myscroll3 );
Ball4 = (MyScrollTextView) findViewById (R. id. myscroll4 );
Ball5 = (MyScrollTextView) findViewById (R. id. myscroll5 );
Ball6 = (MyScrollTextView) findViewById (R. id. myscroll6 );
Ball7 = (MyScrollTextView) findViewById (R. id. myscroll7 );
ChangeButton = (Button) findViewById (R. id. my_button_update );
// Final List Strs = new ArrayList ();
// Strs. add (xx01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 19 19 20 21 22 24 25 26 27 28 29 30 31 32 33 34 35 );
// Strs. add (yy01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 19 19 20 21 22 24 25 26 27 28 29 30 31 32 33 34 35 );
Final List SsqRed = new ArrayList ();
// SsqRed. add (01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 26 27 28 29 30 31 32 33 );
SsqRed. add (0102030405060708091011121314151617181920212223242526272829303132333 );
Final List SsqBlue = new ArrayList ();
SsqBlue. add (1, 01020304050607080910111213141516 );
Final List DltRed = new ArrayList ();
DltRed. add (0102030405060708091011121314151617181920212223242526272829303132333435 );
Final List DltBlue = new ArrayList ();
DltBlue. add (010203040506070809101112 );
ChangeButton. setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View v ){
Ball1.setText ();
Ball2.setText ();
Ball3.setText ();
Ball4.setText ();
Ball5.setText ();
Ball6.setText ();
Ball7.setText ();
Time = 2;
Task = new TimerTask (){
@ Override
Public void run (){
RunOnUiThread (new Runnable () {// UI thread
@ Override
Public void run (){
Time --;
If (time <= 0 ){
Ball1.stop ();
Ball2.stop ();
Ball3.stop ();
Ball4.stop ();
Ball5.stop ();
Ball6.stop ();
Ball7.stop ();
Ball1.setScrollText ();
Ball2.setScrollText ();
Ball3.setScrollText ();
Ball4.setScrollText ();
Ball5.setScrollText ();
Ball6.setScrollText ();
Ball7.setScrollText ();
Ball1.setText (02 );
Ball2.setText (06 );
Ball3.setText (24 );
Ball4.setText (06 );
Ball5.setText (14 );
Ball6.setText (27 );
Ball7.setText (11 );
Task. cancel ();
} Else {
Int I = (int) (ssqRed. size () * Math. random ());
Ball1.setScrollText (ssqRed. get (I ));
Ball2.setScrollText (ssqRed. get (I ));
Ball3.setScrollText (ssqRed. get (I ));
Ball4.setScrollText (ssqRed. get (I ));
Ball5.setScrollText (ssqRed. get (I ));
Ball6.setScrollText (ssqRed. get (I ));
Ball7.setScrollText (ssqBlue. get (I ));
Random random = new Random ();
Random. nextInt (2); // [0, 2)
Ball1.isUp = isUp [random. nextInt (2)];
Ball2.isUp = isUp [random. nextInt (2)];
Ball3.isUp = isUp [random. nextInt (2)];
Ball4.isUp = isUp [random. nextInt (2)];
Ball5.isUp = isUp [random. nextInt (2)];
Ball6.isUp = isUp [random. nextInt (2)];
Ball7.isUp = isUp [random. nextInt (2)];
}
}
});
}
};
Timer. schedule (task, 0, 1000 );
}
});
}
}
We can control the speed of each ball. Here we use a random number. Yes, the speed of each ball is inconsistent and within a certain range,
We can also control the direction of each ball, up or down.