package com.example.imagelistview;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.app.Activity;
import android.graphics.Bitmap;
public class MainActivity extends Activity {
private ListView listView;
private List<String> urlList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取ListView控件
listView = (ListView) findViewById(R.id.lv);
urlList = new ArrayList<String>();
//添加图片到list集合中去。
urlList.add("http://192.168.12.93:8080/1.png");
urlList.add("http://192.168.12.93:8080/2.png");
urlList.add("http://192.168.12.93:8080/3.png");
urlList.add("http://192.168.12.93:8080/4.png");
urlList.add("http://192.168.12.93:8080/5.png");
urlList.add("http://192.168.12.93:8080/6.png");
urlList.add("http://192.168.12.93:8080/7.png");
urlList.add("http://192.168.12.93:8080/8.png");
urlList.add("http://192.168.12.93:8080/9.png");
urlList.add("http://192.168.12.93:8080/10.png");
urlList.add("http://192.168.12.93:8080/11.png");
urlList.add("http://192.168.12.93:8080/12.png");
urlList.add("http://192.168.12.93:8080/13.png");
urlList.add("http://192.168.12.93:8080/14.png");
//给ListView设置适配器
listView.setAdapter(new MyAdapter());
}
//继承自BaseAdapter的适配器
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
//返回list的数目
return urlList.size();
}
@Override
public Object getItem(int position) {
return urlList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//为了优化ListView,所以定义一个类用来存放ListView中要用到的所有控件,这样就不用
//每次用到的时候去一次次查找。
ViewHolder viewHolder = null;
if(convertView == null){
viewHolder = new ViewHolder();
//将字条目的布局填充到ListView中去
convertView = View.inflate(getApplicationContext(), R.layout.item, null);
viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv);
//这一步必不可少
convertView.setTag(viewHolder);
}else{
//这一步必不可少
viewHolder = (ViewHolder) convertView.getTag();
}
//解决图片错位和图片闪烁的方法:为每一ImageView设置标志
viewHolder.iv.setTag(urlList.get(position));
//为每一个ImageView设置默认的背景。
viewHolder.iv.setImageResource(R.drawable.ic_launcher);
new NetImageLoader() {
@Override
public void loadImage(ImageView imageView, Bitmap bitmap) {
//图片错误,闪烁
if(imageView.getTag().equals(urlList.get(position))){
imageView.setImageBitmap(bitmap);
}
}
}.downLoadImage(urlList.get(position), viewHolder.iv);
//开启一个线程去下载相应图片
return convertView;
}
}
class ViewHolder{
ImageView iv;
}
}
package com.example.imagelistview;
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;
public abstract class NetImageLoader {
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
Bitmap bitmap = (Bitmap) msg.obj;
//回调
loadImage(imageView, bitmap);
};
};
public abstract void loadImage(ImageView imageView,Bitmap bitmap);
private ImageView imageView;
public void downLoadImage(final String imgUrl,ImageView imageView){
this.imageView = imageView;
new Thread(){
public void run() {
try {
URL url = new URL(imgUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
InputStream is = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
sleep(3000);
Message message = new Message();
message.obj = bitmap;
handler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
}
<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=".MainActivity" >
<ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
<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=".MainActivity" >
<ImageView
android:id="@+id/iv"
android:layout_width="60dp"
android:layout_height="60dp"/>
</RelativeLayout>
package com.example.imagelistview;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.app.Activity;
import android.graphics.Bitmap;
public class MainActivity extends Activity {
private ListView listView;
private List<String> urlList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取ListView控件
listView = (ListView) findViewById(R.id.lv);
urlList = new ArrayList<String>();
//添加图片到list集合中去。
urlList.add("http://192.168.12.93:8080/1.png");
urlList.add("http://192.168.12.93:8080/2.png");
urlList.add("http://192.168.12.93:8080/3.png");
urlList.add("http://192.168.12.93:8080/4.png");
urlList.add("http://192.168.12.93:8080/5.png");
urlList.add("http://192.168.12.93:8080/6.png");
urlList.add("http://192.168.12.93:8080/7.png");
urlList.add("http://192.168.12.93:8080/8.png");
urlList.add("http://192.168.12.93:8080/9.png");
urlList.add("http://192.168.12.93:8080/10.png");
urlList.add("http://192.168.12.93:8080/11.png");
urlList.add("http://192.168.12.93:8080/12.png");
urlList.add("http://192.168.12.93:8080/13.png");
urlList.add("http://192.168.12.93:8080/14.png");
//给ListView设置适配器
listView.setAdapter(new MyAdapter());
}
//继承自BaseAdapter的适配器
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
//返回list的数目
return urlList.size();
}
@Override
public Object getItem(int position) {
return urlList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//为了优化ListView,所以定义一个类用来存放ListView中要用到的所有控件,这样就不用
//每次用到的时候去一次次查找。
ViewHolder viewHolder = null;
if(convertView == null){
viewHolder = new ViewHolder();
//将字条目的布局填充到ListView中去
convertView = View.inflate(getApplicationContext(), R.layout.item, null);
viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv);
//这一步必不可少
convertView.setTag(viewHolder);
}else{
//这一步必不可少
viewHolder = (ViewHolder) convertView.getTag();
}
//解决图片错位和图片闪烁的方法:为每一ImageView设置标志
viewHolder.iv.setTag(urlList.get(position));
//为每一个ImageView设置默认的背景。
viewHolder.iv.setImageResource(R.drawable.ic_launcher);
new NetImageLoader() {
@Override
public void loadImage(ImageView imageView, Bitmap bitmap) {
//图片错误,闪烁
if(imageView.getTag().equals(urlList.get(position))){
imageView.setImageBitmap(bitmap);
}
}
}.downLoadImage(urlList.get(position), viewHolder.iv);
//开启一个线程去下载相应图片
return convertView;
}
}
class ViewHolder{
ImageView iv;
}
}