1.新建Android工程AsyncLoadPicture
新建布局文件activity_main.xml主界面为一个GridView,还有其布局文件gridview_item.xml
2.功能主界面MainActivity.java,主代码如下
1 package com.example.asyncloadpicture; 2 3 import java.util.ArrayList; 4 5 import android.app.Activity; 6 import android.content.Context; 7 import android.graphics.Bitmap; 8 import android.graphics.BitmapFactory; 9 import android.os.AsyncTask; 10 import android.os.Bundle; 11 import android.widget.GridView; 12 13 /** 14 * @author ymw 15 * @data 2014-12-03 16 */ 17 public class MainActivity extends Activity { 18 19 private GridView gridViewImg; 20 private ArrayList<ImageEntity> imageList = new ArrayList<ImageEntity>(); 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main); 26 27 gridViewImg = (GridView) findViewById(R.id.gridViewImg); 28 initData(); 29 } 30 31 private void initData() { 32 for (int i = 0; i < 15; i++) { 33 ImageEntity b = new ImageEntity(); 34 b.setImage(BitmapFactory.decodeResource(getResources(), 35 R.drawable.nopicture)); 36 37 imageList.add(b); 38 } 39 40 ImageAdapter adapter = new ImageAdapter(getApplicationContext(), 41 imageList); 42 gridViewImg.setAdapter(adapter); 43 String[] params = { 44 "http://images.cnitblog.com/i/169207/201408/112229149526951.png", 45 "p2" }; 46 new ImageLoadTask(getApplicationContext(), adapter).execute(params); 47 } 48 49 /** 图片异步加载类 */ 50 public class ImageLoadTask extends AsyncTask<String, Void, Void> { 51 private ImageAdapter adapter; 52 53 public ImageLoadTask(Context context, ImageAdapter adapter) { 54 this.adapter = adapter; 55 } 56 57 @Override 58 protected Void doInBackground(String... params) { 59 String url = params[0];// 获取传来的参数,图片uri地址 60 String p2 = params[1]; 61 for (int i = 0; i < adapter.getCount(); i++) { 62 ImageEntity bean = (ImageEntity) adapter.getItem(i); 63 Bitmap bitmap = BitmapFactory.decodeStream(Request 64 .HandlerData(url)); 65 bean.setImage(bitmap); 66 publishProgress(); // 通知去更新UI 67 } 68 69 return null; 70 } 71 72 public void onProgressUpdate(Void... voids) { 73 if (isCancelled()) 74 return; 75 // 更新UI 76 adapter.notifyDataSetChanged(); 77 } 78 } 79 80 /** 图片实体 */ 81 public class ImageEntity { 82 private Bitmap image; 83 84 public Bitmap getImage() { 85 return image; 86 } 87 88 public void setImage(Bitmap image) { 89 this.image = image; 90 } 91 92 } 93 }
3.GridView列表图片适配器代码ImageAdapter.java
1 package com.example.asyncloadpicture; 2 3 import java.util.ArrayList; 4 5 import android.content.Context; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.BaseAdapter; 10 import android.widget.ImageView; 11 12 import com.example.asyncloadpicture.MainActivity.ImageEntity; 13 14 /** 15 * 16 * @author ymw 17 * @data 2014-12-03 图片适配器 18 */ 19 public class ImageAdapter extends BaseAdapter { 20 21 private Context _context; 22 private ArrayList<ImageEntity> _list; 23 24 public ImageAdapter(Context context, ArrayList<ImageEntity> imageList) { 25 this._context = context; 26 this._list = imageList; 27 } 28 29 @Override 30 public int getCount() { 31 return _list.size(); 32 } 33 34 @Override 35 public Object getItem(int position) { 36 return _list.get(position); 37 } 38 39 @Override 40 public long getItemId(int position) { 41 return position; 42 } 43 44 @Override 45 public View getView(int position, View convertView, ViewGroup parent) { 46 LayoutInflater inflater = LayoutInflater.from(_context); 47 convertView = inflater.inflate(R.layout.gridview_item, null); 48 49 ImageView imgView = (ImageView) convertView.findViewById(R.id.imgView); 50 ImageEntity b = _list.get(position); 51 // 如果有图片则读取,没有则跳过 52 if (b.getImage() != null) { 53 imgView.setImageBitmap(b.getImage()); 54 } 55 return convertView; 56 } 57 }
4.请求网络读取图片的类Request.java
1 package com.example.asyncloadpicture; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.net.URL; 6 import java.net.URLConnection; 7 8 import android.graphics.drawable.Drawable; 9 import android.util.Log; 10 11 /** 12 * 工具类,主要根据url读取图片返回流的方法 13 * 14 * @author ymw 15 * 16 */ 17 public class Request { 18 public static InputStream HandlerData(String url) { 19 InputStream inStream = null; 20 21 try { 22 URL feedUrl = new URL(url); 23 URLConnection conn = feedUrl.openConnection(); 24 conn.setConnectTimeout(10 * 1000); 25 inStream = conn.getInputStream(); 26 } catch (Exception e) { 27 e.printStackTrace(); 28 } 29 30 return inStream; 31 } 32 33 /** 直接返回Drawable对象设置图片 */ 34 public static Drawable loadImageFromNetwork(String imageUrl) { 35 Drawable drawable = null; 36 try { 37 // 可以在这里通过文件名来判断,是否本地有此图片 38 drawable = Drawable.createFromStream( 39 new URL(imageUrl).openStream(), "image.jpg"); 40 } catch (IOException e) { 41 Log.d("test", e.getMessage()); 42 } 43 if (drawable == null) { 44 Log.d("test", "null drawable"); 45 } else { 46 Log.d("test", "not null drawable"); 47 } 48 49 return drawable; 50 } 51 }
5.代码实例如下链接:
http://files.cnblogs.com/_ymw/AsyncLoadPicture_%E5%8D%9A%E5%AE%A2%E9%99%84%E4%BB%B6.rar
时间: 2024-10-14 11:11:16