网络相册

这个例子需要点基础知识:
Gallery的使用,这个网上的例子多的是

  • 如何使用AsyncTask
  • 使用URLConnection从网络下载图片
  • 异步加载在Android应用上用处很广泛,这篇例子的Adapter部分完全可以移植到其他的组件中,如ListView。

下面,我们来看代码吧:
首先,是最核心的代码ImageAdapter的实现,所有核心的功能都是在这里实现的

  
  
  
  
  1. public class ImageAdapter extends BaseAdapter{
  2. private List<String> imageUrls;
  3. private Context context;
  4. public ImageAdapter(List<String> imageUrls, Context context) {
  5. this.imageUrls = imageUrls;
  6. this.context = context;
  7. }
  8. public int getCount() {
  9. return imageUrls.size();
  10. }
  11. public Object getItem(int position) {
  12. return imageUrls.get(position);
  13. }
  14. public long getItemId(int position) {
  15. return position;
  16. }
  17. public View getView(int position, View convertView, ViewGroup parent) {
  18. Bitmap image;
  19. if(convertView==null){
  20. convertView = LayoutInflater.from(context).inflate(R.layout.item,null); //实例化convertView
  21. image = UrlConnectionActivity.imagesCache.get(imageUrls.get(position)); //从缓存中读取图片
  22. if(image==null){
  23. //当缓存中没有要使用的图片时,先显示默认的图片
  24. image = UrlConnectionActivity.imagesCache.get("background_non_load");
  25. //异步加载图片
  26. LoadImageTask task = new LoadImageTask(convertView);
  27. task.execute(imageUrls.get(position));
  28. }
  29. convertView.setTag(image);
  30. }
  31. else{
  32. image = (Bitmap) convertView.getTag();
  33. }
  34. ImageView imageView = (ImageView) convertView.findViewById(R.id.gallery_image);
  35. imageView.setImageBitmap(image);
  36. imageView.setScaleType(ImageView.ScaleType.FIT_XY);
  37. return convertView;
  38. }
  39. //加载图片的异步任务
  40. class LoadImageTask extends AsyncTask<String,Void,Bitmap>{
  41. private View resultView;
  42. LoadImageTask(View resultView) {
  43. this.resultView = resultView;
  44. }
  45. // doInBackground完成后才会被调用
  46. @Override
  47. protected void onPostExecute(Bitmap bitmap) {
  48. //调用setTag保存图片以便于自动更新图片
  49. resultView.setTag(bitmap);
  50. }
  51. //从网上下载图片
  52. @Override
  53. protected Bitmap doInBackground(String... params) {
  54. Bitmap image=null;
  55. try {
  56. //new URL对象 把网址传入
  57. URL url = new URL(params[0]);
  58. //取得链接
  59. URLConnection conn = url.openConnection();
  60. conn.connect();
  61. //取得返回的InputStream
  62. InputStream is = conn.getInputStream();
  63. //将InputStream变为Bitmap
  64. image = BitmapFactory.decodeStream(is);
  65. is.close();
  66. UrlConnectionActivity.imagesCache.put(params[0],image); //把下载好的图片保存到缓存中
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. }
  70. return image;
  71. }
  72. }
  73. }

在Activity中使用

  
  
  
  
  1. public class MyActivity extends Activity{
  2. public static HashMap<String,Bitmap> imagesCache=new HashMap<String, Bitmap>(); //图片缓存
  3. private Gallery images_ga;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.url_connection_image);
  8. init();
  9. }
  10. private void init(){
  11. Bitmap image= BitmapFactory.decodeResource(getResources(),R.drawable.jiazaizhong);
  12. imagesCache.put("background_non_load",image); //设置缓存中默认的图片
  13. images_ga = (Gallery) findViewById(R.id.image_wall_gallery);
  14. List<String> urls = new ArrayList<String>(); //图片地址List
  15. //奶茶MM的图片,嘻嘻
  16. urls.add("http://fujian.xabbs.com/forum/201109/02/160646nn9hjjiimixvkxhe.jpg");
  17. urls.add("http://img1.cache.netease.com/catchpic/A/A9/A9D98040B397C366AE93E67871346561.jpg");
  18. urls.add("http://new.aliyiyao.com/UpFiles/Image/2011/01/13/nc_129393721364387442.jpg");
  19. urls.add("http://pic.viewpics.cn/2011/07/03/naichaMMzhangzetianzuixinzhaopian/18.jpg");
  20. urls.add("http://i1.sinaimg.cn/ent/m/c/2010-01-18/U1819P28T3D2847679F326DT20100118115712.jpg");
  21. urls.add("http://comic.sinaimg.cn/2011/0824/U5237P1157DT20110824161051.jpg");
  22. ImageAdapter imageAdapter = new ImageAdapter(urls,this);
  23. images_ga.setAdapter(imageAdapter);
  24. }
  25. }

在ImageAdapter中用到的XML文件:res/layout/item.xml

  
  
  
  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent">
  5. <ImageView
  6. android:layout_width="wrap_content"
  7. android:layout_height="wrap_content"
  8. android:id="@+id/gallery_image"/>
  9. </LinearLayout>


来自为知笔记(Wiz)


你可能感兴趣的:(网络相册)