首页 > 代码库 > 使用AsyncTask异步加载图片
使用AsyncTask异步加载图片
主要看看AsyncTask如何使用:
MainActivity.java
[java] view plaincopyprint?
- public class MainActivity extends Activity {
- private static final String TAG = "MainActivity";
- private List<String> datas;
- private LayoutInflater inflater;
- private Context context;
- private MyAdapter adapter;
- private ListView listview;
- private List<String> imgUrls;
- private List<TextView> list = new ArrayList<TextView>();
- private List<String> sortUrl = new ArrayList<String>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- inflater = LayoutInflater.from(this);
- context = MainActivity.this;
- listview = (ListView) findViewById(R.id.listview);
- initData();
- adapter= new MyAdapter();
- listview.setAdapter(adapter);
- }
- private void initData() {
- imgUrls = new ArrayList<String>();
- imgUrls.add("http://img5.douban.com/view/movie_poster_cover/mpst/public/p2199507156.jpg");
- imgUrls.add("http://img5.douban.com/lpic/s27397768.jpg");
- imgUrls.add("http://img3.douban.com/lpic/s27505695.jpg");
- imgUrls.add("http://img3.douban.com/lpic/s27461340.jpg");
- imgUrls.add("http://img3.douban.com/lpic/s27451442.jpg");
- imgUrls.add("http://img5.douban.com/lpic/s27453059.jpg");
- imgUrls.add("http://img3.douban.com/lpic/s27452925.jpg");
- imgUrls.add("http://img5.douban.com/lpic/s27452846.jpg");
- imgUrls.add("http://img3.douban.com/lpic/s27325332.jpg");
- imgUrls.add("http://img3.douban.com/lpic/s27325315.jpg");
- imgUrls.add("http://img5.douban.com/lpic/s27389537.jpg");
- imgUrls.add("http://img3.douban.com/view/movie_poster_cover/mpst/public/p2199638985.jpg");
- imgUrls.add("http://img3.douban.com/view/movie_poster_cover/mpst/public/p2199637691.jpg");
- imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2195768476.jpg");
- imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2180002996.jpg");
- imgUrls.add("http://img3.douban.com/view/movie_poster_cover/lpst/public/p2182978810.jpg");
- imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2164841498.jpg");
- imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2161445839.jpg");
- imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2183221886.jpg");
- imgUrls.add("http://img3.douban.com/view/movie_poster_cover/lpst/public/p2164383301.jpg");
- imgUrls.add("http://img3.douban.com/view/movie_poster_cover/lpst/public/p2199638985.jpg");
- imgUrls.add("http://img3.douban.com/view/movie_poster_cover/lpst/public/p2199407040.jpg");
- imgUrls.add("http://img3.douban.com/view/movie_poster_cover/lpst/public/p2176900061.jpg");
- imgUrls.add("http://img3.douban.com/view/movie_poster_cover/lpst/public/p2180405730.jpg");
- imgUrls.add("http://img3.douban.com/view/movie_poster_cover/lpst/public/p2197059721.jpg");
- imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2196353608.jpg");
- imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2187391526.jpg");
- imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2185073849.jpg");
- imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2196328516.jpg");
- imgUrls.add("http://img5.douban.com/view/movie_poster_cover/lpst/public/p2166850749.jpg");
- imgUrls.add("http://img3.douban.com/view/movie_poster_cover/lpst/public/p2183957412.jpg");
- imgUrls.add("http://img3.douban.com/spic/s27459164.jpg");
- imgUrls.add("http://img3.douban.com/spic/s27480483.jpg");
- imgUrls.add("http://img3.douban.com/spic/s27460951.jpg");
- imgUrls.add("http://img3.douban.com/spic/s27453772.jpg");
- imgUrls.add("http://img3.douban.com/spic/s27410904.jpg");
- imgUrls.add("http://img5.douban.com/spic/s27466008.jpg");
- imgUrls.add("http://img3.douban.com/spic/s3259484.jpg");
- imgUrls.add("http://img3.douban.com/spic/s3081692.jpg");
- imgUrls.add("http://img3.douban.com/spic/s1400630.jpg");
- imgUrls.add("http://img5.douban.com/spic/s4715377.jpg");
- imgUrls.add("http://img3.douban.com/spic/s6201192.jpg");
- imgUrls.add("http://img5.douban.com/spic/s2650727.jpg");
- imgUrls.add("http://img3.douban.com/spic/s3636124.jpg");
- imgUrls.add("http://img5.douban.com/spic/s3595798.jpg");
- imgUrls.add("http://img3.douban.com/spic/s27078194.jpg");
- imgUrls.add("http://img3.douban.com/view/event_poster/large/public/2a35a85a873e8e7.jpg");
- imgUrls.add("http://img3.douban.com/view/event_poster/large/public/ce66e0569742fd7.jpg");
- imgUrls.add("http://img3.douban.com/view/event_poster/large/public/4c44752e83900d9.jpg");
- imgUrls.add("http://img3.douban.com/view/event_poster/large/public/d22f7b9e5c8c92c.jpg");
- }
- private class MyAdapter extends BaseAdapter{
- @Override
- public int getCount() {
- return imgUrls.size();
- }
- @Override
- public Object getItem(int position) {
- return imgUrls.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup arg2) {
- ViewHolder viewHolder = null;
- if(convertView == null){
- viewHolder = new ViewHolder();
- convertView = inflater.inflate(R.layout.item, null);
- viewHolder.image = (ImageView) convertView.findViewById(R.id.img);
- convertView.setTag(viewHolder);
- }else{
- viewHolder = (ViewHolder) convertView.getTag();
- }
- ImageTask imageTask = new ImageTask(viewHolder.image);
- iamgeTask.execute(imgUrls.get(position));
- return convertView;
- }
- class ViewHolder{
- ImageView image;
- }
- }
- }
ImageTask.java
[java] view plaincopyprint?
- public class ImageTask extends AsyncTask<String, Void, Bitmap> {
- private ImageView iv;
- public ImageTask(ImageView iv){
- this.iv = iv;
- }
- @Override
- protected Bitmap doInBackground(String... param) {
- String imgUrl = param[0];
- try {
- URL url = new URL(imgUrl);
- try {
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- InputStream in = conn.getInputStream();
- Bitmap bitmap = BitmapFactory.decodeStream(in);
- if(bitmap!=null){
- return bitmap;
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPostExecute(Bitmap result) {
- super.onPostExecute(result);
- if(result != null){
- iv.setImageBitmap(result);
- }
- }
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- }
- }
使用AsyncTask异步加载图片
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。