This example describes the Gallary control that Android implements to automatically scroll. Share to everyone for your reference. as follows:
Import Java.util.Timer;
Import Java.util.TimerTask;
Import Android.content.Context;
Import Android.os.Handler;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import android.view.KeyEvent;
Import android.view.MotionEvent;
Import Android.widget.Gallery; The values in the public class Homegallery extends Gallery {/** * Limit the maximum length of each scroll, and the width of the picture is 480PX. Here set the 600 effect better.
The larger the value, the greater the length of the scroll. * That is, there will be one scrolling across multiple image.
Here limit the length after each scroll can only cross one image/private static final int timeranimation = 1;
private static final int time = 2000; Private final Handler Mhandler = new Handler () {public void Handlemessage (Android.os.Message msg) {SWITC
H (msg.what) {case Timeranimation:int position = getselecteditemposition ();
LOG.I ("msg", "Position:" +position);
if (Position >= (GetCount ()-1)) {OnKeyDown (keyevent.keycode_dpad_left, NULL);
else {onKeyDown (keyevent.keycode_dpad_right, NULL); } break;
Default:break;
}
};
};
Private Final Timer timer = new timer (); Private final TimerTask task = new TimerTask () {public void run () {Mhandler.sendemptymessage (Timeranimat
ION);
}
};
Public Homegallery (Context Paramcontext) {super (Paramcontext);
Timer.schedule (Task, time, time); } public Homegallery (context Paramcontext, AttributeSet Paramattributeset) {super (Paramcontext, Paramattributeset
);
Timer.schedule (Task, time, time); } public Homegallery (context Paramcontext, attributeset paramattributeset, int paramint) {Super (Paramcontex
T, Paramattributeset, Paramint);
Timer.schedule (Task, time, time); Private Boolean Isscrollingleft (Motionevent paramMotionEvent1, motionevent paramMotionEvent2) {Float F2 =
Parammotionevent2.getx ();
Float F1 = Parammotionevent1.getx ();
if (F2 > F1) return true;
return false; public boolean onfling (motionevent paramMotionEvent1, Motionevent ParamMotionEvent2, float paramFloat1, float paramFloat2) {int keycode;
if (Isscrollingleft (ParamMotionEvent1, ParamMotionEvent2)) {keycode = Keyevent.keycode_dpad_left;
else {keycode = Keyevent.keycode_dpad_right;
} onKeyDown (keycode, NULL);
return true;
public void Destroy () {timer.cancel ();
}
}
I hope this article will help you with your Android program.