項目開發中我們需要從網絡獲取圖片顯示到控件中,很多開源框架如Picasso可以實現圖片下載和緩存功能。這里介紹的是一種簡易的網絡圖片獲取方式並把它顯示到ListView中。
本案例實現的效果如下:

項目結構:

根據部分開源代碼,我修改並封裝了一個網絡圖片加載的工具類GetImageByUrl,通過調用其中的setImage方法,傳入待顯示圖片的ImageView控件和該圖片的url路徑這兩個參數即可實現獲取網絡圖片的功能。
GetImageByUrl.java
package com.leo.imagelistview.util;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
/** * 根據圖片url路徑獲取圖片 * * @author LeoLeoHan * */
public class GetImageByUrl {
private PicHandler pic_hdl;
private ImageView imgView;
private String url;
/** * 通過圖片url路徑獲取圖片並顯示到對應控件上 * * @param imgView * @param url */
public void setImage(ImageView imgView, String url) {
this.url = url;
this.imgView = imgView;
pic_hdl = new PicHandler();
Thread t = new LoadPicThread();
t.start();
}
class LoadPicThread extends Thread {
@Override
public void run() {
Bitmap img = getUrlImage(url);
System.out.println(img + "---");
Message msg = pic_hdl.obtainMessage();
msg.what = 0;
msg.obj = img;
pic_hdl.sendMessage(msg);
}
}
class PicHandler extends Handler {
@Override
public void handleMessage(Message msg) {
Bitmap myimg = (Bitmap) msg.obj;
imgView.setImageBitmap(myimg);
}
}
public Bitmap getUrlImage(String url) {
Bitmap img = null;
try {
URL picurl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) picurl
.openConnection();
conn.setConnectTimeout(6000);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.connect();
InputStream is = conn.getInputStream();
img = BitmapFactory.decodeStream(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return img;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
下面的代碼就是實現ListView顯示網絡圖片。
首先創建一個自定義的布局文件images_item.xml和自定義的ImageAdapter。
images_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" >
<ImageView android:id="@+id/iv_image" android:layout_width="80dp" android:layout_height="80dp" />
<LinearLayout android:layout_width="0dp" android:layout_height="80dp" android:layout_weight="1" android:orientation="vertical" >
<TextView android:id="@+id/tv_url" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
ImageAdapter.java
package com.leo.imagelistview.adapter;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.leo.imagelistview.R;
import com.leo.imagelistview.util.GetImageByUrl;
/** * * @author LeoLeoHan * */
public class ImageAdapter extends BaseAdapter {
// 要顯示的數據的集合
private List<Map<String, Object>> data;
// 接受上下文
private Context context;
// 聲明內部類對象
private ViewHolder viewHolder;
/** * 構造函數 * * @param context * @param data */
public ImageAdapter(Context context, List<Map<String, Object>> data) {
this.context = context;
this.data = data;
}
// 返回的總個數
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
// 返回每個條目對應的數據
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
// 返回的id
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
// 返回這個條目對應的控件對象
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 判斷當前條目是否為null
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = View.inflate(context, R.layout.images_item, null);
viewHolder.iv_image = (ImageView) convertView
.findViewById(R.id.iv_image);
viewHolder.tv_url = (TextView) convertView
.findViewById(R.id.tv_url);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// 獲取List集合中的map對象
Map<String, Object> map = data.get(position);
// 獲取圖片的url路徑
String url = map.get("url").toString();
// 這里調用了圖片加載工具類的setImage方法將圖片直接顯示到控件上
GetImageByUrl getImageByUrl = new GetImageByUrl();
getImageByUrl.setImage(viewHolder.iv_image, url);
viewHolder.tv_url.setText(url);
return convertView;
}
/** * 內部類 記錄單個條目中所有屬性 * * @author LeoLeoHan * */
class ViewHolder {
public ImageView iv_image;
public TextView tv_url;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
MainActivity.java
package com.leo.imagelistview;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.leo.imagelistview.adapter.ImageAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.widget.BaseAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
private List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
//聲明控件
private ListView lv_images;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 獲取ListView對象
lv_images = (ListView) findViewById(R.id.lv_images);
//獲取數據
getData();
BaseAdapter adapter = new ImageAdapter(this, data);
//設置適配器
lv_images.setAdapter(adapter);
}
/** * 簡單添加一些網絡圖片的url路徑 * 實際開發中url路徑是從服務器中解析json數據 */
public void getData() {
String url1 = "http://my.csdn.net/uploads/avatar/6/A/7/1_leoleohan.jpg";
String url2 = "http://img1.cache.netease.com/men/2014/6/2/2014060213070843617.jpg";
String url3 = "http://static2.businessinsider.com/image/522f7a076da811e1404b39a9-480-/jony-ive-9.png";
String url4 = "http://y0.ifengimg.com/8e16f14474b61551/2013/0731/rdn_51f878236017c.jpg";
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("url", url1);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("url", url2);
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("url", url3);
Map<String, Object> map4 = new HashMap<String, Object>();
map4.put("url", url4);
data.add(map1);
data.add(map2);
data.add(map3);
data.add(map4);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" >
<ListView android:id="@+id/lv_images" android:layout_width="match_parent" android:layout_height="match_parent" />
</RelativeLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
轉載來源:https://blog.csdn.net/LeoLeoHan/article/details/46553317
