历史版本1:Android基础篇之在ListView中显示网络图片

历史版本1:Android基础篇之在ListView中显示网络图片
返回文档
  • 编辑时间:05-20 19:46 历史版本编辑者:Thiny 历史版本: 最新历史版本 下一历史版本
  • 内容长度:122830 图片数:1目录数:0
  • 修改原因:

大家知道ImageView 类虽然有一个 setImageUri 方法,但不能直接接受一个由网络地址生成的uri作为参数从而显示图片,我们只好使用其 setImageBitmap 方法,如

view plain copy to clipboard print ?
  1. Bitmap mBitmap = null
  2.     
  3. URL url = new URL(imageUrl); 
  4.              
  5. HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
  6.              
  7. InputStream is = conn.getInputStream(); 
  8.              
  9. mBitmap = BitmapFactory.decodeStream(is); 

     在SimpleAdapter中需要一个数据源,用来存储数据的,在显示图片时我们要用HashMap<>存储一个Bitmap;但仅存取了Bitmap时在ListView中是无法显示图片的,我们需要对SimpleAdapter进行处理 。

如下是对SimpleAdaptr处理的主要代码:

view plain copy to clipboard print ?
  1. adapter.setViewBinder(new ViewBinder() { 
  2.              
  3.             public boolean setViewValue(View view, Object data, 
  4.                     String textRepresentation) { 
  5.                 //判断是否为我们要处理的对象 
  6.                 if(view instanceof ImageView  && data instanceof Bitmap){ 
  7.                     ImageView iv = (ImageView) view; 
  8.                  
  9.                     iv.setImageBitmap((Bitmap) data); 
  10.                     return true
  11.                 }else 
  12.                 return false
  13.             } 
  14.         }); 

看API对android.widget.SimpleAdapter.ViewBinder的解释:

This class can be used by external clients of SimpleAdapter to bind values to views. You should use this class to bind values to views that are not directly supported by SimpleAdapter or to change the way binding occurs for views supported by SimpleAdapter.


你可以使用该类来处理SimpleAdapter不直接支持的数据;


下面来看看完整的代码:

main.xml 中就是一个ListView  , list_item.xml 是为ListView中控件提供布局 ,就是一个ImageView。这两个比较简单,不贴出代码


主要代码   MainActivity.java

view plain copy to clipboard print ?
  1. package com.yin.listView; 
  2. import java.io.IOException; 
  3. import java.io.InputStream; 
  4. import java.net.HttpURLConnection; 
  5. import java.net.MalformedURLException; 
  6. import java.net.URL; 
  7. import java.util.ArrayList; 
  8. import java.util.HashMap; 
  9. import java.util.List; 
  10. import android.app.Activity; 
  11. import android.graphics.Bitmap; 
  12. import android.graphics.BitmapFactory; 
  13. import android.os.Bundle; 
  14. import android.view.LayoutInflater; 
  15. import android.view.View; 
  16. import android.widget.ImageView; 
  17. import android.widget.ListView; 
  18. import android.widget.SimpleAdapter; 
  19. import android.widget.SimpleAdapter.ViewBinder; 
  20. public class MainActivity extends Activity { 
  21.     ListView mListView; 
  22.     View mView; 
  23.     public static final String imageUrl = "http://www.google.com//ig/images/weather/mostly_sunny.gif"
  24.     public void onCreate(Bundle savedInstanceState) { 
  25.         super.onCreate(savedInstanceState); 
  26.         setContentView(R.layout.main); 
  27.          
  28.         mView = LayoutInflater.from(this).inflate(R.layout.list_item,null); 
  29.         mListView = (ListView) findViewById(R.id.mList); 
  30.         List<HashMap<String,Object>> mListData = getListData(); 
  31.          
  32.         SimpleAdapter adapter = new SimpleAdapter(this, mListData, R.layout.list_item, 
  33.                                 new String[]{"icon"}, new int[]{R.id.image}); 
  34.         
  35.         adapter.setViewBinder(new ViewBinder() { 
  36.              
  37.             public boolean setViewValue(View view, Object data, 
  38.                     String textRepresentation) { 
  39.                 //判断是否为我们要处理的对象 
  40.                 if(view instanceof ImageView  && data instanceof Bitmap){ 
  41.                     ImageView iv = (ImageView) view; 
  42.                  
  43.                     iv.setImageBitmap((Bitmap) data); 
  44.                     return true
  45.                 }else 
  46.                 return false
  47.             } 
  48.         }); 
  49.          
  50.         mListView.setAdapter(adapter); 
  51.     } 
  52.     public List<HashMap<String,Object>> getListData(){ 
  53.         List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>(); 
  54.         HashMap<String,Object> map = null
  55.         for(int i=0;i<5;i++){ 
  56.             map = new HashMap<String, Object>(); 
  57.             map.put("icon",getBitmap()); 
  58.             list.add(map); 
  59.         } 
  60.         return list; 
  61.     } 
  62.      
  63.     public Bitmap getBitmap(){ 
  64.         Bitmap mBitmap = null
  65.         try
  66.             URL url = new URL(imageUrl); 
  67.             HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
  68.             InputStream is = conn.getInputStream(); 
  69.             mBitmap = BitmapFactory.decodeStream(is); 
  70.              
  71.         } catch (MalformedURLException e) { 
  72.             e.printStackTrace(); 
  73.         } catch (IOException e) { 
  74.             e.printStackTrace(); 
  75.         } 
  76.          
    复制到剪贴板  XML/HTML代码
    1. <uses-permission android:name="android.permission.INTERNET"> 

  77.         return mBitmap; 
  78.     } 


注意:此例是显示网络图片 要记得添加权限




历史版本1:Android基础篇之在ListView中显示网络图片_第1张图片


→如果您认为本文档还有待完善,请 编辑文档

标签: ListView

你可能感兴趣的:(历史版本1:Android基础篇之在ListView中显示网络图片)