Android基礎篇之在ListView中顯示網絡圖片

 
你們知道ImageView 類雖然有一個 setImageUri 方法,但不能直接接受一個由網絡地址生成的uri做爲參數從而顯示圖片,咱們只好使用其 setImageBitmap 方法,如
 
Bitmap mBitmap = null;
        
URL url = new URL(p_w_picpathUrl);
        
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        
InputStream is = conn.getInputStream();
        
mBitmap = BitmapFactory.decodeStream(is);
 
 在SimpleAdapter中須要一個數據源,用來存儲數據的,在顯示圖片時咱們要用HashMap<>存儲一個Bitmap;但僅存取了Bitmap時在ListView中是沒法顯示圖片的,咱們須要對SimpleAdapter進行處理 。
以下是對SimpleAdaptr處理的主要代碼
 
adapter.setViewBinder( new ViewBinder() {
        
       public boolean setViewValue(View view, Object data,
          String textRepresentation) {
         //判斷是否爲咱們要處理的對象
         if(view instanceof ImageView    && data instanceof Bitmap){
          ImageView iv = (ImageView) view;
        
          iv.setImageBitmap((Bitmap) data);
           return true;
        } else
         return false;
      }
    });
 
 
看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
 
package com.yin.listView;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
public class MainActivity extends Activity {
ListView mListView;
View mView;
public static final String p_w_picpathUrl = "http://www.google.com//ig/p_w_picpaths/weather/mostly_sunny.gif";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mView = LayoutInflater.from(this).inflate(R.layout.list_item,null);
mListView = (ListView) findViewById(R.id.mList);
List<HashMap<String,Object>> mListData = getListData();

SimpleAdapter adapter = new SimpleAdapter(this, mListData, R.layout.list_item,
new String[]{"icon"}, new int[]{R.id.p_w_picpath});

adapter.setViewBinder(new ViewBinder() {

public boolean setViewValue(View view, Object data,
String textRepresentation) {
//判斷是否爲咱們要處理的對象
if(view instanceof ImageView && data instanceof Bitmap){
ImageView iv = (ImageView) view;

iv.setImageBitmap((Bitmap) data);
return true;
}else
return false;
}
});

mListView.setAdapter(adapter);
}
public List<HashMap<String,Object>> getListData(){
List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> map = null;
for(int i=0;i<5;i++){
map = new HashMap<String, Object>();
map.put("icon",getBitmap());
list.add(map);
}
return list;
}

public Bitmap getBitmap(){
Bitmap mBitmap = null;
try {
URL url = new URL(p_w_picpathUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream is = conn.getInputStream();
mBitmap = BitmapFactory.decodeStream(is);

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return mBitmap;
}
}
 
注意:此例是顯示網絡圖片 要記得添加權限
<uses-permission android:name="android.permission.INTERNET">
相關文章
相關標籤/搜索