如何使用asyncTask从资源加载图像

时间:2014-09-10 15:22:07

标签: android android-listview android-asynctask

在这个自定义适配器中,在我的列表视图中只有一个imageview。我在这个列表视图中加载了一个图像列表。我想在asynctask中加载新图像但是idont知道如何实现asynctask并且大多数教程指的是图像下载但我使用的是可绘制资源,所以请指导我通过这个如何实现异步加载图像来替换当前可见的图像图。

class MyAdapter extends BaseAdapter {

    private Context context;
    private int images[];

    public MyAdapter(Context context, int images[]) {
        this.context = context;
        this.images = images;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return images.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return images[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    class MyViewHolder {
        ImageView imageView;

        public MyViewHolder(View v) {
            // TODO Auto-generated constructor stub
            imageView = (ImageView) v.findViewById(R.id.imgView);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View row = convertView;
        MyViewHolder holder = null;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.custom_list, parent, false);

            holder = new MyViewHolder(row);
            row.setTag(holder);
        } else {
            holder = (MyViewHolder) row.getTag();
            row = convertView;
        }
        holder.imageView.setImageResource(images[position]);
        return row;
    }
}

这是我的MainActivity,我在调用MyAdapter来获取图像列表。

public class MainActivity extends Activity {

    private ListView listView;
    private MyAdapter myAdapter;

    int[] images = { R.drawable.img001, R.drawable.img002, R.drawable.img003,
            R.drawable.img004, R.drawable.img005, R.drawable.img006,
            R.drawable.img007, R.drawable.img008, R.drawable.img009,
            R.drawable.img010, R.drawable.img011, R.drawable.img012,
            R.drawable.img013, R.drawable.img014, R.drawable.img015,
            R.drawable.img016, R.drawable.img017, R.drawable.img018,
            R.drawable.img019, R.drawable.img020, R.drawable.img021,
            R.drawable.img022, R.drawable.img023, R.drawable.img024,
            R.drawable.img025, R.drawable.img026, R.drawable.img027,
            R.drawable.img028, R.drawable.img029, R.drawable.img030,
            R.drawable.img031, R.drawable.img032, R.drawable.img033,
            R.drawable.img034, R.drawable.img035, R.drawable.img036,
            R.drawable.img037, R.drawable.img038, R.drawable.img039,
            R.drawable.img040, R.drawable.img041, R.drawable.img042,
            R.drawable.img043, R.drawable.img044, R.drawable.img045,
            R.drawable.img046, R.drawable.img047, R.drawable.img048,
            R.drawable.img049, R.drawable.img050, R.drawable.img051,
            R.drawable.img052, R.drawable.img053, R.drawable.img054,
            R.drawable.img055, R.drawable.img056, R.drawable.img057,
            R.drawable.img058, R.drawable.img059, R.drawable.img060,
            R.drawable.img061, R.drawable.img062, R.drawable.img063,
            R.drawable.img064, R.drawable.img065, R.drawable.img066,
            R.drawable.img067, R.drawable.img068, R.drawable.img069,
            R.drawable.img070, R.drawable.img071, R.drawable.img072,
            R.drawable.img073, R.drawable.img074, R.drawable.img075,
            R.drawable.img076, R.drawable.img077, R.drawable.img078,
            R.drawable.img079, R.drawable.img080, R.drawable.img081,
            R.drawable.img082, R.drawable.img083, R.drawable.img084,
            R.drawable.img085, R.drawable.img086, R.drawable.img087,
            R.drawable.img088, R.drawable.img089, R.drawable.img090,
            R.drawable.img091, R.drawable.img092, R.drawable.img093,
            R.drawable.img094, R.drawable.img095, R.drawable.img096,
            R.drawable.img097, R.drawable.img098, R.drawable.img099,
            R.drawable.img100 };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);     
        myAdapter = new MyAdapter(this, images);
        listView = (ListView) findViewById(R.id.imgList);
        listView.setAdapter(myAdapter);
    }
  }

2 个答案:

答案 0 :(得分:6)

dependencies {
    compile 'com.squareup.picasso:picasso:2.3.2'
}

Picasso.with(context).load(R.drawable.<id>).into(holder.imageView);

答案 1 :(得分:2)

这里有一个完整的例子:http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

这正是您所需要的。但是,如果你开始使用Android,这段代码真的很复杂。一个更好的建议是使用一个库,有一些选择: