图库xml界面代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/re_photo_wall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="6dp"
android:paddingLeft="6dp"/>
LinearLayout>
其中用recyclerView 来加载图片设置与上方间隔6dp
paddingLeft的功能下面会讲到
PhotoWallActivity代码
public class PhotoWallActivity extends AppCompatActivity {
private HashMap> mGruopMap = new HashMap>();
private List imgs = new ArrayList<>();
private final static int SCAN_OK = 1;
private ProgressDialog mProgressDialog;
private RecyclerView rePhotoWall;
private PhotoWallAdapter adapter;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case SCAN_OK:
//扫描完毕,关闭进度dialog
mProgressDialog.dismiss();
adapter.notifyDataSetChanged();
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo_wall);
initView();
}
private void initView() {
rePhotoWall = (RecyclerView) findViewById(R.id.re_photo_wall);
//设置recyclerView 为网格布局,3列
rePhotoWall.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
adapter = new PhotoWallAdapter(PhotoWallActivity.this, imgs);
rePhotoWall.setAdapter(adapter);
if (imgs.size() == 0) {
getImages();
}
}
/**
* 利用ContentProvider扫描手机中的图片,此方法在运行在子线程中
*/
private void getImages() {
//显示进度dialog
mProgressDialog = ProgressDialog.show(this, null, "正在加载...");
//开启线程扫描
new Thread(new Runnable() {
@Override
public void run() {
Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver mContentResolver = PhotoWallActivity.this.getContentResolver();
//只查询jpeg和png的图片
Cursor mCursor = mContentResolver.query(mImageUri, null,
MediaStore.Images.Media.MIME_TYPE + "=? or "
+ MediaStore.Images.Media.MIME_TYPE + "=?",
new String[]{"image/jpeg", "image/png"}, MediaStore.Images.Media.DATE_MODIFIED);
if (mCursor == null) {
return;
}
while (mCursor.moveToNext()) {
//获取图片的路径
String path = mCursor.getString(mCursor
.getColumnIndex(MediaStore.Images.Media.DATA));
ImageBean bean = new ImageBean(path, false);
imgs.add(bean);
//获取该图片的父路径名
String parentName = new File(path).getParentFile().getName();
//根据父路径名将图片放入到mGruopMap中
if (!mGruopMap.containsKey(parentName)) {
List childList = new ArrayList();
ImageBean imageBean = new ImageBean(path, false);
childList.add(imageBean);
mGruopMap.put(parentName, childList);
} else {
mGruopMap.get(parentName).add(new ImageBean(path, false));
}
}
//添加全部图片的集合
mGruopMap.put("全部图片", imgs);
//通知Handler扫描图片完成
mHandler.sendEmptyMessage(SCAN_OK);
mCursor.close();
}
}).start();
}
/**
* 组装分组界面的数据源,因为我们扫描手机的时候将图片信息放在HashMap中
* 所以需要遍历HashMap将数据组装成List
* 用于展示图库分组列表
* @param mGruopMap
* @return
*/
private List subGroupOfImage(HashMap> mGruopMap) {
if (mGruopMap.size() == 0) {
return null;
}
//遍历
List list = new ArrayList();
Iterator>> it = mGruopMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry> entry = it.next();
ImageGroupBean mImageBean = new ImageGroupBean();
//根据key获取其中图片list
String key = entry.getKey();
List value = entry.getValue();
mImageBean.setFolderName(key);//获取该组文件夹名称
mImageBean.setImageCounts(value.size());//获取该组图片数量
mImageBean.setTopImagePath(value.get(0).getImgPath());//获取该组的第一张图片
//将全部图片放在第一位置
if (mImageBean.getFolderName().equals("全部图片")){
list.add(0,mImageBean);
}else {
list.add(mImageBean);
}
}
return list;
}
}
在PhotoWallActivity我获取了本地所有图片,并对图片按存储路径进行了分组存放到HashMap中。
其中subGroupOfImage方法,将HashMap中每组的第一张图片、图片数量、以及父文件名存放到list中,提供后面选择分组展示使用(目前没做,但数据都有了)。
ImageBean与ImageGroupBean 代码
public class ImageBean {
private String imgPath;
private boolean isChoosed;
public ImageBean(String imgPath, boolean isChoosed) {
this.imgPath = imgPath;
this.isChoosed = isChoosed;
}
public String getImgPath() {
return imgPath;
}
public void setImgPath(String imgPath) {
this.imgPath = imgPath;
}
public boolean isChoosed() {
return isChoosed;
}
public void setChoosed(boolean choosed) {
isChoosed = choosed;
}
}
public class ImageGroupBean {
/**
* 文件夹的第一张图片路径
*/
private String topImagePath;
/**
* 文件夹名
*/
private String folderName;
/**
* 文件夹中的图片数
*/
private int imageCounts;
public String getTopImagePath() {
return topImagePath;
}
public void setTopImagePath(String topImagePath) {
this.topImagePath = topImagePath;
}
public String getFolderName() {
return folderName;
}
public void setFolderName(String folderName) {
this.folderName = folderName;
}
public int getImageCounts() {
return imageCounts;
}
public void setImageCounts(int imageCounts) {
this.imageCounts = imageCounts;
}
}
PhotoWallAdapter的代码:
public class PhotoWallAdapter extends RecyclerView.Adapter<PhotoWallAdapter.PhotoWallViewHolder> {
private Context context;
private List imgs;
public PhotoWallAdapter(Context context, List imgs) {
this.context = context;
this.imgs = imgs;
}
@Override
public PhotoWallViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.view_photo_item, parent, false);
return new PhotoWallViewHolder(view);
}
@Override
public void onBindViewHolder(PhotoWallViewHolder holder, int position) {
//图片每行3个,则有4个间隔,每个6dp,就为24dp(前面的paddingLeft 6dp为第一个左边的间距),这样得出每个图片的宽度
holder.ivPhoto.setLayoutParams(new FrameLayout.LayoutParams((ScreenUtils.getScreenWidth(context) - SizeUtils.dp2px(context, 24)) / 3,
(ScreenUtils.getScreenWidth(context) - SizeUtils.dp2px(context, 24)) / 3));
//glide加载
Glide.with(context)
.load(imgs.get(position).getImgPath())
.centerCrop()
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.placeholder(R.drawable.default_img)
.into(holder.ivPhoto);
}
@Override
public int getItemCount() {
return imgs.size();
}
static class PhotoWallViewHolder extends RecyclerView.ViewHolder {
ImageView ivPhoto;
ImageView ivChoose;
public PhotoWallViewHolder(View itemView) {
super(itemView);
ivChoose = (ImageView) itemView.findViewById(R.id.iv_choose);
ivPhoto = (ImageView) itemView.findViewById(R.id.iv_photo);
}
}
}
规定每张图片的宽高,然后用glide加载。
view_photo_item.xml界面:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" />
<ImageView
android:id="@+id/iv_choose"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="right|top"
android:layout_margin="2dp"
android:src="@mipmap/un_choose" />
FrameLayout>
在父布局中设置android:layout_marginBottom=”6dp”,实现上下的间隔。
到这里,基本的图库功能就可以实现了,但是还有一些功能没做,例如上面提到的分组展示、图片多选,以后有时间的话就把他补上。