当滑动的中间的时候,有点卡,所以我用了个异步加载,可以异步加载的时候,有一张大点的图片出现了内存泄漏的问题。没加异步的时候,没有这样的问题,这是为什么呢?是不是异步加载写错了。异步加载该怎么写?真正的无限循环能不能实现?而不是把getCount()里面把值设成无限大

解决方案 »

  1.   

    我的异步加载实在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 };
    imageView = new ImageView[imagesId.length];
    } // 返回要显示的图片的总数
    public int getCount() {
    return imagesId.length;
    } // 获得相关的数据项中的指定位置的数据集。这里我们可以指定为该位置的Bitmap
    public Object getItem(int position) {
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), imagesId[position]);
    return bitmap;
    } // 返回相关位置的item的id,这里返回和position一样的ID
    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);
    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]]);
    return null;
    } @Override
    protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    imageView[position].setImageBitmap(bitmap);
    }
    }
    }