在网上找了好久似乎都没有关于这方面的(可能是自己的信息量太小吧),于是自己来填补这个漏洞吧。

常见的方法莫过于自己定义一个数组,用以存储图片的引用,如:

1 privateInteger[] 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 =newArrayList<String>();
2 PicureNameList.add("路径");
3 ...
4 ...

然后获取gallery实例,

1 Gallery g = (Gallery) findViewById(R.id.mygallery);

相应的mygallery.xml

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(newImageAdapter(this,PictureNameList));
02
03 /*设定一个itemclickListener事件*/
04 g.setOnItemClickListener(newOnItemClickListener()
05 {
06 publicvoidonItemClick(AdapterView<?> parent,
07 View v,intposition,longid)
08 {
09
10 //这里就根据你自己的需要去做一些功能的展示
11 }
12 });

下面就来看这个ImageAdaper实现

01 publicclassImageAdapterextendsBaseAdapter
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 returni;
59 }
60 }
61 }

其中只需要关注最后一个getView函数,这个函数的关键就在于其中两行

1 Bitmap bm = BitmapFactory.decodeFile(lis.
2 get(position).toString());
3 i.setImageBitmap(bm);

获取图片并显示图片。ok!

记得在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"/>



源码下载


更多相关文章

  1. gallary滑动切换图片
  2. 对于各种分辨率手机的测试学习
  3. Android(安卓)分辨率 及 px, dip相互转换
  4. Android:通过build gradle中的buildTypes设置自动log开关(二)
  5. 调用Android原生裁剪方式裁剪图片并保存
  6. Android高仿IOS和QQ的弹出对话框
  7. Android调用系统相册和相机选择图片并显示在imageview中
  8. Android相机、相册获取图片,解决相机拍照图片被压缩模糊的情况
  9. 【设计模式与Android】迭代器模式——容器遍历,细节隐藏

随机推荐

  1. drawable( hdpi, ldpi, mdpi, xhdpi)区别
  2. [置顶] Android开源工具项目集合
  3. Android(安卓)studio http请求获取数据失
  4. Android下使用Logcat打印信息
  5. 安卓基础学习(android studio)
  6. Android浸入式
  7. android MultiDex
  8. Android(安卓)- MPTCP - (./net/ipv4/Kco
  9. Android(安卓)Activity 四种启动模式
  10. Fragment中跨线程调用控件的问题