高仿微信相册(整理)
来源:互联网 发布:阿里云ecs linux教程 编辑:程序博客网 时间:2024/06/05 15:29
对于相册展示,多选图片以及最多选择限制张数,在以前的项目中也做过。只是没有整理,最近有点事情,就梳理下,方便以后学习使用,大神请自行掠过。
注明,本demo是参考鸿洋大神的 http://blog.csdn.net/lmj623565791/article/details/39943731/
首先上效果图
上面就是最终效果了,UI有点丑,请见谅,毕竟不是美工。只能从网上找点图片来充数。
下面直接看实现代码吧。文笔是在不是很好。哈哈~~~~~
Activity的布局就不贴上了,很简单就是一个GridView,底部用一个相对布局。
扫描SD卡上的图片,分文件夹保存,因为是耗时操作,所以在线程中操作,完成后handler通知。
/** * 获取SD中的jpg png文件 */ private void getPhotos() { if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { ToastUtils.show(this, "暂无外部存储"); return; } progressDialog = ProgressDialog.show(this, null, "正在扫描照片"); new Thread(new Runnable() { @Override public void run() { String firstImage = null; Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; ContentResolver mContentResolver = PhotoActivity.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); while (mCursor.moveToNext()) { // 获取图片的路径 String path = mCursor.getString(mCursor .getColumnIndex(MediaStore.Images.Media.DATA)); imagePaths.add(path); // 拿到第一张图片的路径 if (firstImage == null) firstImage = path; // 获取该图片的父路径名 File parentFile = new File(path).getParentFile(); if (parentFile == null) continue; String dirPath = parentFile.getAbsolutePath(); ImageFloder imageFloder = null; // 利用一个HashSet防止多次扫描同一个文件夹(不加这个判断,图片多起来还是相当恐怖的~~) if (mDirPaths.contains(dirPath)) { continue; } else { mDirPaths.add(dirPath); // 初始化imageFloder imageFloder = new ImageFloder(); imageFloder.setDir(dirPath); imageFloder.setFirstImagePath(path); } int picSize = parentFile.list(new FilenameFilter() { @Override public boolean accept(File dir, String filename) { if (filename.endsWith(".jpg") || filename.endsWith(".png") || filename.endsWith(".jpeg")) return true; return false; } }).length; totalCount += picSize; imageFloder.setCount(picSize); mImageFloders.add(imageFloder); if (picSize > mPicsSize) { mPicsSize = picSize; mImgDir = parentFile; } } mCursor.close(); // 扫描完成,辅助的HashSet也就可以释放内存了 mDirPaths = null; // 通知Handler扫描图片完成 handler.sendEmptyMessage(SCAN_OVER); } }).start(); }
上面操作就是扫描完成后,hadnler通知已经完成,然后进行加载数据,设置Adapter。默认展示的是所有的图片,并且需要添加一个相机的位置。
private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case SCAN_OVER: progressDialog.dismiss(); //添加相机位置 imagePaths.add(0, ""); //添加所有图片文件夹 ImageFloder imageFloder = new ImageFloder(); imageFloder.setCount(totalCount); imageFloder.setFirstImagePath(imagePaths.get(0)); imageFloder.setDir(Environment.getExternalStorageDirectory().getAbsolutePath()); mImageFloders.add(0, imageFloder); //数据绑定View data2View(); //初始化PopupWindow initPopupWindow(); break; } } };
填充数据,设置gridView的adapter
/** * 数据绑定View */ private void data2View() { if (mImgDir == null) { ToastUtils.showMessage(getApplicationContext(), "一张也没有"); return; } photoAdapter = new PhotoAdapter(this, imagePaths, R.layout.item_all_img, null); photoAdapter.selectMax(9); gridView.setAdapter(photoAdapter); tvCount.setText(totalCount + "张"); }
图片展示的Adapter
/** * 相片展示的填充器 * Created by Sunshine on 2016/9/9. */public class PhotoAdapter extends CommonAdapter<String> { private String dirPath; private int maxValue; private ArrayList<String> selectPathList = new ArrayList<>(); private ArrayList<String> dataList; public PhotoAdapter(Context context, ArrayList<String> dataList, int itemLayoutId, String dirPath) { super(context, dataList, itemLayoutId); this.dirPath = dirPath; this.dataList = dataList; } public void selectMax(int max) { this.maxValue = max; } @Override protected void convert(final ViewHolder viewHolder, final String item) { //初始化图片 viewHolder.setImageView(R.id.item_image_view, R.mipmap.zw); //查找控件 final ImageView checkBox = viewHolder.getView(R.id.child_checkbox); final ImageView imageView = viewHolder.getView(R.id.item_image_view); //加载图片 if (TextUtils.isEmpty(item)) { viewHolder.setImageView(R.id.item_image_view, R.mipmap.asv); checkBox.setVisibility(View.GONE); } else { checkBox.setVisibility(View.VISIBLE); if (TextUtils.isEmpty(dirPath)) { viewHolder.setImageView(R.id.item_image_view, item); } else { viewHolder.setImageView(R.id.item_image_view, dirPath + "/" + item); } } //点击事件 checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AnimUtils.addAnimation(checkBox); if (selectPathList.contains(TextUtils.isEmpty(dirPath) ? item : (dirPath + "/" + item))) { selectPathList.remove(TextUtils.isEmpty(dirPath) ? item : (dirPath + "/" + item)); checkBox.setImageResource(R.drawable.select_normal); imageView.setColorFilter(null); }else { if (selectPathList.size() >= maxValue) { ToastUtils.showMessage(context.getApplicationContext(), "选择相片最大数不能超过" + maxValue + "张~~~"); return; } selectPathList.add(TextUtils.isEmpty(dirPath) ? item : (dirPath + "/" + item)); checkBox.setImageResource(R.drawable.select_pressed); imageView.setColorFilter(Color.parseColor("#77000000")); } } }); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (TextUtils.isEmpty(item)&&viewHolder.getPosition()==0){ ToastUtils.showMessage(context.getApplicationContext(),"打开相机"); return; } Bundle bundle = new Bundle(); bundle.putSerializable("paths",dataList); bundle.putInt("position",viewHolder.getPosition()); IntentUtils.switchActivity(context,ShowPhotoActivity.class,bundle); } }); if (selectPathList.contains(TextUtils.isEmpty(dirPath) ? item : (dirPath + "/" + item))) { checkBox.setImageResource(R.drawable.select_pressed); imageView.setColorFilter(Color.parseColor("#77000000")); }else { checkBox.setImageResource(R.drawable.select_normal); imageView.setColorFilter(null); } }}
文件夹展示的popupWindow
/** * PopupWindow实现类 * Created by Sunshine on 2016/9/8. */public class ListImageDirPopupWindow extends BasePopupWindowForListView<ImageFloder> { private ListView dirListView; public ListImageDirPopupWindow(View contentView, int width, int height, List<ImageFloder> mDatas) { super(contentView, width, height, true, mDatas); } @Override protected void beforeInitWeNeedSomeParams(Object... params) { } @Override public void initViews() { dirListView = (ListView) findViewById(R.id.list_view); dirListView.setAdapter(new CommonAdapter<ImageFloder>(context,mDatas,R.layout.list_dir_item) { @Override protected void convert(ViewHolder viewHolder, ImageFloder item) { viewHolder.setText(R.id.id_dir_item_name,item.getName()); viewHolder.setText(R.id.id_dir_item_count,item.getCount()+"张"); viewHolder.setImageView(R.id.id_dir_item_image,item.getFirstImagePath()); } }); } public OnSelectDirListener onSelectDirListener; public void setOnSelectDirListener(OnSelectDirListener onSelectDirListener){ this.onSelectDirListener = onSelectDirListener; } /** * 选择文件夹监听 */ public interface OnSelectDirListener{ void selectDir(ImageFloder imageFloder,int position); } @Override public void initEvents() { dirListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { if (onSelectDirListener!=null){ onSelectDirListener.selectDir(mDatas.get(i),i); } } }); } @Override public void init() { }}
Activity根据选择的文件夹进行刷新数据
popupWindow.setOnSelectDirListener(new ListImageDirPopupWindow.OnSelectDirListener() { @Override public void selectDir(ImageFloder imageFloder, int position) { popupWindow.dismiss(); if (position == 0) { photoAdapter = new PhotoAdapter(PhotoActivity.this, imagePaths, R.layout.item_all_img, null); photoAdapter.selectMax(9); gridView.setAdapter(photoAdapter); } else { mImgDir = new File(imageFloder.getDir()); List<String> pathList = Arrays.asList(mImgDir.list(new FilenameFilter() { @Override public boolean accept(File dir, String filename) { if (filename.endsWith(".jpg") || filename.endsWith(".png") || filename.endsWith(".jpeg")) return true; return false; } })); if (selectImageList == null) { selectImageList = new ArrayList<String>(); } else { selectImageList.clear(); } for (String str : pathList) { selectImageList.add(str); } photoAdapter = new PhotoAdapter(PhotoActivity.this, selectImageList, R.layout.item_all_img, mImgDir.getAbsolutePath()); photoAdapter.selectMax(9); gridView.setAdapter(photoAdapter); } } });
以上就是基本的内容。demo后续会上传,请见谅!如有建议,多多指教!谢谢!!!
本人是新手,有不当之处还请见谅。
0 0
- 高仿微信相册(整理)
- 从相机或相册获取照片并进行裁剪(网上代码整理)
- 从相机或相册获取照片并进行裁剪(网上代码整理)
- 在手机相册(ios设备相册)中创建相册
- 自定义相机、相册(相册篇)
- 动态相册(未完成)
- 相册
- 相册
- 相册
- 相册
- 相册
- 相册
- 相册
- 相册
- 关于 HTML5 调用用户的 照相机、相册 初步整理
- 整理---从手机相册获取图片的方法
- 【整理】Cordova 从相册中选择照片/拍照并上传
- 仿QQ聊天界面里边的相册(QQ相册)
- linux下shell显示-bash-4.1$ 不显示路径解决方法
- 利用Caffe做回归(regression)、多标签训练
- Android事件的分发
- MPC8313开发板学习(一)SecureCRT:找不到端口号
- 使用jQuery操作Cookies的实现代码
- 高仿微信相册(整理)
- 一个华科研究生导师的肺腑之言(主要适用于理工科)
- Adobe Flash player折腾好久,ubuntu chromium 浏览器终于可以看视频
- iOS重写系统中的UINavigationController返回按钮的事件
- JavaScript中判断对象类型的种种方法
- Java中的单例
- Cocos2d-x的跨平台原理
- SendMessage用法
- sql语句解析实现