业务需要,需要在Gallery中实现定时循环滚动效果,而网上的大部分资料都是循环跳动,没有像屏幕拖动那种效果,经过几天的研究和资料整合,找出了解决方法。
首先定时循环:
ScheduledThreadPoolExecutor timer = (ScheduledThreadPoolExecutor) Executors
.newScheduledThreadPool(1);
ImageTask imageTask = new ImageTask();
timer.scheduleAtFixedRate(imageTask, 0, 8, TimeUnit.SECONDS);
private class ImageTask extends TimerTask {
public void run() {
// getPos是我在imageAdapter中加入的方法,在ImageAdapter加一个成员变量position,然后在getView中将position
的值负值给成员变量,getPos()返回这个成员变量值
int curPos = imageAdapter.getPos();
Message message2 = new Message();
Bundle data = new Bundle();
data.putInt("pos", curPos);
message2.what = 1;
message2.setData(data);
myHandler.sendMessage(message2);
}
然后在Handler对象中处理:
imageAdapter.setCount(focusList.size());
focusGallery.setAdapter(imageAdapter);
//注意,这两个事件是我模拟处理的,其中的数字是我用用手拖动然后用在实际Gallery的onFling事件Log打印出来的。
想实现定时有一个假象的拖动动作,就模拟了这两个事件。
MotionEvent e1 = MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_DOWN, 322.25406f, 108.34271f, 0);
MotionEvent e2 = MotionEvent.obtain(
SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),
MotionEvent.ACTION_MOVE, 146.52338f, 122.55939f, 0);
//这里一定要使用setSelection,否则每次都会从第一张图片滚动,也就是说只要速度正常,只出现第一张和第二张图片
选择当前图片,然后在当前位置下滚动。
focusGallery.setSelection(msg.getData().getInt("pos"));
if(firstLoad){
firstLoad = false;
}else{
//-945这个值可以自己测试做相应的调整,负值表示向右滑动
focusGallery.onFling(e1, e2, -945, 0);
}
addHeaderPointImage(focusList.size());
}
然后在Gallery的Adapter中然getCount()返回Integer.MAX_VALUE;在getView中position参数进行如下处理
int imageSize = focusList.size();
position = position % imageSize;
然后就可以无限循环了。
最后处理一下那个圆点。
focusGallery.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> paramAdapterView,
View paramView, int paramInt, long paramLong) {
int curPos = imageAdapter.getPos();
paramInt = curPos % focusList.size();
Log.v("liuyx", curPos+"");
for (int i = 0; i < pointImageList.size(); i++) {
if (paramInt == i) {
pointImageList.get(i).setImageResource(
R.drawable.information_focus_cirlce_current);
} else {
pointImageList.get(i).setImageResource(
R.drawable.information_focus_cirlce);
}
}
}
public void onNothingSelected(AdapterView<?> paramAdapterView) {
}
});
分享到:
相关推荐
Gallery无限循环.zip
Android3D画廊Gallery实现无限循环、自动跳转、倒影效果,可根据不同情况自己设置图片间间距,从而改变整体间距样式。
ViewPager 无限滚动 gallery效果
无限循环滑动 gallery viewPager 自定义组件 我都封装起来了很简单,好用!
绝对好用的gallery 经过修改增加倒影,支持自动滚动,手动滚动,图片点击事件,配有图片说明文字
可以android里德gallery的图片无限循环
Gallery选中高亮和无限循环,http://blog.csdn.net/cmdkmd/article/details/7576307
android Gallery 3d实现无限循环播放 用做广告板最后了 有多种效果 有倒影 能无限循环播放 当滑动到最后的时候 能反过来播放
Android Gallery 3张图无限循环 左右滑动都有效
Gallery画廊效果,循环banner效果,Gallery画廊效果,循环banner效果Gallery画廊效果,循环banner效果Gallery画廊效果,循环banner效果
NULL 博文链接:https://kusebingtang.iteye.com/blog/907193
Gallery实现广告循环 Gallery实现广告循环
这个Gallery实现了图片自动循环播放+倒影效果,还是挺实用的。推荐下载哈。嘿嘿。肯定不会后悔的。里面都有详细的说明。 使用:直接导入项目运行就行了。
1.gallery循环 2.java反射机制获取图片 3.可以从gallery中删除图片 初学必看。。
android Gallery 实现无线循环代码。
Gallery中间ImageView变大效果
android 中Gallery控件相当于一个图片浏览器,将图片资源加载进去便可以显示其效果
Gallery实现了3D效果,让你体验完美的图片浏览效果
可以实现左右循环滑动,并且能够定时切换图片。