android实现自动滚动的Gallary控件效果
本文实例讲述了android实现自动滚动的Gallary控件。分享给大家供大家参考。具体如下:
importjava.util.Timer; importjava.util.TimerTask; importandroid.content.Context; importandroid.os.Handler; importandroid.util.AttributeSet; importandroid.util.Log; importandroid.view.KeyEvent; importandroid.view.MotionEvent; importandroid.widget.Gallery; publicclassHomeGalleryextendsGallery { /** *这里的数值,限制了每次滚动的最大长度,图片宽度为480PX。这里设置600效果好一些。这个值越大,滚动的长度就越大。 *也就是会出现一次滚动跨多个Image。这里限制长度后,每次滚动只能跨一个Image */ privatestaticfinalinttimerAnimation=1; privatestaticfinalinttime=2000; privatefinalHandlermHandler=newHandler() { publicvoidhandleMessage(android.os.Messagemsg) { switch(msg.what) { casetimerAnimation: intposition=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; } }; }; privatefinalTimertimer=newTimer(); privatefinalTimerTasktask=newTimerTask() { publicvoidrun() { mHandler.sendEmptyMessage(timerAnimation); } }; publicHomeGallery(ContextparamContext) { super(paramContext); timer.schedule(task,time,time); } publicHomeGallery(ContextparamContext,AttributeSetparamAttributeSet) { super(paramContext,paramAttributeSet); timer.schedule(task,time,time); } publicHomeGallery(ContextparamContext,AttributeSetparamAttributeSet, intparamInt) { super(paramContext,paramAttributeSet,paramInt); timer.schedule(task,time,time); } privatebooleanisScrollingLeft(MotionEventparamMotionEvent1, MotionEventparamMotionEvent2) { floatf2=paramMotionEvent2.getX(); floatf1=paramMotionEvent1.getX(); if(f2>f1) returntrue; returnfalse; } publicbooleanonFling(MotionEventparamMotionEvent1, MotionEventparamMotionEvent2,floatparamFloat1,floatparamFloat2) { intkeyCode; if(isScrollingLeft(paramMotionEvent1,paramMotionEvent2)) { keyCode=KeyEvent.KEYCODE_DPAD_LEFT; }else { keyCode=KeyEvent.KEYCODE_DPAD_RIGHT; } onKeyDown(keyCode,null); returntrue; } publicvoiddestroy() { timer.cancel(); } }
希望本文所述对大家的Android程序设计有所帮助。