图片异步加载框架(框架)
参考自http://blog.csdn.net/hantangsongming/article/details/41961749 博客

创建继承自Application的类用于在程序最开始初始化图片缓存加载参数 public class UILApplication extends Application { @Override public void onCreate() { super.onCreate(); initImageLoader(getApplicationContext()); } public static void initImageLoader(Context context) { //缓存文件的目录 File cacheDir = StorageUtils.getOwnCacheDirectory(context, "universalimageloader/Cache"); ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context) .memoryCacheExtraOptions(480, 800) // max width, max height,即保存的每个缓存文件的最大长宽 .threadPoolSize(3) //线程池内线程的数量 .threadPriority(Thread.NORM_PRIORITY - 2) .denyCacheImageMultipleSizesInMemory() .diskCacheFileNameGenerator(new Md5FileNameGenerator()) //将保存的时候的URI名称用MD5 加密 .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) .memoryCacheSize(2 * 1024 * 1024) // 内存缓存的最大值 .diskCacheSize(50 * 1024 * 1024) // SD卡缓存的最大值 .tasksProcessingOrder(QueueProcessingType.LIFO) // 由原先的discCache -> diskCache .diskCache(new UnlimitedDiskCache(cacheDir))//自定义缓存路径 .imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间 .writeDebugLogs() // Remove for release app .build(); //全局初始化此配置 ImageLoader.getInstance().init(config); } } 然后创建类Constants 用来获取测试使用的图片url地址 public final class Constants { public static final String[] IMAGES = new String[] { // Heavy images "http://avatar.csdn.net/F/E/4/1_gengkunpeng.jpg", "http://p0.so.qhimg.com/bdr/_240_/t012f2ffd5f9de0538a.jpg", "http://p0.so.qhimg.com/bdr/_240_/t0120bd8cb3b4a3e6c5.jpg", "http://p0.so.qhimg.com/bdr/_240_/t0101bc29746f8aca3f.jpg", "http://p2.so.qhimg.com/bdr/_240_/t0143bb6f176386979c.jpg", // Light images "http://tabletpcssource.com/wp-content/uploads/2011/05/android-logo.png", "http://4.bp.blogspot.com/-LEvwF87bbyU/Uicaskm-g6I/AAAAAAAAZ2c/V-WZZAvFg5I/s800/Pesto+Guacamole+500w+0268.jpg", // Image with "Mark has been invalidated" problem "file:///sdcard/Universal Image Loader @#&=+-_.,!()~'%20.png", // Image from SD card with encoded symbols "assets://Living Things @#&=+-_.,!()~'%20.jpg", // Image from assets "drawable://" + R.drawable.ic_launcher, // Image from drawables "http://upload.wikimedia.org/wikipedia/ru/b/b6/袣邪泻_泻芯褌_褋_屑褘褕邪屑懈_胁芯械胁邪谢.png", // Link with UTF-8 "https://www.eff.org/sites/default/files/chrome150_0.jpg", // Image from HTTPS "http://bit.ly/soBiXr", // Redirect link "http://img001.us.expono.com/100001/100001-1bc30-2d736f_m.jpg", // EXIF "", // Empty link "http://wrong.site.com/corruptedLink", // Wrong link }; } 在mainactivity中 public class MainActivity extends Activity { private ImageLoader imageLoader; private ListView lv; private String[] imageUrls; private DisplayImageOptions options; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageLoader = ImageLoader.getInstance(); lv = (ListView)findViewById(R.id.list); imageUrls = com.example.loadimage.Constants.IMAGES; // 使用DisplayImageOptions.Builder()创建DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_stub) // 设置图片下载期间显示的图片 .showImageForEmptyUri(R.drawable.ic_empty) // 设置图片Uri为空或是错误的时候显示的图片 .showImageOnFail(R.drawable.ic_error) // 设置图片加载或解码过程中发生错误显示的图片 .cacheInMemory(true) // 设置下载的图片是否缓存在内存中 .cacheOnDisk(true) // 设置下载的图片是否缓存在SD卡中 .displayer(new RoundedBitmapDisplayer(20)) // 设置成圆角图片 .build(); // 构建完成 lv.setAdapter(new ItemListAdapter()); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.item_clear_memory_cache: ImageLoader.getInstance().clearMemoryCache(); return true; case R.id.item_clear_disc_cache: ImageLoader.getInstance().clearDiskCache(); return true; default: return false; } } class ItemListAdapter extends BaseAdapter { @Override public int getCount() { // TODO Auto-generated method stub return imageUrls.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return imageUrls[position]; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder = null; if (convertView == null) { convertView = getLayoutInflater().inflate(R.layout.item_list, parent, false); holder = new ViewHolder(); holder.text = (TextView) convertView.findViewById(R.id.text); holder.image = (ImageView) convertView.findViewById(R.id.image); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.text.setText("Item " + (position + 1)); imageLoader.displayImage(imageUrls[position], holder.image, options); return convertView; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } class ViewHolder { public ImageView image; public TextView text; } } }
所需要的jar包以及demo包:http://note.youdao.com/yws/public/redirect/share?id=9ea396c3ed0cd53a9602b607caeccfe2&type=false
浙公网安备 33010602011771号