Android异步加载图片详解之方式二(1)

简介: MainActivity.java如下: package com.cn.perfectlistview; import java.util.ArrayList; import android.

MainActivity.java如下:

package com.cn.perfectlistview;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
//解决的问题:
//1 ListView异步加载网络图片
//2 ListView滑动时,图片错位
public class MainActivity extends Activity {
    private ListView listView;
    MyListViewAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();
    }


    public void initView(){
		listView = (ListView) findViewById(R.id.listView);
		ArrayList<ListViewItem> arrayList = new ArrayList<ListViewItem>();

		ListViewItem item1 = new ListViewItem
		("xxxx","http://images.csdn.net/20121018/zazhi-68-78-1018.jpg");
		arrayList.add(item1);

		ListViewItem item2 = new ListViewItem
		("xxxx","http://info-database.csdn.net/Upload/2012-10-08/zazhi-210-90-1008.jpg");
		arrayList.add(item2);

		ListViewItem item3 = new ListViewItem
		("xxxx","http://images.csdn.net/20121119/20111211223655841.jpg");
		arrayList.add(item3);

		ListViewItem item4 = new ListViewItem
		("xxxx","http://images.csdn.net/20121119/20120619174604972.jpg");
		arrayList.add(item4);

		ListViewItem item5 = new ListViewItem
		("xxxx","http://csdnimg.cn/www/images/pic_foot_report110.png");
		arrayList.add(item5);

		ListViewItem item6 = new ListViewItem
		("xxxx","http://csdnimg.cn/www/images/pic_foot_report.png");
		arrayList.add(item6);

		ListViewItem item7 = new ListViewItem
		("xxxx","http://csdnimg.cn/www/images/pic_foot_BNIA.png");
		arrayList.add(item7);

		ListViewItem item8 = new ListViewItem
		("xxxx","http://csdnimg.cn/www/images/pic_foot_gongshang.png");
		arrayList.add(item8);

		ListViewItem item9 = new ListViewItem
		("xxxx","http://images.csdn.net/20120803/logo-qixing02.jpg");
		arrayList.add(item9);

		ListViewItem item10 = new ListViewItem
		("xxxx","http://images.csdn.net/20120726/quanjing-logo-shouye.jpg");
		arrayList.add(item10);

		ListViewItem item11 = new ListViewItem
		("xxxx","http://images.csdn.net/20120726/nhn-logo-shouye.jpg");
		arrayList.add(item11);

		ListViewItem item12 = new ListViewItem
		("xxxx","http://images.csdn.net/20120510/shanghai-jiaoda-logo.jpg");
		arrayList.add(item12);

		ListViewItem item13 = new ListViewItem
		("xxxx","http://images.csdn.net/20120312/bigman2.gif");
		arrayList.add(item13);

		ListViewItem item14 = new ListViewItem
		("xxxx","http://images.csdn.net/20120216/csdn2.gif");
		arrayList.add(item14);

		ListViewItem item15 = new ListViewItem
		("xxxx","http://images.csdn.net/20121109/win8_100x74.jpg");
		arrayList.add(item15);

		ListViewItem item16 = new ListViewItem
		("xxxx","http://images.csdn.net/20120816/cf-20120816.jpg");
		arrayList.add(item16);

		ListViewItem item17 = new ListViewItem
		("xxxx","http://images.csdn.net/20120704/bi05.jpg");
		arrayList.add(item17);

		ListViewItem item18 = new ListViewItem
		("xxxx","http://images.csdn.net/20120816/amd-20120816.jpg");
		arrayList.add(item18);

		ListViewItem item19 = new ListViewItem
		("xxxx","http://y2.ifengimg.com/2012/12/08/09383369.jpg");
		arrayList.add(item19);

		ListViewItem item20 = new ListViewItem
		("xxxx","http://y1.ifengimg.com/2012/12/08/09320040.jpg");
		arrayList.add(item20);

		ListViewItem item21 = new ListViewItem
		("xxxx","http://y3.ifengimg.com/4ccd6020154f55cb/2012/1207/2.jpg");
		arrayList.add(item21);

		ListViewItem item22 = new ListViewItem
		("xxxx","http://y2.ifengimg.com/2012/12/08/09425571.jpg");
		arrayList.add(item22);

		ListViewItem item23 = new ListViewItem
		("xxxx","http://y2.ifengimg.com/b19fadab6fcc0645/2012/1011/123.jpg");
		arrayList.add(item23);

		ListViewItem item24 = new ListViewItem
		("xxxx","http://y2.ifengimg.com/2012/11/15/07414772.jpg");
		arrayList.add(item24);

		ListViewItem item25 = new ListViewItem
		("xxxx","http://y2.ifengimg.com/2012/11/15/07532374.jpg");
		arrayList.add(item25);

		ListViewItem item26 = new ListViewItem
		("xxxx","http://y0.ifengimg.com/2012/11/15/07583017.jpg");
		arrayList.add(item26);

		ListViewItem item27 = new ListViewItem
		("xxxx","http://y0.ifengimg.com/2012/02/20/14175732.jpg");
		arrayList.add(item27);

		ListViewItem item28 = new ListViewItem
		("xxxx","http://y2.ifengimg.com/2012/10/29/21175274.jpg");
		arrayList.add(item28);

		ListViewItem item29 = new ListViewItem
		("xxxx","http://res.news.ifeng.com/2727e09814adca26/2011/1221/rdn_4ef0fb0333564.jpg");
		arrayList.add(item29);

		ListViewItem item30 = new ListViewItem
		("xxxx","http://y1.ifengimg.com/2012/12/04/10060551.jpg");
		arrayList.add(item30);

		ListViewItem item31 = new ListViewItem
		("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec068537d8f6.jpg");
		arrayList.add(item31);

		ListViewItem item32 = new ListViewItem
		("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec068554fa3f.jpg");
		arrayList.add(item32);

		ListViewItem item33 = new ListViewItem
		("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec06853bd85b.jpg");
		arrayList.add(item33);

		ListViewItem item34 = new ListViewItem
		("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec06854ace88.jpg");
		arrayList.add(item34);

		ListViewItem item35 = new ListViewItem
		("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec06855bad01.jpg");
		arrayList.add(item35);

		ListViewItem item36 = new ListViewItem
		("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec0685649c4e.jpg");
		arrayList.add(item36);

		ListViewItem item37 = new ListViewItem
		("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec068562ac3d.jpg");
		arrayList.add(item37);

		ListViewItem item38 = new ListViewItem
		("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec06854dc836.jpg");
		arrayList.add(item38);

		ListViewItem item39 = new ListViewItem
		("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec06855ef0ea.jpg");
		arrayList.add(item39);

		ListViewItem item40 = new ListViewItem
		("xxxx","http://res.news.ifeng.com/b77953a0c6aae8fb/2011/1114/rdn_4ec068568beda.jpg");
		arrayList.add(item40);
		

		adapter = new MyListViewAdapter(MainActivity.this, arrayList);
		
		listView.setAdapter(adapter);
	}
}


MyListViewAdapter.java如下:

package com.cn.perfectlistview;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn.loadImages.ImageDownloader;
public class MyListViewAdapter extends BaseAdapter{
      private ArrayList<ListViewItem> mArrayList;
      private Context mContext;
      private ImageDownloader imageDownloader;
      public MyListViewAdapter(Context mContext,ArrayList<ListViewItem> mArrayList) {
  		super();
  		this.mArrayList = mArrayList;
  		this.mContext = mContext;
  		String folderNameOnSDCard="images";
  		imageDownloader=new ImageDownloader(mContext, folderNameOnSDCard);
  		
  	  
  	}

	public int getCount() {
		if (mArrayList==null) {
			return 0;
		} else {
			return mArrayList.size();
		}
		
	}

	public Object getItem(int position) {
		if (mArrayList==null) {
			return null;
		} else {
            return mArrayList.get(position);
		}
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder=null;
		if (convertView==null) {
			holder=new ViewHolder();
			convertView=LayoutInflater.from(this.mContext).inflate(R.layout.listviewitem, null, false);
			holder.textView=(TextView) convertView.findViewById(R.id.textView);
		    holder.imageView=(ImageView) convertView.findViewById(R.id.imageview);
		    convertView.setTag(holder);
		} else {
            holder=(ViewHolder) convertView.getTag();
		}
		
	
		
		if (this.mArrayList!=null) {
			ListViewItem listViewItem=this.mArrayList.get(position);
			if (holder.textView!=null) {
				holder.textView.setText(listViewItem.getContent());
			}
			if (holder.imageView!=null) {
				try {
					String imageUrl=listViewItem.getImageURL();
					//加载网络上的图片
					imageDownloader.download(imageUrl, holder.imageView);
				} catch (Exception e) {
					e.printStackTrace();
				}
					
			}
		}	    
		

		return convertView;
	}

	
	private class ViewHolder{
		ImageView imageView;
		TextView textView;
	}
      
}


ListViewItem.java如下:

package com.cn.perfectlistview;
public class ListViewItem {
	String content;
	String imageURL;

	public ListViewItem(String content, String imageURL) {
		super();
		this.content = content;
		this.imageURL = imageURL;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getImageURL() {
		return imageURL;
	}

	public void setImageURL(String imageURL) {
		this.imageURL = imageURL;
	}

}


 

相关文章
|
6月前
|
XML Java Android开发
Android Studio App开发之对图片进行简单加工(包括放缩,旋转等等 附源码)
Android Studio App开发之对图片进行简单加工(包括放缩,旋转等等 附源码)
102 0
|
6月前
|
XML Java Android开发
Android Studio App开发之使用相机拍摄照片和从相册中选取图片(附源码 超详细必看)
Android Studio App开发之使用相机拍摄照片和从相册中选取图片(附源码 超详细必看)
793 0
|
2月前
|
存储 缓存 编解码
Android经典面试题之图片Bitmap怎么做优化
本文介绍了图片相关的内存优化方法,包括分辨率适配、图片压缩与缓存。文中详细讲解了如何根据不同分辨率放置图片资源,避免图片拉伸变形;并通过示例代码展示了使用`BitmapFactory.Options`进行图片压缩的具体步骤。此外,还介绍了Glide等第三方库如何利用LRU算法实现高效图片缓存。
61 20
Android经典面试题之图片Bitmap怎么做优化
|
6月前
|
Android开发
Android通过手势(多点)缩放和拖拽图片
Android通过手势(多点)缩放和拖拽图片
52 4
|
6月前
|
Java Android开发
android 下载图片的问题
android 下载图片的问题
44 3
|
3月前
|
数据处理 开发工具 数据安全/隐私保护
Android平台RTMP推送|轻量级RTSP服务|GB28181接入之文字、png图片水印的精进之路
本文探讨了Android平台上推流模块中添加文字与PNG水印的技术演进。自2015年起,为了满足应急指挥及安防领域的需求,逐步发展出三代水印技术:第一代为静态文字与图像水印;第二代实现了动态更新水印内容的能力,例如实时位置与时间信息;至第三代,则优化了数据传输效率,直接使用Bitmap对象传递水印数据至JNI层,减少了内存拷贝次数。这些迭代不仅提升了用户体验和技术效率,也体现了开发者追求极致与不断创新的精神。
|
3月前
|
自然语言处理 定位技术 API
Android经典实战之如何获取图片的经纬度以及如何根据经纬度获取对应的地点名称
本文介绍如何在Android中从图片提取地理位置信息并转换为地址。首先利用`ExifInterface`获取图片内的经纬度,然后通过`Geocoder`将经纬度转为地址。注意操作需在子线程进行且考虑多语言支持。
218 4
|
3月前
|
XML 前端开发 Android开发
Android经典实战之Kotlin中实现圆角图片和圆形图片
本文介绍两种实现圆角图像视图的方法。第一种是通过自定义Kotlin `AppCompatImageView`,重写`onDraw`方法使用`Canvas`和`Path`进行圆角剪裁。第二种利用Android Material库中的`ShapeableImageView`,简单配置即可实现圆角效果。两种方法均易于实现且提供动态调整圆角半径的功能。
65 0
|
5月前
|
JSON 编解码 Apache
Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
57 1
|
5月前
|
Java Android开发
18. 【Android教程】图片控件 ImageView
18. 【Android教程】图片控件 ImageView
79 4
下一篇
无影云桌面