In this paper, an example is given to illustrate the method of Android to achieve caption scrolling effect. It's mostly a ready-made Java class file, written by Android's got up Tony, this time forwarding, and hope the original author Tony doesn't mind. This Android marquee scrolling class has a number of customization features that define the pause time for the current scrolling to the end, in milliseconds, and the current scrolling speed, the smaller the value, the faster it will be.
The main implementation code is as follows:
Package com.tony.autoscroll;
Import Android.content.Context;
Import Android.os.Handler;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import android.view.MotionEvent;
Import Android.widget.ScrollView;
/** * @author Tony */public class Autoscrollview extends ScrollView {private final Handler Handler = new Handler ();
Private long Duration = 50;
Private Boolean isscrolled = false;
private int currentindex = 0;
Private long period = 1000;
private int currenty =-1;
private Double X;
private double y;
private int type =-1;
/** * @param context */public Autoscrollview (context) {This (context, NULL); }/** * @param context * @param attrs/Public Autoscrollview (context, AttributeSet attrs) {The context,
Attrs, 0); /** * @param context * @param attrs * @param defstyle/Public Autoscrollview (context, AttributeSet attrs
, int defstyle) {Super (context, attrs, Defstyle); } public BoOlean Ontouchevent (Motionevent event) {int Action = event.getaction ();
Switch (Action) {case motionevent.action_down:x=event.getx ();
Y=event.gety ();
if (type = = 0) {setscrolled (false);
} break;
Case MotionEvent.ACTION_MOVE:double Movey = event.gety ()-y;
Double MoveX = Event.getx ()-X;
LOG.D ("Test", "Movey =" + Movey + "MoveX =" + MoveX); if (movey>20| | movey<-20) && (MoveX < | | MoveX > -50) && getparent ()!= null) {GetParent (). Requestdisallo
Wintercepttouchevent (TRUE);
} break;
Case MotionEvent.ACTION_UP:if (type = = 0) {currentindex = getscrolly ();
Setscrolled (TRUE);
} break;
Default:break;
Return Super.ontouchevent (event); @Override public boolean onintercepttouchevent (Motionevent p_event) {log.d ("test", "Onintercepttouchevent"
);
return true; /** * To determine whether the current scroll state * @return The isscrolled * * * public boolean isscrolled () {return IsScrolled; /** * Turn automatic scrolling on or off * @param isscrolled * True to open, false to OFF/public void setscrolled (Boolean isscrolled) {T
his.isscrolled = isscrolled;
AutoScroll ();
/** * Gets the pause time of the current scroll to the end, in milliseconds * @return the period/public long Getperiod () {return period; /** * Sets the pause time for the current scroll to the end, in milliseconds * @param period *the period to set */public void Setperiod (long period) {This.perio
d = period;
/** * Gets the current scrolling speed in milliseconds, the smaller the value, the faster.
* @return The speed/public long getspeed () {return duration;
/** * Sets the current scrolling speed in milliseconds, the smaller the value, the faster.
* @param speed *the duration to set */public void setspeed (long speed) {this.duration = speed;
public void SetType (int type) {this.type = type; private void AutoScroll () {handler.postdelayed (new Runnable () {@Override public void Run () {Boolean flag = is
scrolled;
if (flag) {//LOG.D ("test", "currenty =" + CurrentY + "getscrolly () =" + getscrolly ()); if (CurrentY = = getscrolly ()) {try {Thread.sleeP (period);
catch (Interruptedexception e) {e.printstacktrace ();
} currentindex = 0;
Scrollto (0, 0);
Handler.postdelayed (this, period);
else {currenty = getscrolly ();
Handler.postdelayed (this, duration);
currentindex++;
Scrollto (0, Currentindex * 1);
} else {//currentindex = 0;
Scrollto (0, 0);
}}, duration); }
}