Android的gallery实现无限循环和降低选择频率
gallery是一个很好用的控件,可以实现很炫的效果。不过有的时候要对gallery进行改造,使其效果更完美。
??Gallery组件主要用于横向显示图像列表,不过按常规做法。Gallery组件只能有限地显示指定的图像。也就是说,如果为Gallery组件指定了10张图像,那么当Gallery组件显示到第10张时,就不会再继续显示了。为了实现像UCWeb的循环显现导航菜单,要进行相应的改造。??
循环显示有些类似于循环链表,最后一个结点的下一个结点又是第1个结点。循环显示图像也可以模拟这一点。
如果这时Gallery组件正好显示到最后一个图像,position参数值正好为getCount() - 1。那么我们如何再让Gallery显示下一个图像呢?也就是说让position参数值再增1,对!将getCount()方法的返回值也增1。
那么这里还有一个问题,如果position参数值无限地增加,就意味着resIds数组要不断地增大,这样会大大消耗系统的资源。想到这,就需要解决两个问题:既要position不断地增加,又让resIds数组中保存的图像资源ID是有限的,该怎么做呢?对于getCount()方法非常好解决,可以让getCount方法返回一个很大的数,例如,Integer.MAX_VALUE。这时position参数值就可以随着Gallery组件的图像不断向前移动而增大。现在resIds数组只有15个元素,如果position的值超过数组边界,要想继续循环取得数组中的元素(也就是说,当position的值是15时,取resIds数组的第0个元素,是16时取第1个元素),最简单的方法就是取余,代码如下:
resIds[position % resIds.length]
对ImageAdapter类做了如下两个改进:
1. 使getCount方法返回一个很大的值。建议返回Integer.MAX_VALUE。
2. 在getView方法中通过取余来循环取得resIds数组中的图像资源ID。
通过上面两点改进,可以使图像列表在向右移动时会循环显示图像。当然,这种方法从本质上说只是伪循环,也就是说,如果真把图像移动到getCount方法返回的值那里,那也就显示到最后一个图像的。不过在这里getCount方法返回的是Integer.MAX_VALUE,这个值超过了20亿,除非有人真想把图像移动到第20亿的位置,否则Gallery组件看着就是一个循环显示图像的组件。代码如下:
? ?
view plaincopy to clipboardprint?
01.public class ImageAdapter extends BaseAdapter? ?
02.? ?{? ?
03.? ?? ? int mGalleryItemBackground;? ?
04.? ?? ? private Context mContext;? ?
05.??
06.? ?? ? public ImageAdapter(Context context)? ?
07.? ?? ? {? ?
08.? ?? ?? ???mContext = context;? ?
09.? ?? ?? ???TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);? ?
10.? ?? ?? ???mGalleryItemBackground = typedArray.getResourceId(? ?
11.? ?? ?? ?? ?? ?? ? R.styleable.Gallery_android_galleryItemBackground, 0);? ?? ?? ?? ?? ?? ?? ?? ?? ?
12.? ?? ? }? ?
13.? ?? ? // 第1点改进,返回一个很大的值,例如,Integer.MAX_VALUE? ?
14.? ?? ? public int getCount()? ?
15.? ?? ? {? ?
16.? ?? ?? ???return Integer.MAX_VALUE;? ?
17.? ?? ? }? ?
18.??
19.? ?? ? public Object getItem(int position)? ?
20.? ?? ? {? ?
21.? ?? ?? ???return position;? ?
22.? ?? ? }? ?
23.??
24.? ?? ? public long getItemId(int position)? ?
25.? ?? ? {? ?
26.? ?? ?? ???return position;? ?
27.? ?? ? }? ?
28.??
29.? ?? ? public View getView(int position, View convertView, ViewGroup parent)? ?
30.? ?? ? {? ?
31.? ?? ?? ???ImageView imageView = new ImageView(mContext);? ?
32.? ?? ?? ?? ?? ?// 第2点改进,通过取余来循环取得resIds数组中的图像资源ID? ?
33.? ?? ?? ???imageView.setImageResource(resIds[position % resIds.length]);? ?
34.? ?? ?? ???imageView.setScaleType(ImageView.ScaleType.FIT_XY);? ?
35.? ?? ?? ???imageView.setLayoutParams(new Gallery.LayoutParams(163, 106));? ?
36.? ?? ?? ???imageView.setBackgroundResource(mGalleryItemBackground);? ?
37.? ?? ?? ???return imageView;? ?
38.? ?? ? }? ?
39.? ?}??
public class ImageAdapter extends BaseAdapter
? ? {
? ?? ???int mGalleryItemBackground;
? ?? ???private Context mContext;
? ?? ???public ImageAdapter(Context context)
? ?? ???{
? ?? ?? ?? ?mContext = context;
? ?? ?? ?? ?TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
? ?? ?? ?? ?mGalleryItemBackground = typedArray.getResourceId(
? ?? ?? ?? ?? ?? ???R.styleable.Gallery_android_galleryItemBackground, 0);? ?? ?? ?? ?? ?? ?? ?? ?
? ?? ???}
? ?? ???// 第1点改进,返回一个很大的值,例如,Integer.MAX_VALUE
? ?? ???public int getCount()
? ?? ???{
? ?? ?? ?? ?return Integer.MAX_VALUE;
? ?? ???}
? ?? ???public Object getItem(int position)
? ?? ???{
? ?? ?? ?? ?return position;
? ?? ???}
? ?? ???public long getItemId(int position)
? ?? ???{
? ?? ?? ?? ?return position;
? ?? ???}
? ?? ???public View getView(int position, View convertView, ViewGroup parent)
? ?? ???{
? ?? ?? ?? ?ImageView imageView = new ImageView(mContext);
? ?? ?? ?? ?? ? // 第2点改进,通过取余来循环取得resIds数组中的图像资源ID
? ?? ?? ?? ?imageView.setImageResource(resIds[position % resIds.length]);
? ?? ?? ?? ?imageView.setScaleType(ImageView.ScaleType.FIT_XY);
? ?? ?? ?? ?imageView.setLayoutParams(new Gallery.LayoutParams(163, 106));
? ?? ?? ?? ?imageView.setBackgroundResource(mGalleryItemBackground);
? ?? ?? ?? ?return imageView;
? ?? ???}
? ? }
gallery菜单滑动有一个不好的效果就是每次经过中间的菜单都默认是被选中状态,同时会加载数据 以至于切换不流畅,有一种卡卡的感觉!!其实用线程来处理这个问题,一定的时间后如果选择的index值不变,说明已经稳定不变。
private int showingIndex = -1;? ?
private static final int TIME_OUT_DISPLAY =300;? ?
private int toShowIndex = 0;? ?
//--------------------------------------------------? ?
//在选中事件里面做处理? ?
public void onItemSelected(AdapterView<?> parent, View v, final int position,? ?
long id) {? ?
??
??
//--------------------------------------------------? ?
toShowIndex = position;? ?
final Handler handler = new Handler() {? ?? ?
@Override??
public void handleMessage(Message msg) {? ?
if(showingIndex != toShowIndex){? ?
showingIndex = toShowIndex;? ?
menu_position = position;? ?
??
//做你的业务逻辑处理? ?
}? ?
}? ?? ?
};? ?
Thread checkChange = new Thread() {? ?? ???
@Override??
public void run() {? ?
int myIndex = toShowIndex;? ?
try {? ?
sleep( TIME_OUT_DISPLAY );? ?
if( myIndex == toShowIndex ){? ?
handler.sendEmptyMessage(0);? ?? ?
}? ?
} catch (InterruptedException e) {? ?
e.printStackTrace();? ?
}? ?
}? ?? ?
};? ?
??
checkChange.start();? ?
??
.??
}??
来自:http://www.eoeandroid.com/thread-54390-1-1.html
