隨筆 - 0, 文章 - 264, 評論 - 170, 引用 - 0
          數據加載中……

          android中展示圖片

           1    1public class Images extends Activity  
           2    2. {  
           3    3.     private Uri[] mUrls;  
           4    4.     String[] mFiles=null;  
           5    5.   
           6    6.     public void onCreate(Bundle icicle)  
           7    7.     {  
           8    8.   
           9    9.         super.onCreate(icicle);  
          10   10.         setContentView(R.layout.images);  
          11   11.   
          12   12.         File images = Environment.getDataDirectory();  
          13   13.         File[] imagelist = images.listFiles(new FilenameFilter(){  
          14   14.         @override  
          15   15.         public boolean accept(File dir, String name)  
          16   16.         {  
          17   17.             return ((name.endsWith(".jpg"))||(name.endsWith(".png"))  
          18   18.         }  
          19   19.     });  
          20   20.         mFiles = new String[imagelist.length];  
          21   21.   
          22   22.         for(int i= 0 ; i< imagelist.length; i++)  
          23   23.         {  
          24   24.             mFiles[i] = imagelist[i].getAbsolutePath();  
          25   25.         }  
          26   26.         mUrls = new Uri[mFiles.length];  
          27   27.   
          28   28.         for(int i=0; i < mFiles.length; i++)  
          29   29.         {  
          30   30.             mUrls[i] = Uri.parse(mFiles[i]);     
          31   31.         }     
          32   32.   
          33   33.         Gallery g = (Gallery) findViewById(R.id.gallery);  
          34   34.         g.setAdapter(new ImageAdapter(this));  
          35   35.         g.setFadingEdgeLength(40);  
          36   36.     }  
          37   37.     public class ImageAdapter extends BaseAdapter{  
          38   38.           
          39   39.         int mGalleryItemBackground;  
          40   40.         public ImageAdapter(Context c)  {     
          41   41.             mContext = c;     
          42   42.         }  
          43   43.         public int getCount(){  
          44   44.             return mUrls.length;  
          45   45.         }  
          46   46.         public Object getItem(int position){  
          47   47.             return position;  
          48   48.         }  
          49   49.         public long getItemId(int position) {  
          50   50.             return position;  
          51   51.         }  
          52   52.         public View getView(int position, View convertView, ViewGroup parent){  
          53   53.             ImageView i = new ImageView(mContext);  
          54   54.   
          55   55.             i.setImageURI(mUrls[position]);  
          56   56.             i.setScaleType(ImageView.ScaleType.FIT_XY);  
          57   57.             i.setLayoutParams(new Gallery.LayoutParams(260210));  
          58   58.             return i;  
          59   59.         }     
          60   60.         private Context mContext;  
          61   61.         }     
          62   62.     } 

          posted on 2010-10-26 09:34 小一敗涂地 閱讀(1988) 評論(0)  編輯  收藏 所屬分類: android+移動開發

          主站蜘蛛池模板: 承德市| 莫力| 双鸭山市| 孙吴县| 万全县| 方正县| 仙居县| 通辽市| 丹阳市| 左权县| 建始县| 昌邑市| 德钦县| 清远市| 浮梁县| 夏河县| 天峻县| 宕昌县| 莒南县| 天长市| 普定县| 武宁县| 门头沟区| 沧州市| 获嘉县| 武义县| 奉新县| 萨嘎县| 嘉禾县| 原平市| 九台市| 屏东市| 宝应县| 郧西县| 浦东新区| 桃园县| 丰都县| 敖汉旗| 庄浪县| 方正县| 湖南省|