Custom TextView Marquee Effects control start/stop/speed/focus _android

Source: Internet
Author: User
Tags stub xmlns
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
 code as follows:

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:
 code 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= "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

 code 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 ();
}
}






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.