利用LruCache为GridView异步加载大量网络图片完整示例

来源:互联网 发布:linux idle process 编辑:程序博客网 时间:2024/06/03 20:08

转载:http://blog.csdn.net/lfdfhl/article/details/18350601

=============================================

MainActivity如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package cc.testlrucache;  
  2.   
  3. import android.os.Bundle;  
  4. import android.widget.GridView;  
  5. import android.app.Activity;  
  6. /** 
  7.  * Demo描述: 
  8.  * 在GridView中采用LruCache异步加载大量图片,避免OOM 
  9.  *  
  10.  * 学习资料: 
  11.  * http://blog.csdn.net/guolin_blog/article/details/9526203 
  12.  * Thank you very much 
  13.  */  
  14.   
  15. public class MainActivity extends Activity {  
  16.     private GridView mGridView;  
  17.     private GridViewAdapter mGridViewAdapter;  
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         init();  
  23.     }  
  24.       
  25.     private void init(){  
  26.         mGridView = (GridView) findViewById(R.id.gridView);  
  27.         mGridViewAdapter = new GridViewAdapter(this0, ImagesUrl.Urls, mGridView);  
  28.         mGridView.setAdapter(mGridViewAdapter);  
  29.     }  
  30.   
  31.     //取消所有的下载任务  
  32.     @Override  
  33.     protected void onDestroy() {  
  34.         super.onDestroy();  
  35.         mGridViewAdapter.cancelAllTasks();  
  36.     }  
  37.   
  38. }  


GridViewAdapter如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package cc.testlrucache;  
  2.   
  3. import java.net.HttpURLConnection;  
  4. import java.net.URL;  
  5. import java.util.HashSet;  
  6. import android.annotation.SuppressLint;  
  7. import android.content.Context;  
  8. import android.graphics.Bitmap;  
  9. import android.graphics.BitmapFactory;  
  10. import android.os.AsyncTask;  
  11. import android.support.v4.util.LruCache;  
  12. import android.view.LayoutInflater;  
  13. import android.view.View;  
  14. import android.view.ViewGroup;  
  15. import android.widget.AbsListView;  
  16. import android.widget.AbsListView.OnScrollListener;  
  17. import android.widget.ArrayAdapter;  
  18. import android.widget.GridView;  
  19. import android.widget.ImageView;  
  20. /** 
  21.  * LruCache的流程分析: 
  22.  * 我们从第一次进入应用的情况下开始 
  23.  * 1 依据图片的Url从LruCache缓存中取图片. 
  24.  *   若图片存在缓存中,则显示该图片;否则显示默认图片 
  25.  * 2 因为是第一次进入该界面所以会执行: 
  26.  *   loadBitmaps(firstVisibleItem, visibleItemCount); 
  27.  *   我们从loadBitmaps()方法作为切入点,继续往下梳理 
  28.  * 3 尝试从LruCache缓存中取图片.如果在显示即可,否则进入4 
  29.  * 4 开启一个异步任务下载图片.下载完成后显示图片,并且将 
  30.  *   该图片存入LruCache缓存中 
  31.  *    
  32.  * 在停止滑动时,会调用loadBitmaps(firstVisibleItem, visibleItemCount) 
  33.  * 情况与上类似 
  34.  */  
  35. @SuppressLint("NewApi")  
  36. public class GridViewAdapter extends ArrayAdapter<String> {  
  37.       
  38.     private GridView mGridView;  
  39.     //图片缓存类  
  40.     private LruCache<String, Bitmap> mLruCache;  
  41.     //记录所有正在下载或等待下载的任务  
  42.     private HashSet<DownloadBitmapAsyncTask> mDownloadBitmapAsyncTaskHashSet;  
  43.     //GridView中可见的第一张图片的下标  
  44.     private int mFirstVisibleItem;  
  45.     //GridView中可见的图片的数量  
  46.     private int mVisibleItemCount;  
  47.     //记录是否是第一次进入该界面  
  48.     private boolean isFirstEnterThisActivity = true;  
  49.   
  50.     public GridViewAdapter(Context context, int textViewResourceId,String[] objects, GridView gridView) {  
  51.         super(context, textViewResourceId, objects);  
  52.           
  53.         mGridView = gridView;  
  54.         mGridView.setOnScrollListener(new ScrollListenerImpl());  
  55.           
  56.         mDownloadBitmapAsyncTaskHashSet = new HashSet<DownloadBitmapAsyncTask>();  
  57.           
  58.         // 获取应用程序最大可用内存  
  59.         int maxMemory = (int) Runtime.getRuntime().maxMemory();  
  60.         // 设置图片缓存大小为maxMemory的1/6  
  61.         int cacheSize = maxMemory/6;  
  62.           
  63.         mLruCache = new LruCache<String, Bitmap>(cacheSize) {  
  64.             @Override  
  65.             protected int sizeOf(String key, Bitmap bitmap) {  
  66.                 return bitmap.getByteCount();  
  67.             }  
  68.         };  
  69.           
  70.     }  
  71.   
  72.     @Override  
  73.     public View getView(int position, View convertView, ViewGroup parent) {  
  74.         String url = getItem(position);  
  75.         View view;  
  76.         if (convertView == null) {  
  77.             view = LayoutInflater.from(getContext()).inflate(R.layout.gridview_item, null);  
  78.         } else {  
  79.             view = convertView;  
  80.         }  
  81.         ImageView imageView = (ImageView) view.findViewById(R.id.imageView);  
  82.         //为该ImageView设置一个Tag,防止图片错位  
  83.         imageView.setTag(url);  
  84.         //为该ImageView设置显示的图片  
  85.         setImageForImageView(url, imageView);  
  86.         return view;  
  87.     }  
  88.   
  89.     /** 
  90.      * 为ImageView设置图片(Image) 
  91.      * 1 从缓存中获取图片 
  92.      * 2 若图片不在缓存中则为其设置默认图片 
  93.      */  
  94.     private void setImageForImageView(String imageUrl, ImageView imageView) {  
  95.         Bitmap bitmap = getBitmapFromLruCache(imageUrl);  
  96.         if (bitmap != null) {  
  97.             imageView.setImageBitmap(bitmap);  
  98.         } else {  
  99.             imageView.setImageResource(R.drawable.default_image);  
  100.         }  
  101.     }  
  102.   
  103.     /** 
  104.      * 将图片存储到LruCache 
  105.      */  
  106.     public void addBitmapToLruCache(String key, Bitmap bitmap) {  
  107.         if (getBitmapFromLruCache(key) == null) {  
  108.             mLruCache.put(key, bitmap);  
  109.         }  
  110.     }  
  111.   
  112.     /** 
  113.      * 从LruCache缓存获取图片 
  114.      */  
  115.     public Bitmap getBitmapFromLruCache(String key) {  
  116.         return mLruCache.get(key);  
  117.     }  
  118.   
  119.       
  120.   
  121.    /** 
  122.     * 为GridView的item加载图片 
  123.     *  
  124.     * @param firstVisibleItem  
  125.     * GridView中可见的第一张图片的下标 
  126.     *  
  127.     * @param visibleItemCount  
  128.     * GridView中可见的图片的数量 
  129.     *  
  130.     */  
  131.     private void loadBitmaps(int firstVisibleItem, int visibleItemCount) {  
  132.         try {  
  133.             for (int i = firstVisibleItem; i < firstVisibleItem + visibleItemCount; i++) {  
  134.                 String imageUrl = ImagesUrl.Urls[i];  
  135.                 Bitmap bitmap = getBitmapFromLruCache(imageUrl);  
  136.                 if (bitmap == null) {  
  137.                     DownloadBitmapAsyncTask downloadBitmapAsyncTask = new DownloadBitmapAsyncTask();  
  138.                     mDownloadBitmapAsyncTaskHashSet.add(downloadBitmapAsyncTask);  
  139.                     downloadBitmapAsyncTask.execute(imageUrl);  
  140.                 } else {  
  141.                     //依据Tag找到对应的ImageView显示图片  
  142.                     ImageView imageView = (ImageView) mGridView.findViewWithTag(imageUrl);  
  143.                     if (imageView != null && bitmap != null) {  
  144.                         imageView.setImageBitmap(bitmap);  
  145.                     }  
  146.                 }  
  147.             }  
  148.         } catch (Exception e) {  
  149.             e.printStackTrace();  
  150.         }  
  151.     }  
  152.   
  153.     /** 
  154.      * 取消所有正在下载或等待下载的任务 
  155.      */  
  156.     public void cancelAllTasks() {  
  157.         if (mDownloadBitmapAsyncTaskHashSet != null) {  
  158.             for (DownloadBitmapAsyncTask task : mDownloadBitmapAsyncTaskHashSet) {  
  159.                 task.cancel(false);  
  160.             }  
  161.         }  
  162.     }  
  163.       
  164.       
  165.     private class ScrollListenerImpl implements OnScrollListener{  
  166.         /** 
  167.          *  
  168.          * 我们的本意是通过onScrollStateChanged获知:每次GridView停止滑动时加载图片 
  169.          * 但是存在一个特殊情况: 
  170.          * 当第一次入应用的时候,此时并没有滑动屏幕的操作即不会调用onScrollStateChanged,但应该加载图片. 
  171.          * 所以在此处做一个特殊的处理. 
  172.          * 即代码: 
  173.          * if (isFirstEnterThisActivity && visibleItemCount > 0) { 
  174.          *      loadBitmaps(firstVisibleItem, visibleItemCount); 
  175.          *      isFirstEnterThisActivity = false; 
  176.          *    } 
  177.          *     
  178.          * ------------------------------------------------------------ 
  179.          *  
  180.          * 其余的都是正常情况. 
  181.          * 所以我们需要不断保存:firstVisibleItem和visibleItemCount 
  182.          * 从而便于中在onScrollStateChanged()判断当停止滑动时加载图片 
  183.          *  
  184.          */  
  185.         @Override  
  186.         public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {  
  187.             mFirstVisibleItem = firstVisibleItem;  
  188.             mVisibleItemCount = visibleItemCount;  
  189.             if (isFirstEnterThisActivity && visibleItemCount > 0) {  
  190.                 loadBitmaps(firstVisibleItem, visibleItemCount);  
  191.                 isFirstEnterThisActivity = false;  
  192.             }  
  193.         }  
  194.           
  195.         /** 
  196.          *  GridView停止滑动时下载图片 
  197.          *  其余情况下取消所有正在下载或者等待下载的任务 
  198.          */  
  199.         @Override  
  200.         public void onScrollStateChanged(AbsListView view, int scrollState) {  
  201.             if (scrollState == SCROLL_STATE_IDLE) {  
  202.                 loadBitmaps(mFirstVisibleItem, mVisibleItemCount);  
  203.             } else {  
  204.                 cancelAllTasks();  
  205.             }  
  206.         }  
  207.           
  208.     }  
  209.   
  210.     /** 
  211.      * 下载图片的异步任务 
  212.      */  
  213.     class DownloadBitmapAsyncTask extends AsyncTask<String, Void, Bitmap> {  
  214.         private String imageUrl;  
  215.         @Override  
  216.         protected Bitmap doInBackground(String... params) {  
  217.             imageUrl = params[0];  
  218.             Bitmap bitmap = downloadBitmap(params[0]);  
  219.             if (bitmap != null) {  
  220.                 //下载完后,将其缓存到LrcCache  
  221.                 addBitmapToLruCache(params[0], bitmap);  
  222.             }  
  223.             return bitmap;  
  224.         }  
  225.   
  226.         @Override  
  227.         protected void onPostExecute(Bitmap bitmap) {  
  228.             super.onPostExecute(bitmap);  
  229.             //下载完成后,找到其对应的ImageView显示图片  
  230.             ImageView imageView = (ImageView) mGridView.findViewWithTag(imageUrl);  
  231.             if (imageView != null && bitmap != null) {  
  232.                 imageView.setImageBitmap(bitmap);  
  233.             }  
  234.             mDownloadBitmapAsyncTaskHashSet.remove(this);  
  235.         }  
  236.     }  
  237.   
  238.     // 获取Bitmap  
  239.     private Bitmap downloadBitmap(String imageUrl) {  
  240.         Bitmap bitmap = null;  
  241.         HttpURLConnection httpURLConnection = null;  
  242.         try {  
  243.             URL url = new URL(imageUrl);  
  244.             httpURLConnection = (HttpURLConnection) url.openConnection();  
  245.             httpURLConnection.setConnectTimeout(5 * 1000);  
  246.             httpURLConnection.setReadTimeout(10 * 1000);  
  247.             httpURLConnection.setDoInput(true);  
  248.             httpURLConnection.setDoOutput(true);  
  249.             bitmap = BitmapFactory.decodeStream(httpURLConnection.getInputStream());  
  250.         } catch (Exception e) {  
  251.             e.printStackTrace();  
  252.         } finally {  
  253.             if (httpURLConnection != null) {  
  254.                 httpURLConnection.disconnect();  
  255.             }  
  256.         }  
  257.         return bitmap;  
  258.     }  
  259.   
  260. }  


ImagesUrl如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package cc.testlrucache;  
  2.   
  3. public class ImagesUrl {  
  4.   
  5.     public final static String[] Urls = new String[] {  
  6.             "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s160-c/A%252520Photographer.jpg",  
  7.             "https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s160-c/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",  
  8.             "https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s160-c/Another%252520Rockaway%252520Sunset.jpg",  
  9.             "https://lh3.googleusercontent.com/--L0Km39l5J8/URquXHGcdNI/AAAAAAAAAbs/3ZrSJNrSomQ/s160-c/Antelope%252520Butte.jpg",  
  10.             "https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s160-c/Antelope%252520Hallway.jpg",  
  11.             "https://lh4.googleusercontent.com/-WIuWgVcU3Qw/URqubRVcj4I/AAAAAAAAAbs/YvbwgGjwdIQ/s160-c/Antelope%252520Walls.jpg",  
  12.             "https://lh6.googleusercontent.com/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs/s160-c/Apre%2525CC%252580s%252520la%252520Pluie.jpg",  
  13.             "https://lh3.googleusercontent.com/-s-AFpvgSeew/URquc6dF-JI/AAAAAAAAAbs/Mt3xNGRUd68/s160-c/Backlit%252520Cloud.jpg",  
  14.             "https://lh5.googleusercontent.com/-bvmif9a9YOQ/URquea3heHI/AAAAAAAAAbs/rcr6wyeQtAo/s160-c/Bee%252520and%252520Flower.jpg",  
  15.             "https://lh5.googleusercontent.com/-n7mdm7I7FGs/URqueT_BT-I/AAAAAAAAAbs/9MYmXlmpSAo/s160-c/Bonzai%252520Rock%252520Sunset.jpg",  
  16.             "https://lh6.googleusercontent.com/-4CN4X4t0M1k/URqufPozWzI/AAAAAAAAAbs/8wK41lg1KPs/s160-c/Caterpillar.jpg",  
  17.             "https://lh3.googleusercontent.com/-rrFnVC8xQEg/URqufdrLBaI/AAAAAAAAAbs/s69WYy_fl1E/s160-c/Chess.jpg",  
  18.             "https://lh5.googleusercontent.com/-WVpRptWH8Yw/URqugh-QmDI/AAAAAAAAAbs/E-MgBgtlUWU/s160-c/Chihuly.jpg",  
  19.             "https://lh5.googleusercontent.com/-0BDXkYmckbo/URquhKFW84I/AAAAAAAAAbs/ogQtHCTk2JQ/s160-c/Closed%252520Door.jpg",  
  20.             "https://lh3.googleusercontent.com/-PyggXXZRykM/URquh-kVvoI/AAAAAAAAAbs/hFtDwhtrHHQ/s160-c/Colorado%252520River%252520Sunset.jpg",  
  21.             "https://lh3.googleusercontent.com/-ZAs4dNZtALc/URquikvOCWI/AAAAAAAAAbs/DXz4h3dll1Y/s160-c/Colors%252520of%252520Autumn.jpg",  
  22.             "https://lh4.googleusercontent.com/-GztnWEIiMz8/URqukVCU7bI/AAAAAAAAAbs/jo2Hjv6MZ6M/s160-c/Countryside.jpg",  
  23.             "https://lh4.googleusercontent.com/-bEg9EZ9QoiM/URquklz3FGI/AAAAAAAAAbs/UUuv8Ac2BaE/s160-c/Death%252520Valley%252520-%252520Dunes.jpg",  
  24.             "https://lh6.googleusercontent.com/-ijQJ8W68tEE/URqulGkvFEI/AAAAAAAAAbs/zPXvIwi_rFw/s160-c/Delicate%252520Arch.jpg",  
  25.             "https://lh5.googleusercontent.com/-Oh8mMy2ieng/URqullDwehI/AAAAAAAAAbs/TbdeEfsaIZY/s160-c/Despair.jpg",  
  26.             "https://lh5.googleusercontent.com/-gl0y4UiAOlk/URqumC_KjBI/AAAAAAAAAbs/PM1eT7dn4oo/s160-c/Eagle%252520Fall%252520Sunrise.jpg",  
  27.             "https://lh3.googleusercontent.com/-hYYHd2_vXPQ/URqumtJa9eI/AAAAAAAAAbs/wAalXVkbSh0/s160-c/Electric%252520Storm.jpg",  
  28.             "https://lh5.googleusercontent.com/-PyY_yiyjPTo/URqunUOhHFI/AAAAAAAAAbs/azZoULNuJXc/s160-c/False%252520Kiva.jpg",  
  29.             "https://lh6.googleusercontent.com/-PYvLVdvXywk/URqunwd8hfI/AAAAAAAAAbs/qiMwgkFvf6I/s160-c/Fitzgerald%252520Streaks.jpg",  
  30.             "https://lh4.googleusercontent.com/-KIR_UobIIqY/URquoCZ9SlI/AAAAAAAAAbs/Y4d4q8sXu4c/s160-c/Foggy%252520Sunset.jpg",  
  31.             "https://lh6.googleusercontent.com/-9lzOk_OWZH0/URquoo4xYoI/AAAAAAAAAbs/AwgzHtNVCwU/s160-c/Frantic.jpg",  
  32.             "https://lh3.googleusercontent.com/-0X3JNaKaz48/URqupH78wpI/AAAAAAAAAbs/lHXxu_zbH8s/s160-c/Golden%252520Gate%252520Afternoon.jpg",  
  33.             "https://lh6.googleusercontent.com/-95sb5ag7ABc/URqupl95RDI/AAAAAAAAAbs/g73R20iVTRA/s160-c/Golden%252520Gate%252520Fog.jpg",  
  34.             "https://lh3.googleusercontent.com/-JB9v6rtgHhk/URqup21F-zI/AAAAAAAAAbs/64Fb8qMZWXk/s160-c/Golden%252520Grass.jpg",  
  35.             "https://lh4.googleusercontent.com/-EIBGfnuLtII/URquqVHwaRI/AAAAAAAAAbs/FA4McV2u8VE/s160-c/Grand%252520Teton.jpg",  
  36.             "https://lh4.googleusercontent.com/-WoMxZvmN9nY/URquq1v2AoI/AAAAAAAAAbs/grj5uMhL6NA/s160-c/Grass%252520Closeup.jpg",  
  37.             "https://lh3.googleusercontent.com/-6hZiEHXx64Q/URqurxvNdqI/AAAAAAAAAbs/kWMXM3o5OVI/s160-c/Green%252520Grass.jpg",  
  38.             "https://lh5.googleusercontent.com/-6LVb9OXtQ60/URquteBFuKI/AAAAAAAAAbs/4F4kRgecwFs/s160-c/Hanging%252520Leaf.jpg",  
  39.             "https://lh4.googleusercontent.com/-zAvf__52ONk/URqutT_IuxI/AAAAAAAAAbs/D_bcuc0thoU/s160-c/Highway%2525201.jpg",  
  40.             "https://lh6.googleusercontent.com/-H4SrUg615rA/URquuL27fXI/AAAAAAAAAbs/4aEqJfiMsOU/s160-c/Horseshoe%252520Bend%252520Sunset.jpg",  
  41.             "https://lh4.googleusercontent.com/-JhFi4fb_Pqw/URquuX-QXbI/AAAAAAAAAbs/IXpYUxuweYM/s160-c/Horseshoe%252520Bend.jpg",  
  42.             "https://lh5.googleusercontent.com/-UGgssvFRJ7g/URquueyJzGI/AAAAAAAAAbs/yYIBlLT0toM/s160-c/Into%252520the%252520Blue.jpg",  
  43.             "https://lh3.googleusercontent.com/-CH7KoupI7uI/URquu0FF__I/AAAAAAAAAbs/R7GDmI7v_G0/s160-c/Jelly%252520Fish%2525202.jpg",  
  44.             "https://lh4.googleusercontent.com/-pwuuw6yhg8U/URquvPxR3FI/AAAAAAAAAbs/VNGk6f-tsGE/s160-c/Jelly%252520Fish%2525203.jpg",  
  45.             "https://lh5.googleusercontent.com/-GoUQVw1fnFw/URquv6xbC0I/AAAAAAAAAbs/zEUVTQQ43Zc/s160-c/Kauai.jpg",  
  46.             "https://lh6.googleusercontent.com/-8QdYYQEpYjw/URquwvdh88I/AAAAAAAAAbs/cktDy-ysfHo/s160-c/Kyoto%252520Sunset.jpg",  
  47.             "https://lh4.googleusercontent.com/-vPeekyDjOE0/URquwzJ28qI/AAAAAAAAAbs/qxcyXULsZrg/s160-c/Lake%252520Tahoe%252520Colors.jpg",  
  48.             "https://lh4.googleusercontent.com/-xBPxWpD4yxU/URquxWHk8AI/AAAAAAAAAbs/ARDPeDYPiMY/s160-c/Lava%252520from%252520the%252520Sky.jpg",  
  49.             "https://lh3.googleusercontent.com/-897VXrJB6RE/URquxxxd-5I/AAAAAAAAAbs/j-Cz4T4YvIw/s160-c/Leica%25252050mm%252520Summilux.jpg",  
  50.             "https://lh5.googleusercontent.com/-qSJ4D4iXzGo/URquyDWiJ1I/AAAAAAAAAbs/k2pBXeWehOA/s160-c/Leica%25252050mm%252520Summilux.jpg",  
  51.             "https://lh6.googleusercontent.com/-dwlPg83vzLg/URquylTVuFI/AAAAAAAAAbs/G6SyQ8b4YsI/s160-c/Leica%252520M8%252520%252528Front%252529.jpg",  
  52.             "https://lh3.googleusercontent.com/-R3_EYAyJvfk/URquzQBv8eI/AAAAAAAAAbs/b9xhpUM3pEI/s160-c/Light%252520to%252520Sand.jpg",  
  53.             "https://lh3.googleusercontent.com/-fHY5h67QPi0/URqu0Cp4J1I/AAAAAAAAAbs/0lG6m94Z6vM/s160-c/Little%252520Bit%252520of%252520Paradise.jpg",  
  54.             "https://lh5.googleusercontent.com/-TzF_LwrCnRM/URqu0RddPOI/AAAAAAAAAbs/gaj2dLiuX0s/s160-c/Lone%252520Pine%252520Sunset.jpg",  
  55.             "https://lh3.googleusercontent.com/-4HdpJ4_DXU4/URqu046dJ9I/AAAAAAAAAbs/eBOodtk2_uk/s160-c/Lonely%252520Rock.jpg",  
  56.             "https://lh6.googleusercontent.com/-erbF--z-W4s/URqu1ajSLkI/AAAAAAAAAbs/xjDCDO1INzM/s160-c/Longue%252520Vue.jpg",  
  57.             "https://lh6.googleusercontent.com/-0CXJRdJaqvc/URqu1opNZNI/AAAAAAAAAbs/PFB2oPUU7Lk/s160-c/Look%252520Me%252520in%252520the%252520Eye.jpg",  
  58.             "https://lh3.googleusercontent.com/-D_5lNxnDN6g/URqu2Tk7HVI/AAAAAAAAAbs/p0ddca9W__Y/s160-c/Lost%252520in%252520a%252520Field.jpg",  
  59.             "https://lh6.googleusercontent.com/-flsqwMrIk2Q/URqu24PcmjI/AAAAAAAAAbs/5ocIH85XofM/s160-c/Marshall%252520Beach%252520Sunset.jpg",  
  60.             "https://lh4.googleusercontent.com/-Y4lgryEVTmU/URqu28kG3gI/AAAAAAAAAbs/OjXpekqtbJ4/s160-c/Mono%252520Lake%252520Blue.jpg",  
  61.             "https://lh4.googleusercontent.com/-AaHAJPmcGYA/URqu3PIldHI/AAAAAAAAAbs/lcTqk1SIcRs/s160-c/Monument%252520Valley%252520Overlook.jpg",  
  62.             "https://lh4.googleusercontent.com/-vKxfdQ83dQA/URqu31Yq_BI/AAAAAAAAAbs/OUoGk_2AyfM/s160-c/Moving%252520Rock.jpg",  
  63.             "https://lh5.googleusercontent.com/-CG62QiPpWXg/URqu4ia4vRI/AAAAAAAAAbs/0YOdqLAlcAc/s160-c/Napali%252520Coast.jpg",  
  64.             "https://lh6.googleusercontent.com/-wdGrP5PMmJQ/URqu5PZvn7I/AAAAAAAAAbs/m0abEcdPXe4/s160-c/One%252520Wheel.jpg",  
  65.             "https://lh6.googleusercontent.com/-6WS5DoCGuOA/URqu5qx1UgI/AAAAAAAAAbs/giMw2ixPvrY/s160-c/Open%252520Sky.jpg",  
  66.             "https://lh6.googleusercontent.com/-u8EHKj8G8GQ/URqu55sM6yI/AAAAAAAAAbs/lIXX_GlTdmI/s160-c/Orange%252520Sunset.jpg",  
  67.             "https://lh6.googleusercontent.com/-74Z5qj4bTDE/URqu6LSrJrI/AAAAAAAAAbs/XzmVkw90szQ/s160-c/Orchid.jpg",  
  68.             "https://lh6.googleusercontent.com/-lEQE4h6TePE/URqu6t_lSkI/AAAAAAAAAbs/zvGYKOea_qY/s160-c/Over%252520there.jpg",  
  69.             "https://lh5.googleusercontent.com/-cauH-53JH2M/URqu66v_USI/AAAAAAAAAbs/EucwwqclfKQ/s160-c/Plumes.jpg",  
  70.             "https://lh3.googleusercontent.com/-eDLT2jHDoy4/URqu7axzkAI/AAAAAAAAAbs/iVZE-xJ7lZs/s160-c/Rainbokeh.jpg",  
  71.             "https://lh5.googleusercontent.com/-j1NLqEFIyco/URqu8L1CGcI/AAAAAAAAAbs/aqZkgX66zlI/s160-c/Rainbow.jpg",  
  72.             "https://lh5.googleusercontent.com/-DRnqmK0t4VU/URqu8XYN9yI/AAAAAAAAAbs/LgvF_592WLU/s160-c/Rice%252520Fields.jpg",  
  73.             "https://lh3.googleusercontent.com/-hwh1v3EOGcQ/URqu8qOaKwI/AAAAAAAAAbs/IljRJRnbJGw/s160-c/Rockaway%252520Fire%252520Sky.jpg",  
  74.             "https://lh5.googleusercontent.com/-wjV6FQk7tlk/URqu9jCQ8sI/AAAAAAAAAbs/RyYUpdo-c9o/s160-c/Rockaway%252520Flow.jpg",  
  75.             "https://lh6.googleusercontent.com/-6cAXNfo7D20/URqu-BdzgPI/AAAAAAAAAbs/OmsYllzJqwo/s160-c/Rockaway%252520Sunset%252520Sky.jpg",  
  76.             "https://lh3.googleusercontent.com/-sl8fpGPS-RE/URqu_BOkfgI/AAAAAAAAAbs/Dg2Fv-JxOeg/s160-c/Russian%252520Ridge%252520Sunset.jpg",  
  77.             "https://lh6.googleusercontent.com/-gVtY36mMBIg/URqu_q91lkI/AAAAAAAAAbs/3CiFMBcy5MA/s160-c/Rust%252520Knot.jpg",  
  78.             "https://lh6.googleusercontent.com/-GHeImuHqJBE/URqu_FKfVLI/AAAAAAAAAbs/axuEJeqam7Q/s160-c/Sailing%252520Stones.jpg",  
  79.             "https://lh3.googleusercontent.com/-hBbYZjTOwGc/URqu_ycpIrI/AAAAAAAAAbs/nAdJUXnGJYE/s160-c/Seahorse.jpg",  
  80.             "https://lh3.googleusercontent.com/-Iwi6-i6IexY/URqvAYZHsVI/AAAAAAAAAbs/5ETWl4qXsFE/s160-c/Shinjuku%252520Street.jpg",  
  81.             "https://lh6.googleusercontent.com/-amhnySTM_MY/URqvAlb5KoI/AAAAAAAAAbs/pFCFgzlKsn0/s160-c/Sierra%252520Heavens.jpg",  
  82.             "https://lh5.googleusercontent.com/-dJgjepFrYSo/URqvBVJZrAI/AAAAAAAAAbs/v-F5QWpYO6s/s160-c/Sierra%252520Sunset.jpg",  
  83.             "https://lh4.googleusercontent.com/-Z4zGiC5nWdc/URqvBdEwivI/AAAAAAAAAbs/ZRZR1VJ84QA/s160-c/Sin%252520Lights.jpg",  
  84.             "https://lh4.googleusercontent.com/-_0cYiWW8ccY/URqvBz3iM4I/AAAAAAAAAbs/9N_Wq8MhLTY/s160-c/Starry%252520Lake.jpg",  
  85.             "https://lh3.googleusercontent.com/-A9LMoRyuQUA/URqvCYx_JoI/AAAAAAAAAbs/s7sde1Bz9cI/s160-c/Starry%252520Night.jpg",  
  86.             "https://lh3.googleusercontent.com/-KtLJ3k858eY/URqvC_2h_bI/AAAAAAAAAbs/zzEBImwDA_g/s160-c/Stream.jpg",  
  87.             "https://lh5.googleusercontent.com/-dFB7Lad6RcA/URqvDUftwWI/AAAAAAAAAbs/BrhoUtXTN7o/s160-c/Strip%252520Sunset.jpg",  
  88.             "https://lh5.googleusercontent.com/-at6apgFiN20/URqvDyffUZI/AAAAAAAAAbs/clABCx171bE/s160-c/Sunset%252520Hills.jpg",  
  89.             "https://lh4.googleusercontent.com/-7-EHhtQthII/URqvEYTk4vI/AAAAAAAAAbs/QSJZoB3YjVg/s160-c/Tenaya%252520Lake%2525202.jpg",  
  90.             "https://lh6.googleusercontent.com/-8MrjV_a-Pok/URqvFC5repI/AAAAAAAAAbs/9inKTg9fbCE/s160-c/Tenaya%252520Lake.jpg",  
  91.             "https://lh5.googleusercontent.com/-B1HW-z4zwao/URqvFWYRwUI/AAAAAAAAAbs/8Peli53Bs8I/s160-c/The%252520Cave%252520BW.jpg",  
  92.             "https://lh3.googleusercontent.com/-PO4E-xZKAnQ/URqvGRqjYkI/AAAAAAAAAbs/42nyADFsXag/s160-c/The%252520Fisherman.jpg",  
  93.             "https://lh4.googleusercontent.com/-iLyZlzfdy7s/URqvG0YScdI/AAAAAAAAAbs/1J9eDKmkXtk/s160-c/The%252520Night%252520is%252520Coming.jpg",  
  94.             "https://lh6.googleusercontent.com/-G-k7YkkUco0/URqvHhah6fI/AAAAAAAAAbs/_taQQG7t0vo/s160-c/The%252520Road.jpg",  
  95.             "https://lh6.googleusercontent.com/-h-ALJt7kSus/URqvIThqYfI/AAAAAAAAAbs/ejiv35olWS8/s160-c/Tokyo%252520Heights.jpg",  
  96.             "https://lh5.googleusercontent.com/-Hy9k-TbS7xg/URqvIjQMOxI/AAAAAAAAAbs/RSpmmOATSkg/s160-c/Tokyo%252520Highway.jpg",  
  97.             "https://lh6.googleusercontent.com/-83oOvMb4OZs/URqvJL0T7lI/AAAAAAAAAbs/c5TECZ6RONM/s160-c/Tokyo%252520Smog.jpg",  
  98.             "https://lh3.googleusercontent.com/-FB-jfgREEfI/URqvJI3EXAI/AAAAAAAAAbs/XfyweiRF4v8/s160-c/Tufa%252520at%252520Night.jpg",  
  99.             "https://lh4.googleusercontent.com/-vngKD5Z1U8w/URqvJUCEgPI/AAAAAAAAAbs/ulxCMVcU6EU/s160-c/Valley%252520Sunset.jpg",  
  100.             "https://lh6.googleusercontent.com/-DOz5I2E2oMQ/URqvKMND1kI/AAAAAAAAAbs/Iqf0IsInleo/s160-c/Windmill%252520Sunrise.jpg",  
  101.             "https://lh5.googleusercontent.com/-biyiyWcJ9MU/URqvKculiAI/AAAAAAAAAbs/jyPsCplJOpE/s160-c/Windmill.jpg",  
  102.             "https://lh4.googleusercontent.com/-PDT167_xRdA/URqvK36mLcI/AAAAAAAAAbs/oi2ik9QseMI/s160-c/Windmills.jpg",  
  103.             "https://lh5.googleusercontent.com/-kI_QdYx7VlU/URqvLXCB6gI/AAAAAAAAAbs/N31vlZ6u89o/s160-c/Yet%252520Another%252520Rockaway%252520Sunset.jpg",  
  104.             "https://lh4.googleusercontent.com/-e9NHZ5k5MSs/URqvMIBZjtI/AAAAAAAAAbs/1fV810rDNfQ/s160-c/Yosemite%252520Tree.jpg", };  
  105. }  


main.xml如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <GridView  
  7.         android:id="@+id/gridView"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:columnWidth="90dip"  
  11.         android:gravity="center"  
  12.         android:numColumns="auto_fit"  
  13.         android:stretchMode="columnWidth"  
  14.         android:verticalSpacing="10dip"  
  15.     />  
  16.   
  17. </RelativeLayout>  


gridview_item.xml如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content" >  
  5.   
  6.     <ImageView   
  7.         android:id="@+id/imageView"  
  8.         android:layout_width="90dip"  
  9.         android:layout_height="90dip"  
  10.         android:src="@drawable/default_image"  
  11.         android:layout_centerInParent="true"  
  12.         />  
  13.   
  14. </RelativeLayout>  
============================================================================
0 0
原创粉丝点击