在网上找了好久似乎都没有关于这方面的(可能是自己的信息量太小吧),于是自己来填补这个漏洞吧。
常见的方法莫过于自己定义一个数组,用以存储图片的引用,如:
1 | private Integer[] pictures = { |
2 | R.drawable.icon1, |
3 | R.drawable.icon2, |
4 | R.drawable.icon3, |
5 | R.drawable.icon4, |
6 | R.drawable.icon5 |
7 | }; |
首先,你得知道你图片的存储路径,将这些你将要显示的图片的路径存放于一个arraylist里面
1 | ArrayList<String> PictureNameList = new ArrayList<String>(); |
2 | PicureNameList.add( "路径" ); |
3 | ... |
4 | ... |
1 | Gallery g = (Gallery) findViewById(R.id.mygallery); |
01 | <?xml version= "1.0" encoding= "utf-8" ?> |
02 | <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" |
03 | android:layout_width= "fill_parent" |
04 | android:layout_height= "fill_parent" |
05 | android:orientation= "vertical" |
06 | > |
07 |
08 | <!-- 建立一個 Gallery --> |
09 | <Gallery |
10 | android:id= "@+id/mygallery" |
11 | android:layout_width= "fill_parent" |
12 | android:layout_height= "wrap_content" |
13 | android:layout_x= "12px" |
14 | android:layout_y= "106px" |
15 | /> |
16 | |
17 | |
18 | </LinearLayout> |
并加入到适配器里面
01 | g.setAdapter( new ImageAdapter( this ,PictureNameList)); |
02 |
03 | /*设定一个itemclickListener事件*/ |
04 | g.setOnItemClickListener( new OnItemClickListener() |
05 | { |
06 | public void onItemClick(AdapterView<?> parent, |
07 | View v, int position, long id) |
08 | { |
09 |
10 | //这里就根据你自己的需要去做一些功能的展示 |
11 | } |
12 | }); |
01 | public class ImageAdapter extends BaseAdapter |
02 | { |
03 | /*声明变量*/ |
04 | int mGalleryItemBackground; |
05 | private Context mContext; |
06 | private List<String> lis; |
07 | |
08 | /*ImageAdapter的构造符*/ |
09 | public ImageAdapter(Context c,List<String> li) |
10 | { |
11 | mContext = c; |
12 | lis=li; |
13 | /* 使用res/values/attrs.xml中的<declare-styleable>定义 |
14 | * 的Gallery属性.*/ |
15 | TypedArray a = obtainStyledAttributes(R.styleable.Gallery); |
16 | /*取得Gallery属性的Index id*/ |
17 | mGalleryItemBackground = a.getResourceId( |
18 | R.styleable.Gallery_android_galleryItemBackground, 0); |
19 | /*让对象的styleable属性能够反复使用*/ |
20 | a.recycle(); |
21 | } |
22 | |
23 | /*几定要重写的方法getCount,传回图片数目*/ |
24 | public int getCount() |
25 | { |
26 | return lis.size(); |
27 | } |
28 | |
29 | /*一定要重写的方法getItem,传回position*/ |
30 | public Object getItem(int position) |
31 | { |
32 | return position; |
33 | } |
34 | |
35 | /*一定要重写的方法getItemId,传并position*/ |
36 | public long getItemId(int position) |
37 | { |
38 | return position; |
39 | } |
40 | |
41 | /*几定要重写的方法getView,传并几View对象*/ |
42 | public View getView(int position, View convertView, |
43 | ViewGroup parent) |
44 | { |
45 | /*产生ImageView对象*/ |
46 | ImageView i = new ImageView(mContext); |
47 | /*设定图片给imageView对象*/ |
48 | Bitmap bm = BitmapFactory.decodeFile(lis. |
49 | get(position).toString()); |
50 | i.setImageBitmap(bm); |
51 | /*重新设定图片的宽高*/ |
52 | i.setScaleType(ImageView.ScaleType.FIT_XY); |
53 | /*重新设定Layout的宽高*/ |
54 | i.setLayoutParams(new Gallery.LayoutParams(200, 120)); |
55 | /*设定Gallery背景图*/ |
56 | i.setBackgroundResource(mGalleryItemBackground); |
57 | /*传回imageView对象*/ |
58 | return i; |
59 | } |
60 | } |
61 | } |
其中只需要关注最后一个getView函数,这个函数的关键就在于其中两行
1 | Bitmap bm = BitmapFactory.decodeFile(lis. |
2 | get(position).toString()); |
3 | i.setImageBitmap(bm); |
记得在values文件下里面添加一个叫做attrs.xml的文件
1 | <?xml version= "1.0" encoding= "utf-8" ?> |
2 | <resources> |
3 | <declare-styleable name= "Gallery" > |
4 | <attr name= "android:galleryItemBackground" /> |
5 | </declare-styleable> |
6 | </resources> |
1 | <uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" /> |
2 | <uses-permission android:name= "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> |
源码下载