Gallery 之滑动不流畅的解决办法
2012-05-26 15:38:44 来源:WEB开发网核心提示:Gallery滑动的时候之所以会卡,是因为当它滑动到中间的时候,Gallery 之滑动不流畅的解决办法,默认会为选中状态,那么这个时候就要去加载图片,当前图片是1,上一张是0,如果当图片比较大的时候,就会导致卡一下
Gallery滑动的时候之所以会卡,是因为当它滑动到中间的时候,默认会为选中状态,那么这个时候就要去加载图片,如果当图片比较大的时候,就会导致卡一下。
一、通过异步加载图片的方式
其余的地方不变,只是在Adapter中使用异步加载的方式。
public class GalleryAdapter extends BaseAdapter {
private ImageView[] imageView;// 加载图片的imageView数组
private Context context;
private Integer[] imagesId;// 要显示的图片
public GalleryAdapter(Context context) {
this.context = context;
imagesId = new Integer[] { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.a, R.drawable.b,
R.drawable.c, R.drawable.d, R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.a, R.drawable.b,
R.drawable.c, R.drawable.d, R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d };
imageView = new ImageView[imagesId.length];
}
public int getCount() {
return imagesId.length;
}
public Object getItem(int position) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), imagesId[position]);
return bitmap;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (imageView[position] == null)
imageView[position] = new ImageView(context);
new MyTask().execute(position);//开启异步任务加载图片
imageView[position].setLayoutParams(new MyGallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return imageView[position];
}
private class MyTask extends AsyncTask<Integer, Void, Void> {
private Bitmap bitmap;
private int position;
@Override
protected Void doInBackground(Integer... params) {
//在这里加载图片
position = params[0];
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
bitmap = BitmapFactory.decodeResource(context.getResources(), imagesId[params[0]],
options);
return null;
}
@Override
protected void onPostExecute(Void result) {
//因为该方法是由UI线程调用,所以在这里设置ImageView的图片
super.onPostExecute(result);
imageView[position].setImageBitmap(bitmap);
}
}
}
二、上面的方法,虽然是异步加载,可是,是先开启加载,在显示图片,所以,被选中的ImageView会黑一下。而且不知道为什么,理论上异步加载是不会卡的,可是,用这种方法加载更大一点的图片,还是会卡一下,而且卡的很奇怪,总是在移到某一张图片的时候,在往后面移动, 会先弹出下下一张图片的一小部分,然后,才显示下一张图片。这中间会小卡一下,不明白原因啊!!所以想了下,采用了下面的方法改进。
每次缓冲3张,让ImageView先显示,再去异步加载图片
先看Adapter的代码:
public class GalleryAdapter extends BaseAdapter {
private Context context;
private Integer[] imagesId;// 要显示的图片
private Bitmap[] nearBitmaps;// 自己所缓存的当前图片附近的图片,当前图片是1,上一张是0,下一张是2
private int showingIndex;// 正在显示的图片是第几张图片
public GalleryAdapter(Context context, Integer[] imagesId) {
this.context = context;
this.imagesId = imagesId;
nearBitmaps = new Bitmap[3];
// 最开始的时候,先初始化最开始的3张图片
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
nearBitmaps[1] = BitmapFactory.decodeResource(context.getResources(), imagesId[0], options);
nearBitmaps[2] = BitmapFactory.decodeResource(context.getResources(), imagesId[1], options);
}
public int getCount() {
return imagesId.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = new ImageView(context);
convertView.setLayoutParams(new MyGallery.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
if (position == showingIndex)
((ImageView)convertView).setImageBitmap(nearBitmaps[1]);
if (position == showingIndex - 1) {//说明是向前滑动
((ImageView)convertView).setImageBitmap(nearBitmaps[0]);
}
if (position == showingIndex + 1) {//说明是向后滑动
((ImageView)convertView).setImageBitmap(nearBitmaps[2]);
}
return convertView;
}
public Bitmap[] getNearBitmaps() {
return nearBitmaps;
}
public int getShowingIndex() {
return showingIndex;
}
public void setShowingIndex(int showingIndex) {
this.showingIndex = showingIndex;
}
赞助商链接
