Android's own horse-lamp effect is not very good control, can not control speed, can not stop and start immediately, but also by the focus of the impact of the egg ache unceasingly. Because the project needs to use the high control of the effect of the horse, so I wrote a custom TextView
Attention: When the layout file refers to this view, the Paddingleft,paddingrigh must be 0DP, you need to add these two properties, you can modify the code.
Android:ellipsize= "marquee" android:singleline= "true" should also be added to these two attributes
public class MarqueeText extends TextView implements Runnable {
private int currentscrollx;//the current scrolling position
Private Boolean isstop = false;
private int textWidth;
Private Boolean ismeasure = false;
Public MarqueeText {
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) {//Text width only one fetch at a time is OK
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;//scrolling speed
Scrollto (CURRENTSCROLLX, 0);
if (isstop) {
Return
}
if (GETSCROLLX () <=-(This.getwidth ())) {
Scrollto (textWidth, 0);
CURRENTSCROLLX = TextWidth;
Return
}
Postdelayed (this, 5);
}
Start scrolling
public void Startscroll () {
Isstop = false;
This.removecallbacks (this);
Post (this);
}
Stop scrolling
public void Stopscroll () {
Isstop = true;
}
Scroll from beginning to start
public void StartFor0 () {
CURRENTSCROLLX = 0;
Startscroll ();
}
}
Layout file:
<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= "Walk Up"/>
<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= "Start from the beginning"/>
<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= "That's the real word. Happy Light Effect This is the true character of the marquee effect that's the real thing.
Android:textcolor= "#000000"
Android:textsize= "20DP" >
</simtice.demo.marqueetext.MarqueeText>
</LinearLayout>
Mainactivity
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 ();
}
}