在这,我使用了开源项目imageLoader来加载显示大量图片,关于该开源项目的使用,你可以参考下我的上两篇博文,这里主要是写自定义相册的流程。
开始之前我们必须做的准备:
1.引入开源项目的jar。
2.清单文件中添加访问权限:
复制代码
1
2
3<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
获取包含图片的文件夹信息:
1.图片信息数组:包含了图片的基本信息。
复制代码
2.获取相册:通过数据库,获取包含图片的游标。
1
2
3
4
5
6
7
8
9
10/* * 获取图片的字段信息 */ static final String[] STORE_IMAGES = { MediaStore.Images.Media.DISPLAY_NAME, // 名称 MediaStore.Images.Media.DATA, MediaStore.Images.Media.LONGITUDE, // 经度 MediaStore.Images.Media._ID, // id MediaStore.Images.Media.BUCKET_ID, // dir id 目录 MediaStore.Images.Media.BUCKET_DISPLAY_NAME // dir name 目录名字 };
复制代码
3.相册的属性:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35/* * 按相册获取图片信息 */ static List<PhotoAibum> getPhotoAlbum(Context context) { List<PhotoAibum> aibumList = new ArrayList<PhotoAibum>(); Cursor cursor = MediaStore.Images.Media.query( context.getContentResolver(), MediaStore.Images.Media.EXTERNAL_CONTENT_URI, STORE_IMAGES); Map<String, PhotoAibum> countMap = new HashMap<String, PhotoAibum>(); PhotoAibum pa = null; while (cursor.moveToNext()) { String path = cursor.getString(1); String id = cursor.getString(3); String dir_id = cursor.getString(4); String dir = cursor.getString(5); if (!countMap.containsKey(dir_id)) { pa = new PhotoAibum(); pa.setName(dir); pa.setBitmap(Integer.parseInt(id)); pa.setCount("1"); pa.getBitList().add(new PhotoItem(Integer.valueOf(id), path)); countMap.put(dir_id, pa); } else { pa = countMap.get(dir_id); pa.setCount(String.valueOf(Integer.parseInt(pa.getCount()) + 1)); pa.getBitList().add(new PhotoItem(Integer.valueOf(id), path)); } } cursor.close(); Iterable<String> it = countMap.keySet(); for (String key : it) { aibumList.add(countMap.get(key)); } return aibumList; }
复制代码
1
2
3
4
5
6
7
8public class PhotoAibum implements Serializable{ private static final long serialVersionUID = 1L; private String name; //相册名字 private String count; //数量 private int bitmap; // 相册第一张图片
通过以上的方法,我们就可以得到关于相册和图片的信息,然后在适配器中合理的布局自己的相册了。
查看某相册下的所有图片:
1.由于传递List集合:你选中了想查看的相册时,需要告诉下一个活动你选中的是哪个相册。由于Intent传递的数据不易过多,我这里使用了全局变量,来存储选中的相册号。
复制代码
2.预览功能:给每个显示的图片添加一个标签,来显示是否被选中。选中的图片,将其地址存放到一个地址的集合中,传递给预览的界面。
1static int NumberAlbum = 0;
复制代码
由于动态加载的原因,当该视图被回收后,重新加载的时候,添加的标签就会被清除掉,我们需要在绘制视图的时候,判断该路径是否已经存放在选中地址的集合中。
1
2
3
4
5
6
7
8
9
10// 查看集合是否包含该地址 if (!clickList.contains(picturesList.get(position).getPath())) { cView.setBackgroundResource(R.drawable.cb_on); // 将地址添加到集合中 clickList.add(picturesList.get(position).getPath()); } else { cView.setBackgroundResource(R.drawable.cb_normal); // 移除添加过的地址 clickList.remove(picturesList.get(position).getPath()); }
复制代码
1
2
3
4
5
6
7
8
9// 查看集合是否包含该地址 if (clickList.contains(picturesList.get(position).getPath())) { holder.clickView.setBackgroundResource(R.drawable.cb_on); } else { holder.clickView.setBackgroundResource(R.drawable.cb_normal); } imageLoader.displayImage("file://" + picturesList.get(position).getPath(), holder.imageView, options);
预览选中的图片:
接受选中的图片地址,并将根据图片地址将图片显示在ViewPager中。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29class VPagerAdap extends PagerAdapter { @Override public int getCount() { // TODO Auto-generated method stub return picturesList.size(); } @Override public Object instantiateItem(ViewGroup container, int position) { // // TODO Auto-generated method stub View view = getLayoutInflater().inflate(R.layout.item_pager_img, null); ZoomImageView zoomImageView = (ZoomImageView) view .findViewById(R.id.zoom_image_view); // 添加缩放 zoomImageView.setImageBitmap(imageLoader.loadImageSync("file://" + picturesList.get(position), options)); container.addView(view); return view; } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } @Override public void destroyItem(ViewGroup container, int position, Object object) { View view = (View) object; container.removeView(view); } }
zoomImageView 是自定义的View,可以缩放图片。
效果:
1.所有的相册:
2.查看某个相册下的所有图片:
3.预览选中的图片:
点击下载资源
最后
以上就是谦让蓝天最近收集整理的关于一步一步的写自己的相册的全部内容,更多相关一步一步内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复