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程序设计有所帮助。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下