The Running Horse lights in Android are not very effective and cannot control the speed, stop and start immediately, and are also affected by the focus. Because the project needs to use a controllable high running horse lamp effect, so I wrote a custom TextView
Note:: When the layout file references this view, paddingLeft and paddingRigh must both be 0dp. To add these two attributes, you can modify the code on your own.
Android: ellipsize = "marquee" android: singleLine = "true" must be addedCopy codeThe Code is as follows: public class MarqueeText extends TextView implements Runnable {
Private int currentScrollX; // The current rolling position
Private boolean isStop = false;
Private int textWidth;
Private boolean isMeasure = false;
Public MarqueeText (Context context ){
Super (context );
// TODO Auto-generated constructor stub
}
Public MarqueeText (Context context, AttributeSet attrs ){
Super (context, attrs );
}
Public MarqueeText (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
}
@ Override
Protected void onDraw (Canvas canvas ){
// TODO Auto-generated method stub
Super. onDraw (canvas );
If (! IsMeasure) {// you only need to obtain the text width once.
GetTextWidth ();
IsMeasure = true;
}
}
/**
* Get Text width
*/
Private void getTextWidth (){
Paint paint = this. getPaint ();
String str = this. getText (). toString ();
TextWidth = (int) paint. measureText (str );
}
@ Override
Public void run (){
CurrentScrollX-= 2; // scroll speed
ScrollTo (currentScrollX, 0 );
If (isStop ){
Return;
}
If (getScrollX () <=-(this. getWidth ())){
ScrollTo (textWidth, 0 );
CurrentScrollX = textWidth;
// Return;
}
PostDelayed (this, 5 );
}
// Start rolling
Public void startScroll (){
IsStop = false;
This. removeCallbacks (this );
Post (this );
}
// Stop rolling
Public void stopScroll (){
IsStop = true;
}
// Scroll from scratch
Public void startFor0 (){
CurrentScrollX = 0;
StartScroll ();
}
}
Layout file:Copy codeThe Code is as follows: <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<Button
Android: id = "@ + id/start"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: onClick = "start"
Android: text = "start"/>
<Button
Android: id = "@ + id/stop"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: onClick = "stop"
Android: text = "stop"/>
<Button
Android: id = "@ + id/startfor0"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: onClick = "startFor0"
Android: text = "starting from scratch"/>
<Simtice. demo. marqueetext. MarqueeText
Android: id = "@ + id/test"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: background = "#339320"
Android: ellipsize = "marquee"
Android: singleLine = "true"
Android: text = "this is the true text marquee effect. This is the true text marquee effect"
Android: textColor = "#000000"
Android: textSize = "20dp">
</Simtice. demo. marqueetext. MarqueeText>
</LinearLayout>
MainActivity
Copy codeThe Code is as follows: public class MainActivity extends Activity {
Private MarqueeText test;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Test = (MarqueeText) this. findViewById (R. id. test );
}
Public void start (View v ){
Test. startScroll ();
}
Public void stop (View v ){
Test. stopScroll ();
}
Public void startFor0 (View v ){
Test. startFor0 ();
}
}