Android开源框架ImageLoader的完美例子

懵懂的女人 提交于 2020-01-07 00:46:26
本文转载自:https://www.cnblogs.com/zgz345/p/3502315.html 作者:zgz345 转载请注明该声明。

很多人都在讨论如何让图片能在异步加载更加流畅,可以显示大量图片,在拖动ListView的时候不会出现卡的现象。关于ImageLoader这个开源框架的使用有很多网友都介绍过,不过还不够清楚,这里有一个关于这个开源项目的完美例子,ListView的图片加载、GridView的图片加载、ViewPager的图片加载、Gallery画廊的图片加载、Widget的使用。很完善的一个例子,在这里我把所有界面效果做出博客分享出来,需要源码的朋友到我的资源页下载

下载地址:http://download.csdn.net/detail/wwj_748/5975847

 

 

要使用ImageLoader就要到这里下载jar包:

https://github.com/nostra13/Android-Universal-Image-Loader

然后导入项目中去就行了

项目文档结构图:


从界面说起,界面本身是没什么好说的,就是如何在xml当中进行定义罢了

有以下这么多个布局文件


一个一个来看呗

首先是这样的效果

这个在Android4.2.2比较好看,在Android2.3.3就显得比较挫。

 

/2013.8.19_Universal_Image_Loader_Demo/res/layout/ac_home.xml

 

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" >  
  5.   
  6.     <LinearLayout  
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent"  
  9.         android:orientation="vertical" >  
  10.   
  11.         <TextView  
  12.             android:layout_width="fill_parent"  
  13.             android:layout_height="wrap_content"  
  14.             android:gravity="center"  
  15.             android:paddingBottom="10dip"  
  16.             android:paddingTop="20dip"  
  17.             android:text="@string/label_activity_examples"  
  18.             android:textSize="24sp" />  
  19.   
  20.         <Button  
  21.             android:layout_width="fill_parent"  
  22.             android:layout_height="wrap_content"  
  23.             android:layout_margin="10dip"  
  24.             android:onClick="onImageListClick"  
  25.             android:text="@string/button_image_list" />  
  26.   
  27.         <Button  
  28.             android:layout_width="fill_parent"  
  29.             android:layout_height="wrap_content"  
  30.             android:layout_margin="10dip"  
  31.             android:onClick="onImageGridClick"  
  32.             android:text="@string/button_image_grid" />  
  33.   
  34.         <Button  
  35.             android:layout_width="fill_parent"  
  36.             android:layout_height="wrap_content"  
  37.             android:layout_margin="10dip"  
  38.             android:onClick="onImagePagerClick"  
  39.             android:text="@string/button_image_pager" />  
  40.   
  41.         <Button  
  42.             android:layout_width="fill_parent"  
  43.             android:layout_height="wrap_content"  
  44.             android:layout_margin="10dip"  
  45.             android:onClick="onImageGalleryClick"  
  46.             android:text="@string/button_image_gallery" />  
  47.     </LinearLayout>  
  48.   
  49. </ScrollView>  


列表异步加载图片效果

 

/2013.8.19_Universal_Image_Loader_Demo/res/layout/ac_image_list.xml

 

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ListView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@android:id/list"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent" />  


/2013.8.19_Universal_Image_Loader_Demo/res/layout/item_list_image.xml

 

 

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content" >  
  5.   
  6.     <ImageView  
  7.         android:id="@+id/image"  
  8.         android:layout_width="72dip"  
  9.         android:layout_height="72dip"  
  10.         android:layout_margin="3dip"  
  11.         android:adjustViewBounds="true"  
  12.         android:contentDescription="@string/descr_image"  
  13.         android:scaleType="centerCrop" />  
  14.   
  15.     <TextView  
  16.         android:id="@+id/text"  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_gravity="left|center_vertical"  
  20.         android:layout_marginLeft="20dip"  
  21.         android:textSize="22sp" />  
  22.   
  23. </LinearLayout>  


GridView异步加载图片显示

 

 

/2013.8.19_Universal_Image_Loader_Demo/res/layout/ac_image_grid.xml

 

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <GridView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/gridview"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:gravity="center"  
  7.     android:horizontalSpacing="4dip"  
  8.     android:numColumns="3"  
  9.     android:stretchMode="columnWidth"  
  10.     android:verticalSpacing="4dip"  
  11.     android:padding="4dip" />  



 

/2013.8.19_Universal_Image_Loader_Demo/res/layout/item_grid_image.xml

 

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ImageView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/image"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="120dip"  
  6.     android:adjustViewBounds="true"  
  7.     android:contentDescription="@string/descr_image"  
  8.     android:scaleType="centerCrop" />  



 

ViewPager异步加载图片显示

/2013.8.19_Universal_Image_Loader_Demo/res/layout/ac_image_pager.xml

 

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/pager"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent" />  


/2013.8.19_Universal_Image_Loader_Demo/res/layout/item_pager_image.xml

 

 

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:padding="1dip" >  
  6.   
  7.     <ImageView  
  8.         android:id="@+id/image"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_gravity="center"  
  12.         android:adjustViewBounds="true"  
  13.         android:contentDescription="@string/descr_image" />  
  14.   
  15.     <ProgressBar  
  16.         android:id="@+id/loading"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_gravity="center"  
  20.         android:visibility="gone" />  
  21.   
  22. </FrameLayout>  



 

Gallery画廊异步加载图片显示

/2013.8.19_Universal_Image_Loader_Demo/res/layout/ac_image_gallery.xml

 

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Gallery xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/gallery"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="wrap_content"  
  6.     android:layout_gravity="center_vertical"  
  7.     android:spacing="1dip" />  



 

/2013.8.19_Universal_Image_Loader_Demo/res/layout/item_gallery_image.xml

 

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ImageView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/image"  
  4.     android:layout_width="120dip"  
  5.     android:layout_height="120dip"  
  6.     android:layout_gravity="center"  
  7.     android:adjustViewBounds="true"  
  8.     android:contentDescription="@string/descr_image"  
  9.     android:scaleType="centerCrop" />  



 

还有一个就是桌面小部件

 

以上只是布局文件,没有什么可以说,具体Activity代码实现如下:

 

先是这个:

/2013.8.19_Universal_Image_Loader_Demo/src/com/nostra13/example/universalimageloader/HomeActivity.java

主界面Activity代码

 

[java]  view plain copy print ?
  1. /******************************************************************************* 
  2.  * Copyright 2011-2013 Sergey Tarasevich 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  * http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  *******************************************************************************/  
  16. package com.nostra13.example.universalimageloader;  
  17.   
  18. import static com.nostra13.example.universalimageloader.Constants.IMAGES;  
  19.   
  20. import java.io.File;  
  21. import java.io.FileOutputStream;  
  22. import java.io.IOException;  
  23. import java.io.InputStream;  
  24.   
  25. import android.content.Intent;  
  26. import android.os.Bundle;  
  27. import android.view.View;  
  28.   
  29. import com.nostra13.example.universalimageloader.Constants.Extra;  
  30. import com.nostra13.universalimageloader.utils.L;  
  31.   
  32. /** 
  33.  * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) 
  34.  */  
  35. public class HomeActivity extends BaseActivity {  
  36.   
  37.     private static final String TEST_FILE_NAME = "Universal Image Loader @#&=+-_.,!()~'%20.png";  
  38.   
  39.     @Override  
  40.     public void onCreate(Bundle savedInstanceState) {  
  41.         super.onCreate(savedInstanceState);  
  42.         setContentView(R.layout.ac_home);  
  43.   
  44.         // 定义文件对象,目录:/mnt/sdcard, 文件名:TEST_FILE_NAME  
  45.         File testImageOnSdCard = new File("/mnt/sdcard", TEST_FILE_NAME);  
  46.         if (!testImageOnSdCard.exists()) {  // 如果文件不存在  
  47.             // 把文件复制到SD卡  
  48.             copyTestImageToSdCard(testImageOnSdCard);  
  49.         }  
  50.     }  
  51.   
  52.     // 点击进入ListView展示界面  
  53.     public void onImageListClick(View view) {  
  54.         Intent intent = new Intent(this, ImageListActivity.class);  
  55.         intent.putExtra(Extra.IMAGES, IMAGES);  
  56.         startActivity(intent);  
  57.     }  
  58.   
  59.     // 点击进入GridView展示界面  
  60.     public void onImageGridClick(View view) {  
  61.         Intent intent = new Intent(this, ImageGridActivity.class);  
  62.         intent.putExtra(Extra.IMAGES, IMAGES);  
  63.         startActivity(intent);  
  64.     }  
  65.   
  66.     // 点击进入ViewPager展示界面  
  67.     public void onImagePagerClick(View view) {  
  68.         Intent intent = new Intent(this, ImagePagerActivity.class);  
  69.         intent.putExtra(Extra.IMAGES, IMAGES);  
  70.         startActivity(intent);  
  71.     }  
  72.   
  73.     // 点击进入画廊展示界面  
  74.     public void onImageGalleryClick(View view) {  
  75.         Intent intent = new Intent(this, ImageGalleryActivity.class);  
  76.         intent.putExtra(Extra.IMAGES, IMAGES);  
  77.         startActivity(intent);  
  78.     }  
  79.   
  80.     @Override  
  81.     public void onBackPressed() {  
  82.         imageLoader.stop();     // 停止加载图片  
  83.         super.onBackPressed();  
  84.     }  
  85.   
  86.     /** 
  87.      * 开一个线程把assert目录下的图片复制到SD卡目录下 
  88.      * @param testImageOnSdCard 
  89.      */  
  90.     private void copyTestImageToSdCard(final File testImageOnSdCard) {  
  91.         new Thread(new Runnable() {  
  92.             @Override  
  93.             public void run() {  
  94.                 try {  
  95.                     InputStream is = getAssets().open(TEST_FILE_NAME);  
  96.                     FileOutputStream fos = new FileOutputStream(testImageOnSdCard);  
  97.                     byte[] buffer = new byte[8192];  
  98.                     int read;  
  99.                     try {  
  100.                         while ((read = is.read(buffer)) != -1) {  
  101.                             fos.write(buffer, 0, read); // 写入输出流  
  102.                         }  
  103.                     } finally {  
  104.                         fos.flush();        // 写入SD卡  
  105.                         fos.close();        // 关闭输出流  
  106.                         is.close();         // 关闭输入流  
  107.                     }  
  108.                 } catch (IOException e) {  
  109.                     L.w("Can't copy test image onto SD card");  
  110.                 }  
  111.             }  
  112.         }).start();     // 启动线程  
  113.     }  
  114. }  


/2013.8.19_Universal_Image_Loader_Demo/src/com/nostra13/example/universalimageloader/BaseActivity.java

 

 

[java]  view plain copy print ?
  1. /******************************************************************************* 
  2.  * Copyright 2011-2013 Sergey Tarasevich 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  * http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  *******************************************************************************/  
  16. package com.nostra13.example.universalimageloader;  
  17.   
  18. import android.app.Activity;  
  19. import android.view.Menu;  
  20. import android.view.MenuItem;  
  21.   
  22. import com.nostra13.universalimageloader.core.ImageLoader;  
  23.   
  24. /** 
  25.  * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) 
  26.  */  
  27. public abstract class BaseActivity extends Activity {  
  28.   
  29.     protected ImageLoader imageLoader = ImageLoader.getInstance();  
  30.   
  31.     @Override  
  32.     public boolean onCreateOptionsMenu(Menu menu) {  
  33.         // 加载菜单  
  34.         getMenuInflater().inflate(R.menu.main_menu, menu);  
  35.         return true;  
  36.     }  
  37.   
  38.     @Override  
  39.     public boolean onOptionsItemSelected(MenuItem item) {  
  40.         switch (item.getItemId()) {  
  41.             case R.id.item_clear_memory_cache:  
  42.                 imageLoader.clearMemoryCache();     // 清除内存缓存  
  43.                 return true;  
  44.             case R.id.item_clear_disc_cache:  
  45.                 imageLoader.clearDiscCache();       // 清除SD卡中的缓存  
  46.                 return true;  
  47.             default:  
  48.                 return false;  
  49.         }  
  50.     }  
  51. }  



 

/2013.8.19_Universal_Image_Loader_Demo/src/com/nostra13/example/universalimageloader/AbsListViewBaseActivity.java

 

[java]  view plain copy print ?
  1. /******************************************************************************* 
  2.  * Copyright 2011-2013 Sergey Tarasevich 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  * http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  *******************************************************************************/  
  16. package com.nostra13.example.universalimageloader;  
  17.   
  18. import android.os.Bundle;  
  19. import android.view.Menu;  
  20. import android.view.MenuItem;  
  21. import android.widget.AbsListView;  
  22.   
  23. import com.nostra13.universalimageloader.core.assist.PauseOnScrollListener;  
  24.   
  25. /** 
  26.  *  
  27.  *  
  28.  * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) 
  29.  */  
  30. public class AbsListViewBaseActivity extends BaseActivity {  
  31.   
  32.     protected static final String STATE_PAUSE_ON_SCROLL = "STATE_PAUSE_ON_SCROLL";  
  33.     protected static final String STATE_PAUSE_ON_FLING = "STATE_PAUSE_ON_FLING";  
  34.   
  35.     protected AbsListView listView;  
  36.   
  37.     protected boolean pauseOnScroll = false;  
  38.     protected boolean pauseOnFling = true;  
  39.   
  40.     @Override  
  41.     public void onRestoreInstanceState(Bundle savedInstanceState) {  
  42.         pauseOnScroll = savedInstanceState.getBoolean(STATE_PAUSE_ON_SCROLL, false);  
  43.         pauseOnFling = savedInstanceState.getBoolean(STATE_PAUSE_ON_FLING, true);  
  44.     }  
  45.   
  46.     @Override  
  47.     public void onResume() {  
  48.         super.onResume();  
  49.         applyScrollListener();  
  50.     }  
  51.   
  52.     private void applyScrollListener() {  
  53.         listView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));  
  54.     }  
  55.   
  56.     @Override  
  57.     public void onSaveInstanceState(Bundle outState) {  
  58.         outState.putBoolean(STATE_PAUSE_ON_SCROLL, pauseOnScroll);  
  59.         outState.putBoolean(STATE_PAUSE_ON_FLING, pauseOnFling);  
  60.     }  
  61.   
  62.     @Override  
  63.     public boolean onPrepareOptionsMenu(Menu menu) {  
  64.         MenuItem pauseOnScrollItem = menu.findItem(R.id.item_pause_on_scroll);  
  65.         pauseOnScrollItem.setVisible(true);  
  66.         pauseOnScrollItem.setChecked(pauseOnScroll);  
  67.   
  68.         MenuItem pauseOnFlingItem = menu.findItem(R.id.item_pause_on_fling);  
  69.         pauseOnFlingItem.setVisible(true);  
  70.         pauseOnFlingItem.setChecked(pauseOnFling);  
  71.         return true;  
  72.     }  
  73.   
  74.     @Override  
  75.     public boolean onOptionsItemSelected(MenuItem item) {  
  76.         switch (item.getItemId()) {  
  77.             case R.id.item_pause_on_scroll:  
  78.                 pauseOnScroll = !pauseOnScroll;  
  79.                 item.setChecked(pauseOnScroll);  
  80.                 applyScrollListener();  
  81.                 return true;  
  82.             case R.id.item_pause_on_fling:  
  83.                 pauseOnFling = !pauseOnFling;  
  84.                 item.setChecked(pauseOnFling);  
  85.                 applyScrollListener();  
  86.                 return true;  
  87.             default:  
  88.                 return super.onOptionsItemSelected(item);  
  89.         }  
  90.     }  
  91. }  



 

/2013.8.19_Universal_Image_Loader_Demo/src/com/nostra13/example/universalimageloader/Constants.java

常量类代码

 

[java]  view plain copy print ?
  1. /******************************************************************************* 
  2.  * Copyright 2011-2013 Sergey Tarasevich 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  * http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  *******************************************************************************/  
  16. package com.nostra13.example.universalimageloader;  
  17.   
  18. /** 
  19.  * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) 
  20.  * 常量类 
  21.  */  
  22. public final class Constants {  
  23.   
  24.     // 一堆图片链接  
  25.     public static final String[] IMAGES = new String[] {  
  26.             // Heavy images  
  27.             "https://lh6.googleusercontent.com/-jZgveEqb6pg/T3R4kXScycI/AAAAAAAAAE0/xQ7CvpfXDzc/s1024/sample_image_01.jpg",  
  28.             "https://lh4.googleusercontent.com/-K2FMuOozxU0/T3R4lRAiBTI/AAAAAAAAAE8/a3Eh9JvnnzI/s1024/sample_image_02.jpg",  
  29.             "https://lh5.googleusercontent.com/-SCS5C646rxM/T3R4l7QB6xI/AAAAAAAAAFE/xLcuVv3CUyA/s1024/sample_image_03.jpg",  
  30.             "https://lh6.googleusercontent.com/-f0NJR6-_Thg/T3R4mNex2wI/AAAAAAAAAFI/45oug4VE8MI/s1024/sample_image_04.jpg",  
  31.             "https://lh3.googleusercontent.com/-n-xcJmiI0pg/T3R4mkSchHI/AAAAAAAAAFU/EoiNNb7kk3A/s1024/sample_image_05.jpg",  
  32.             "https://lh3.googleusercontent.com/-X43vAudm7f4/T3R4nGSChJI/AAAAAAAAAFk/3bna6D-2EE8/s1024/sample_image_06.jpg",  
  33.             "https://lh5.googleusercontent.com/-MpZneqIyjXU/T3R4nuGO1aI/AAAAAAAAAFg/r09OPjLx1ZY/s1024/sample_image_07.jpg",  
  34.             "https://lh6.googleusercontent.com/-ql3YNfdClJo/T3XvW9apmFI/AAAAAAAAAL4/_6HFDzbahc4/s1024/sample_image_08.jpg",  
  35.             "https://lh5.googleusercontent.com/-Pxa7eqF4cyc/T3R4oasvPEI/AAAAAAAAAF0/-uYDH92h8LA/s1024/sample_image_09.jpg",  
  36.             "https://lh4.googleusercontent.com/-Li-rjhFEuaI/T3R4o-VUl4I/AAAAAAAAAF8/5E5XdMnP1oE/s1024/sample_image_10.jpg",  
  37.             "https://lh5.googleusercontent.com/-_HU4fImgFhA/T3R4pPVIwWI/AAAAAAAAAGA/0RfK_Vkgth4/s1024/sample_image_11.jpg",  
  38.             "https://lh6.googleusercontent.com/-0gnNrVjwa0Y/T3R4peGYJwI/AAAAAAAAAGU/uX_9wvRPM9I/s1024/sample_image_12.jpg",  
  39.             "https://lh3.googleusercontent.com/-HBxuzALS_Zs/T3R4qERykaI/AAAAAAAAAGQ/_qQ16FaZ1q0/s1024/sample_image_13.jpg",  
  40.             "https://lh4.googleusercontent.com/-cKojDrARNjQ/T3R4qfWSGPI/AAAAAAAAAGY/MR5dnbNaPyY/s1024/sample_image_14.jpg",  
  41.             "https://lh3.googleusercontent.com/-WujkdYfcyZ8/T3R4qrIMGUI/AAAAAAAAAGk/277LIdgvnjg/s1024/sample_image_15.jpg",  
  42.             "https://lh6.googleusercontent.com/-FMHR7Vy3PgI/T3R4rOXlEKI/AAAAAAAAAGs/VeXrDNDBkaw/s1024/sample_image_16.jpg",  
  43.             "https://lh4.googleusercontent.com/-mrR0AJyNTH0/T3R4rZs6CuI/AAAAAAAAAG0/UE1wQqCOqLA/s1024/sample_image_17.jpg",  
  44.             "https://lh6.googleusercontent.com/-z77w0eh3cow/T3R4rnLn05I/AAAAAAAAAG4/BaerfWoNucU/s1024/sample_image_18.jpg",  
  45.             "https://lh5.googleusercontent.com/-aWVwh1OU5Bk/T3R4sAWw0yI/AAAAAAAAAHE/4_KAvJttFwA/s1024/sample_image_19.jpg",  
  46.             "https://lh6.googleusercontent.com/-q-js52DMnWQ/T3R4tZhY2sI/AAAAAAAAAHM/A8kjp2Ivdqg/s1024/sample_image_20.jpg",  
  47.             "https://lh5.googleusercontent.com/-_jIzvvzXKn4/T3R4t7xpdVI/AAAAAAAAAHU/7QC6eZ10jgs/s1024/sample_image_21.jpg",  
  48.             "https://lh3.googleusercontent.com/-lnGi4IMLpwU/T3R4uCMa7vI/AAAAAAAAAHc/1zgzzz6qTpk/s1024/sample_image_22.jpg",  
  49.             "https://lh5.googleusercontent.com/-fFCzKjFPsPc/T3R4u0SZPFI/AAAAAAAAAHk/sbgjzrktOK0/s1024/sample_image_23.jpg",  
  50.             "https://lh4.googleusercontent.com/-8TqoW5gBE_Y/T3R4vBS3NPI/AAAAAAAAAHs/EZYvpNsaNXk/s1024/sample_image_24.jpg",  
  51.             "https://lh6.googleusercontent.com/-gc4eQ3ySdzs/T3R4vafoA7I/AAAAAAAAAH4/yKii5P6tqDE/s1024/sample_image_25.jpg",  
  52.             "https://lh5.googleusercontent.com/--NYOPCylU7Q/T3R4vjAiWkI/AAAAAAAAAH8/IPNx5q3ptRA/s1024/sample_image_26.jpg",  
  53.             "https://lh6.googleusercontent.com/-9IJM8so4vCI/T3R4vwJO2yI/AAAAAAAAAIE/ljlr-cwuqZM/s1024/sample_image_27.jpg",  
  54.             "https://lh4.googleusercontent.com/-KW6QwOHfhBs/T3R4w0RsQiI/AAAAAAAAAIM/uEFLVgHPFCk/s1024/sample_image_28.jpg",  
  55.             "https://lh4.googleusercontent.com/-z2557Ec1ctY/T3R4x3QA2hI/AAAAAAAAAIk/9-GzPL1lTWE/s1024/sample_image_29.jpg",  
  56.             "https://lh5.googleusercontent.com/-LaKXAn4Kr1c/T3R4yc5b4lI/AAAAAAAAAIY/fMgcOVQfmD0/s1024/sample_image_30.jpg",  
  57.             "https://lh4.googleusercontent.com/-F9LRToJoQdo/T3R4yrLtyQI/AAAAAAAAAIg/ri9uUCWuRmo/s1024/sample_image_31.jpg",  
  58.             "https://lh4.googleusercontent.com/-6X-xBwP-QpI/T3R4zGVboII/AAAAAAAAAIs/zYH4PjjngY0/s1024/sample_image_32.jpg",  
  59.             "https://lh5.googleusercontent.com/-VdLRjbW4LAs/T3R4zXu3gUI/AAAAAAAAAIw/9aFp9t7mCPg/s1024/sample_image_33.jpg",  
  60.             "https://lh6.googleusercontent.com/-gL6R17_fDJU/T3R4zpIXGjI/AAAAAAAAAI8/Q2Vjx-L9X20/s1024/sample_image_34.jpg",  
  61.             "https://lh3.googleusercontent.com/-1fGH4YJXEzo/T3R40Y1B7KI/AAAAAAAAAJE/MnTsa77g-nk/s1024/sample_image_35.jpg",  
  62.             "https://lh4.googleusercontent.com/-Ql0jHSrea-A/T3R403mUfFI/AAAAAAAAAJM/qzI4SkcH9tY/s1024/sample_image_36.jpg",  
  63.             "https://lh5.googleusercontent.com/-BL5FIBR_tzI/T3R41DA0AKI/AAAAAAAAAJk/GZfeeb-SLM0/s1024/sample_image_37.jpg",  
  64.             "https://lh4.googleusercontent.com/-wF2Vc9YDutw/T3R41fR2BCI/AAAAAAAAAJc/JdU1sHdMRAk/s1024/sample_image_38.jpg",  
  65.             "https://lh6.googleusercontent.com/-ZWHiPehwjTI/T3R41zuaKCI/AAAAAAAAAJg/hR3QJ1v3REg/s1024/sample_image_39.jpg",  
  66.             // Light images  
  67.             "http://tabletpcssource.com/wp-content/uploads/2011/05/android-logo.png",  
  68.             "http://simpozia.com/pages/images/stories/windows-icon.png",  
  69.             "https://si0.twimg.com/profile_images/1135218951/gmail_profile_icon3_normal.png",  
  70.             "http://www.krify.net/wp-content/uploads/2011/09/Macromedia_Flash_dock_icon.png",  
  71.             "http://radiotray.sourceforge.net/radio.png",  
  72.             "http://www.bandwidthblog.com/wp-content/uploads/2011/11/twitter-logo.png",  
  73.             "http://weloveicons.s3.amazonaws.com/icons/100907_itunes1.png",  
  74.             "http://weloveicons.s3.amazonaws.com/icons/100929_applications.png",  
  75.             "http://www.idyllicmusic.com/index_files/get_apple-iphone.png",  
  76.             "http://www.frenchrevolutionfood.com/wp-content/uploads/2009/04/Twitter-Bird.png",  
  77.             "http://3.bp.blogspot.com/-ka5MiRGJ_S4/TdD9OoF6bmI/AAAAAAAAE8k/7ydKtptUtSg/s1600/Google_Sky%2BMaps_Android.png",  
  78.             "http://www.desiredsoft.com/images/icon_webhosting.png",  
  79.             "http://goodereader.com/apps/wp-content/uploads/downloads/thumbnails/2012/01/hi-256-0-99dda8c730196ab93c67f0659d5b8489abdeb977.png",  
  80.             "http://1.bp.blogspot.com/-mlaJ4p_3rBU/TdD9OWxN8II/AAAAAAAAE8U/xyynWwr3_4Q/s1600/antivitus_free.png",  
  81.             "http://cdn3.iconfinder.com/data/icons/transformers/computer.png",  
  82.             "http://cdn.geekwire.com/wp-content/uploads/2011/04/firefox.png?7794fe",  
  83.             "https://ssl.gstatic.com/android/market/com.rovio.angrybirdsseasons/hi-256-9-347dae230614238a639d21508ae492302340b2ba",  
  84.             "http://androidblaze.com/wp-content/uploads/2011/12/tablet-pc-256x256.jpg",  
  85.             "http://www.theblaze.com/wp-content/uploads/2011/08/Apple.png",  
  86.             "http://1.bp.blogspot.com/-y-HQwQ4Kuu0/TdD9_iKIY7I/AAAAAAAAE88/3G4xiclDZD0/s1600/Twitter_Android.png",  
  87.             "http://3.bp.blogspot.com/-nAf4IMJGpc8/TdD9OGNUHHI/AAAAAAAAE8E/VM9yU_lIgZ4/s1600/Adobe%2BReader_Android.png",  
  88.             "http://cdn.geekwire.com/wp-content/uploads/2011/05/oovoo-android.png?7794fe",  
  89.             "http://icons.iconarchive.com/icons/kocco/ndroid/128/android-market-2-icon.png",  
  90.             "http://thecustomizewindows.com/wp-content/uploads/2011/11/Nicest-Android-Live-Wallpapers.png",  
  91.             "http://c.wrzuta.pl/wm16596/a32f1a47002ab3a949afeb4f",  
  92.             "http://macprovid.vo.llnwd.net/o43/hub/media/1090/6882/01_headline_Muse.jpg",  
  93.             // Special cases  
  94.             "http://cdn.urbanislandz.com/wp-content/uploads/2011/10/MMSposter-large.jpg", // very large image  
  95.             "file:///sdcard/Universal Image Loader @#&=+-_.,!()~'%20.png", // Image from SD card with encoded symbols  
  96.             "assets://Living Things @#&=+-_.,!()~'%20.jpg", // Image from assets  
  97.             "drawable://" + R.drawable.ic_launcher, // Image from drawables  
  98.             "http://upload.wikimedia.org/wikipedia/ru/b/b6/袣邪泻_泻芯褌_褋_屑褘褕邪屑懈_胁芯械胁邪谢.png", // Link with UTF-8  
  99.             "https://www.eff.org/sites/default/files/chrome150_0.jpg", // Image from HTTPS  
  100.             "http://bit.ly/soBiXr", // Redirect link  
  101.             "http://img001.us.expono.com/100001/100001-1bc30-2d736f_m.jpg", // EXIF  
  102.             ""// Empty link  
  103.             "http://wrong.site.com/corruptedLink", // Wrong link  
  104.     };  
  105.   
  106.     private Constants() {  
  107.     }  
  108.   
  109.     // 配置  
  110.     public static class Config {  
  111.         public static final boolean DEVELOPER_MODE = false;  
  112.     }  
  113.       
  114.     // 额外类  
  115.     public static class Extra {  
  116.         public static final String IMAGES = "com.nostra13.example.universalimageloader.IMAGES";  
  117.         public static final String IMAGE_POSITION = "com.nostra13.example.universalimageloader.IMAGE_POSITION";  
  118.     }  
  119. }  


/2013.8.19_Universal_Image_Loader_Demo/src/com/nostra13/example/universalimageloader/ImageListActivity.java

 

 

[java]  view plain copy print ?
  1. /******************************************************************************* 
  2.  * Copyright 2011-2013 Sergey Tarasevich 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  * http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  *******************************************************************************/  
  16. package com.nostra13.example.universalimageloader;  
  17.   
  18. import java.util.Collections;  
  19. import java.util.LinkedList;  
  20. import java.util.List;  
  21.   
  22. import android.content.Intent;  
  23. import android.graphics.Bitmap;  
  24. import android.os.Bundle;  
  25. import android.view.View;  
  26. import android.view.ViewGroup;  
  27. import android.widget.AdapterView;  
  28. import android.widget.AdapterView.OnItemClickListener;  
  29. import android.widget.BaseAdapter;  
  30. import android.widget.ImageView;  
  31. import android.widget.ListView;  
  32. import android.widget.TextView;  
  33.   
  34. import com.nostra13.example.universalimageloader.Constants.Extra;  
  35. import com.nostra13.universalimageloader.core.DisplayImageOptions;  
  36. import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;  
  37. import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;  
  38. import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;  
  39. import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;  
  40.   
  41. /** 
  42.  * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) 
  43.  */  
  44. public class ImageListActivity extends AbsListViewBaseActivity {  
  45.   
  46.     DisplayImageOptions options;        // DisplayImageOptions是用于设置图片显示的类  
  47.   
  48.     String[] imageUrls;                 // 图片路径  
  49.   
  50.     @Override  
  51.     public void onCreate(Bundle savedInstanceState) {  
  52.         super.onCreate(savedInstanceState);  
  53.         setContentView(R.layout.ac_image_list);  
  54.   
  55.         Bundle bundle = getIntent().getExtras();  
  56.         imageUrls = bundle.getStringArray(Extra.IMAGES);  
  57.   
  58.         // 使用DisplayImageOptions.Builder()创建DisplayImageOptions  
  59.         options = new DisplayImageOptions.Builder()  
  60.             .showStubImage(R.drawable.ic_stub)          // 设置图片下载期间显示的图片  
  61.             .showImageForEmptyUri(R.drawable.ic_empty)  // 设置图片Uri为空或是错误的时候显示的图片  
  62.             .showImageOnFail(R.drawable.ic_error)       // 设置图片加载或解码过程中发生错误显示的图片      
  63.             .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中  
  64.             .cacheOnDisc(true)                          // 设置下载的图片是否缓存在SD卡中  
  65.             .displayer(new RoundedBitmapDisplayer(20))  // 设置成圆角图片  
  66.             .build();                                   // 创建配置过得DisplayImageOption对象  
  67.   
  68.         listView = (ListView) findViewById(android.R.id.list);  
  69.         ((ListView) listView).setAdapter(new ItemAdapter());  
  70.         listView.setOnItemClickListener(new OnItemClickListener() {  
  71.             @Override  
  72.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
  73.                 // 点击列表项转入ViewPager显示界面  
  74.                 startImagePagerActivity(position);        
  75.             }  
  76.         });  
  77.     }  
  78.   
  79.     @Override  
  80.     public void onBackPressed() {  
  81.         AnimateFirstDisplayListener.displayedImages.clear();  
  82.         super.onBackPressed();  
  83.     }  
  84.   
  85.     private void startImagePagerActivity(int position) {  
  86.         Intent intent = new Intent(this, ImagePagerActivity.class);  
  87.         intent.putExtra(Extra.IMAGES, imageUrls);  
  88.         intent.putExtra(Extra.IMAGE_POSITION, position);  
  89.         startActivity(intent);  
  90.     }  
  91.   
  92.       
  93.     /** 
  94.      *  
  95.      * 自定义列表项适配器 
  96.      * 
  97.      */  
  98.     class ItemAdapter extends BaseAdapter {  
  99.   
  100.         private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();  
  101.   
  102.         private class ViewHolder {  
  103.             public TextView text;  
  104.             public ImageView image;  
  105.         }  
  106.   
  107.         @Override  
  108.         public int getCount() {  
  109.             return imageUrls.length;  
  110.         }  
  111.   
  112.         @Override  
  113.         public Object getItem(int position) {  
  114.             return position;  
  115.         }  
  116.   
  117.         @Override  
  118.         public long getItemId(int position) {  
  119.             return position;  
  120.         }  
  121.   
  122.         @Override  
  123.         public View getView(final int position, View convertView, ViewGroup parent) {  
  124.             View view = convertView;  
  125.             final ViewHolder holder;  
  126.             if (convertView == null) {  
  127.                 view = getLayoutInflater().inflate(R.layout.item_list_image, parent, false);  
  128.                 holder = new ViewHolder();  
  129.                 holder.text = (TextView) view.findViewById(R.id.text);  
  130.                 holder.image = (ImageView) view.findViewById(R.id.image);  
  131.                 view.setTag(holder);        // 给View添加一个格外的数据  
  132.             } else {  
  133.                 holder = (ViewHolder) view.getTag(); // 把数据取出来  
  134.             }  
  135.   
  136.             holder.text.setText("Item " + (position + 1));  // TextView设置文本  
  137.   
  138.             /** 
  139.              * 显示图片 
  140.              * 参数1:图片url 
  141.              * 参数2:显示图片的控件 
  142.              * 参数3:显示图片的设置 
  143.              * 参数4:监听器 
  144.              */  
  145.             imageLoader.displayImage(imageUrls[position], holder.image, options, animateFirstListener);  
  146.   
  147.             return view;  
  148.         }  
  149.     }  
  150.   
  151.     /** 
  152.      * 图片加载第一次显示监听器 
  153.      * @author Administrator 
  154.      * 
  155.      */  
  156.     private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {  
  157.           
  158.         static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());  
  159.   
  160.         @Override  
  161.         public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {  
  162.             if (loadedImage != null) {  
  163.                 ImageView imageView = (ImageView) view;  
  164.                 // 是否第一次显示  
  165.                 boolean firstDisplay = !displayedImages.contains(imageUri);  
  166.                 if (firstDisplay) {  
  167.                     // 图片淡入效果  
  168.                     FadeInBitmapDisplayer.animate(imageView, 500);  
  169.                     displayedImages.add(imageUri);  
  170.                 }  
  171.             }  
  172.         }  
  173.     }  
  174. }  


/2013.8.19_Universal_Image_Loader_Demo/src/com/nostra13/example/universalimageloader/ImageGridActivity.java

 

网格视图Activity

 

[java]  view plain copy print ?
  1. /******************************************************************************* 
  2.  * Copyright 2011-2013 Sergey Tarasevich 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  * http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  *******************************************************************************/  
  16. package com.nostra13.example.universalimageloader;  
  17.   
  18. import android.content.Intent;  
  19. import android.graphics.Bitmap;  
  20. import android.os.Bundle;  
  21. import android.view.View;  
  22. import android.view.ViewGroup;  
  23. import android.widget.AdapterView;  
  24. import android.widget.AdapterView.OnItemClickListener;  
  25. import android.widget.BaseAdapter;  
  26. import android.widget.GridView;  
  27. import android.widget.ImageView;  
  28.   
  29. import com.nostra13.example.universalimageloader.Constants.Extra;  
  30. import com.nostra13.universalimageloader.core.DisplayImageOptions;  
  31.   
  32. /** 
  33.  * 网格视图显示Activity 
  34.  * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) 
  35.  */  
  36. public class ImageGridActivity extends AbsListViewBaseActivity {  
  37.   
  38.     String[] imageUrls;                 // 图片Url  
  39.   
  40.     DisplayImageOptions options;        // 显示图片的设置  
  41.   
  42.     @Override  
  43.     public void onCreate(Bundle savedInstanceState) {  
  44.         super.onCreate(savedInstanceState);  
  45.         setContentView(R.layout.ac_image_grid);  
  46.   
  47.         Bundle bundle = getIntent().getExtras();  
  48.         imageUrls = bundle.getStringArray(Extra.IMAGES);  
  49.   
  50.         options = new DisplayImageOptions.Builder()  
  51.             .showStubImage(R.drawable.ic_stub)  
  52.             .showImageForEmptyUri(R.drawable.ic_empty)  
  53.             .showImageOnFail(R.drawable.ic_error)  
  54.             .cacheInMemory(true)  
  55.             .cacheOnDisc(true)  
  56.             .bitmapConfig(Bitmap.Config.RGB_565)     //设置图片的解码类型  
  57.             .build();  
  58.   
  59.         listView = (GridView) findViewById(R.id.gridview);  
  60.         ((GridView) listView).setAdapter(new ImageAdapter());           // 填充数据  
  61.         listView.setOnItemClickListener(new OnItemClickListener() {  
  62.             @Override  
  63.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
  64.                 startImagePagerActivity(position);  
  65.             }  
  66.         });  
  67.     }  
  68.   
  69.     private void startImagePagerActivity(int position) {  
  70.         Intent intent = new Intent(this, ImagePagerActivity.class);  
  71.         intent.putExtra(Extra.IMAGES, imageUrls);  
  72.         intent.putExtra(Extra.IMAGE_POSITION, position);  
  73.         startActivity(intent);  
  74.     }  
  75.   
  76.     public class ImageAdapter extends BaseAdapter {  
  77.         @Override  
  78.         public int getCount() {  
  79.             return imageUrls.length;  
  80.         }  
  81.   
  82.         @Override  
  83.         public Object getItem(int position) {  
  84.             return null;  
  85.         }  
  86.   
  87.         @Override  
  88.         public long getItemId(int position) {  
  89.             return position;  
  90.         }  
  91.   
  92.         @Override  
  93.         public View getView(int position, View convertView, ViewGroup parent) {  
  94.             final ImageView imageView;  
  95.             if (convertView == null) {  
  96.                 imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);  
  97.             } else {  
  98.                 imageView = (ImageView) convertView;  
  99.             }  
  100.   
  101.             // 将图片显示任务增加到执行池,图片将被显示到ImageView当轮到此ImageView  
  102.             imageLoader.displayImage(imageUrls[position], imageView, options);  
  103.   
  104.             return imageView;  
  105.         }  
  106.     }  
  107. }  



 

/2013.8.19_Universal_Image_Loader_Demo/src/com/nostra13/example/universalimageloader/ImagePagerActivity.java

ViewPager视图Activity

 

[java]  view plain copy print ?
  1. /******************************************************************************* 
  2.  * Copyright 2011-2013 Sergey Tarasevich 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  * http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  *******************************************************************************/  
  16. package com.nostra13.example.universalimageloader;  
  17.   
  18. import android.graphics.Bitmap;  
  19. import android.os.Bundle;  
  20. import android.os.Parcelable;  
  21. import android.support.v4.view.PagerAdapter;  
  22. import android.support.v4.view.ViewPager;  
  23. import android.view.LayoutInflater;  
  24. import android.view.View;  
  25. import android.view.ViewGroup;  
  26. import android.widget.ImageView;  
  27. import android.widget.ProgressBar;  
  28. import android.widget.Toast;  
  29.   
  30. import com.nostra13.example.universalimageloader.Constants.Extra;  
  31. import com.nostra13.universalimageloader.core.DisplayImageOptions;  
  32. import com.nostra13.universalimageloader.core.assist.FailReason;  
  33. import com.nostra13.universalimageloader.core.assist.ImageScaleType;  
  34. import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;  
  35. import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;  
  36.   
  37. /** 
  38.  * ViewPager页面显示Activity 
  39.  * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) 
  40.  */  
  41. public class ImagePagerActivity extends BaseActivity {  
  42.   
  43.     private static final String STATE_POSITION = "STATE_POSITION";  
  44.   
  45.     DisplayImageOptions options;  
  46.   
  47.     ViewPager pager;  
  48.   
  49.     public void onCreate(Bundle savedInstanceState) {  
  50.         super.onCreate(savedInstanceState);  
  51.         setContentView(R.layout.ac_image_pager);  
  52.   
  53.         Bundle bundle = getIntent().getExtras();  
  54.         String[] imageUrls = bundle.getStringArray(Extra.IMAGES);  
  55.         // 当前显示View的位置  
  56.         int pagerPosition = bundle.getInt(Extra.IMAGE_POSITION, 0);  
  57.   
  58.         // 如果之前有保存用户数据  
  59.         if (savedInstanceState != null) {  
  60.             pagerPosition = savedInstanceState.getInt(STATE_POSITION);  
  61.         }  
  62.   
  63.         options = new DisplayImageOptions.Builder()  
  64.             .showImageForEmptyUri(R.drawable.ic_empty)  
  65.             .showImageOnFail(R.drawable.ic_error)  
  66.             .resetViewBeforeLoading(true)  
  67.             .cacheOnDisc(true)  
  68.             .imageScaleType(ImageScaleType.EXACTLY)  
  69.             .bitmapConfig(Bitmap.Config.RGB_565)  
  70.             .displayer(new FadeInBitmapDisplayer(300))  
  71.             .build();  
  72.   
  73.         pager = (ViewPager) findViewById(R.id.pager);  
  74.         pager.setAdapter(new ImagePagerAdapter(imageUrls));  
  75.         pager.setCurrentItem(pagerPosition);    // 显示当前位置的View  
  76.     }  
  77.   
  78.     @Override  
  79.     public void onSaveInstanceState(Bundle outState) {  
  80.         // 保存用户数据  
  81.         outState.putInt(STATE_POSITION, pager.getCurrentItem());  
  82.     }  
  83.   
  84.     private class ImagePagerAdapter extends PagerAdapter {  
  85.   
  86.         private String[] images;  
  87.         private LayoutInflater inflater;  
  88.   
  89.         ImagePagerAdapter(String[] images) {  
  90.             this.images = images;  
  91.             inflater = getLayoutInflater();  
  92.         }  
  93.   
  94.         @Override  
  95.         public void destroyItem(ViewGroup container, int position, Object object) {  
  96.             ((ViewPager) container).removeView((View) object);  
  97.         }  
  98.   
  99.         @Override  
  100.         public void finishUpdate(View container) {  
  101.         }  
  102.   
  103.         @Override  
  104.         public int getCount() {  
  105.             return images.length;  
  106.         }  
  107.   
  108.         @Override  
  109.         public Object instantiateItem(ViewGroup view, int position) {  
  110.             View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false);  
  111.             ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);  
  112.             final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);  
  113.   
  114.             imageLoader.displayImage(images[position], imageView, options, new SimpleImageLoadingListener() {  
  115.                 @Override  
  116.                 public void onLoadingStarted(String imageUri, View view) {  
  117.                     spinner.setVisibility(View.VISIBLE);  
  118.                 }  
  119.   
  120.                 @Override  
  121.                 public void onLoadingFailed(String imageUri, View view, FailReason failReason) {  
  122.                     String message = null;  
  123.                     switch (failReason.getType()) {     // 获取图片失败类型  
  124.                         case IO_ERROR:              // 文件I/O错误  
  125.                             message = "Input/Output error";  
  126.                             break;  
  127.                         case DECODING_ERROR:        // 解码错误  
  128.                             message = "Image can't be decoded";  
  129.                             break;  
  130.                         case NETWORK_DENIED:        // 网络延迟  
  131.                             message = "Downloads are denied";  
  132.                             break;  
  133.                         case OUT_OF_MEMORY:         // 内存不足  
  134.                             message = "Out Of Memory error";  
  135.                             break;  
  136.                         case UNKNOWN:               // 原因不明  
  137.                             message = "Unknown error";  
  138.                             break;  
  139.                     }  
  140.                     Toast.makeText(ImagePagerActivity.this, message, Toast.LENGTH_SHORT).show();  
  141.   
  142.                     spinner.setVisibility(View.GONE);  
  143.                 }  
  144.   
  145.                 @Override  
  146.                 public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {  
  147.                     spinner.setVisibility(View.GONE);       // 不显示圆形进度条  
  148.                 }  
  149.             });  
  150.   
  151.             ((ViewPager) view).addView(imageLayout, 0);     // 将图片增加到ViewPager  
  152.             return imageLayout;  
  153.         }  
  154.   
  155.         @Override  
  156.         public boolean isViewFromObject(View view, Object object) {  
  157.             return view.equals(object);  
  158.         }  
  159.   
  160.         @Override  
  161.         public void restoreState(Parcelable state, ClassLoader loader) {  
  162.         }  
  163.   
  164.         @Override  
  165.         public Parcelable saveState() {  
  166.             return null;  
  167.         }  
  168.   
  169.         @Override  
  170.         public void startUpdate(View container) {  
  171.         }  
  172.     }  
  173. }  


/2013.8.19_Universal_Image_Loader_Demo/src/com/nostra13/example/universalimageloader/ImageGalleryActivity.java

 

画廊视图的Activity

 

[java]  view plain copy print ?
  1. /******************************************************************************* 
  2.  * Copyright 2011-2013 Sergey Tarasevich 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  * http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  *******************************************************************************/  
  16. package com.nostra13.example.universalimageloader;  
  17.   
  18. import android.content.Intent;  
  19. import android.graphics.Bitmap;  
  20. import android.os.Bundle;  
  21. import android.view.View;  
  22. import android.view.ViewGroup;  
  23. import android.widget.AdapterView;  
  24. import android.widget.AdapterView.OnItemClickListener;  
  25. import android.widget.BaseAdapter;  
  26. import android.widget.Gallery;  
  27. import android.widget.ImageView;  
  28.   
  29. import com.nostra13.example.universalimageloader.Constants.Extra;  
  30. import com.nostra13.universalimageloader.core.DisplayImageOptions;  
  31.   
  32. /** 
  33.  * 画廊视图Activity 
  34.  * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) 
  35.  */  
  36. @SuppressWarnings("deprecation")  
  37. public class ImageGalleryActivity extends BaseActivity {  
  38.   
  39.     String[] imageUrls;  
  40.   
  41.     DisplayImageOptions options;  
  42.   
  43.     public void onCreate(Bundle savedInstanceState) {  
  44.         super.onCreate(savedInstanceState);  
  45.         setContentView(R.layout.ac_image_gallery);  
  46.   
  47.         Bundle bundle = getIntent().getExtras();  
  48.         imageUrls = bundle.getStringArray(Extra.IMAGES);  
  49.   
  50.         options = new DisplayImageOptions.Builder()  
  51.             .showStubImage(R.drawable.ic_stub)  
  52.             .showImageForEmptyUri(R.drawable.ic_empty)  
  53.             .showImageOnFail(R.drawable.ic_error)  
  54.             .cacheInMemory(true)  
  55.             .cacheOnDisc(true)  
  56.             .bitmapConfig(Bitmap.Config.RGB_565)  
  57.             .build();  
  58.   
  59.         // 自API 16之后就被抛弃了  
  60.         Gallery gallery = (Gallery) findViewById(R.id.gallery);  
  61.         gallery.setAdapter(new ImageGalleryAdapter());  
  62.         gallery.setOnItemClickListener(new OnItemClickListener() {  
  63.             @Override  
  64.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
  65.                 startImagePagerActivity(position);  
  66.             }  
  67.         });  
  68.     }  
  69.   
  70.     private void startImagePagerActivity(int position) {  
  71.         Intent intent = new Intent(this, ImagePagerActivity.class);  
  72.         intent.putExtra(Extra.IMAGES, imageUrls);  
  73.         intent.putExtra(Extra.IMAGE_POSITION, position);  
  74.         startActivity(intent);  
  75.     }  
  76.   
  77.     private class ImageGalleryAdapter extends BaseAdapter {  
  78.         @Override  
  79.         public int getCount() {  
  80.             return imageUrls.length;  
  81.         }  
  82.   
  83.         @Override  
  84.         public Object getItem(int position) {  
  85.             return position;  
  86.         }  
  87.   
  88.         @Override  
  89.         public long getItemId(int position) {  
  90.             return position;  
  91.         }  
  92.   
  93.         @Override  
  94.         public View getView(int position, View convertView, ViewGroup parent) {  
  95.             ImageView imageView = (ImageView) convertView;  
  96.             if (imageView == null) {  
  97.                 imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_gallery_image, parent, false);  
  98.             }  
  99.             imageLoader.displayImage(imageUrls[position], imageView, options);  
  100.             return imageView;  
  101.         }  
  102.     }  
  103. }  


关于桌面控件的代码我就不贴了,朋友们可以自己下载我这个Demo

 

 

还有就是Manifest文件代码

 

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.nostra13.example.universalimageloader"  
  4.     android:versionCode="35"  
  5.     android:versionName="1.8.7" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="5"  
  9.         android:targetSdkVersion="16" />  
  10.   
  11.     <!-- 网络访问权限 -->  
  12.     <uses-permission android:name="android.permission.INTERNET" />  
  13.     <!-- 文件写入SD卡权限 -->  
  14.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  15.   
  16.     <application  
  17.         android:name=".UILApplication"  
  18.         android:icon="@drawable/ic_launcher"  
  19.         android:label="@string/app_name" >  
  20.         <activity  
  21.             android:name=".HomeActivity"  
  22.             android:label="@string/app_name" >  
  23.             <intent-filter>  
  24.                 <action android:name="android.intent.action.MAIN" />  
  25.   
  26.                 <category android:name="android.intent.category.LAUNCHER" />  
  27.             </intent-filter>  
  28.         </activity>  
  29.         <activity  
  30.             android:name=".ImageListActivity"  
  31.             android:label="@string/ac_name_image_list" />  
  32.         <activity  
  33.             android:name=".ImageGridActivity"  
  34.             android:label="@string/ac_name_image_grid" />  
  35.         <activity  
  36.             android:name=".ImagePagerActivity"  
  37.             android:label="@string/ac_name_image_pager" />  
  38.         <activity  
  39.             android:name=".ImageGalleryActivity"  
  40.             android:label="@string/ac_name_image_gallery" />  
  41.   
  42.         <!-- Widget -->  
  43.         <receiver android:name=".widget.UILWidgetProvider" >  
  44.             <meta-data  
  45.                 android:name="android.appwidget.provider"  
  46.                 android:resource="@xml/widget_provider" />  
  47.   
  48.             <intent-filter>  
  49.                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />  
  50.             </intent-filter>  
  51.         </receiver>  
  52.     </application>  
  53.   
  54. </manifest>  



 

以上的代码已经很详尽了,读者可以把它整合到你的项目当中,就可以实现完美异步加载图片。在这里要感谢那些为我们开源的大神们,他们的贡献让我们这些开发者更快更有效的完成工作,希望我们的开源会越做越好,我相信我们的开发者有这个能力。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!