Android图片选择器ImageEditContainer

1. 简介

本次demo中一共封装了两个组件:ImageEditButton 和 ImageEditContainer。其中ImageEditContainer 是在 ImageEditButton,两个组件可单独使用。

在demo中,实现了 图片选择(拍照+本地),裁剪,压缩,保存本地 以及对已选择图片的删除操作(如果有修改需求,也可以使用对应方法进行操作,该方法已添加);

还有就是 针对 6.0权限的处理问题,本次使用了第三方库 rxpermissions 进行权限的处理。

2.项目主目录结构

3. 功能介绍

MainActivity.java 界面效果图:

ImageEditContainer 组件初始化:

layImageContainer = (ImageEditContainer) findViewById(R.id.lay_image_container); layImageContainer.setEditListener(this); layImageContainer.setBtnImageResource(R.drawable.icon_picture_photograph); layImageContainer.setTotalImageQuantity(3);

如上代码,设置组件的监听,添加按钮展示图,以及最多选择图片个数。

implements  ImageEditContainer.ImageEditContainerListener 的实现

@Override public void doAddImage() { PopupWindow mCameraPop = SelectPicturePopupWindowUtils.showSelectPicturePopupWindow(this); if (mCameraPop != null) mCameraPop.showAtLocation(layImageContainer, Gravity.BOTTOM, 0, 0); } @Override public void doEditLocalImage(ImageItem imageItem) { if (imageItem != null) { layImageContainer.updateEditedImageItem(imageItem); } } @Override public void doEditRemoteImage(RemoteImageItem remoteImageItem) { if (remoteImageItem != null) { if (remoteImageItem.isDeleted) { layImageContainer.removeRemoteImageItem(remoteImageItem); } else { layImageContainer.updateRemoteImageItem(remoteImageItem); } } }

当图片选择数量达到最大个数时,添加按钮会消失。效果图如下所示:

图片裁剪 效果图如下所示:

图片可拖拽,缩放

图片选择好后,进行图片压缩:

private void compressImage(String path) { if (TextUtils.isEmpty(path)) { return; } compressImage = compressImage + 1; ImageItem imageItem = new ImageItem(); imageItem.storedPath = path; File file = new File(FilePathUtils.getImageSavePath()); if (!file.exists()) { file.mkdirs(); } String filePath = FilePathUtils.getImageSavePath() + System.currentTimeMillis() + ".jpg"; new Thread(new MyThread(imageItem, path, filePath)).start(); List<String> imagePaths = new ArrayList<>(); imagePaths.add(path); layImageContainer.addNewImageItem(imageItem); }

图片压缩比较慢,要开启个 线程进行压缩:

public class MyThread implements Runnable { private String imgPath; private String outPath; private ImageItem imageItem; public MyThread(ImageItem imageItem, String imgPath, String outPath) { this.imageItem = imageItem; this.imgPath = imgPath; this.outPath = outPath; } public void run() { try { BitmapUtil.compressAndGenImage(imgPath, outPath, 500, false); compressImage = compressImage - 1; imageItem.storedPath = outPath; } catch (IOException e) { compressImage = compressImage - 1; e.printStackTrace(); } } }

使用的压缩方法:

/** * Compress by quality, and generate image to the path specified * * @param imgPath * @param outPath * @param maxSize target will be compressed to be smaller than this size.(kb) * @param needsDelete Whether delete original file after compress * @throws IOException */ public static void compressAndGenImage(String imgPath, String outPath, int maxSize, boolean needsDelete) throws IOException { compressAndGenImage(getBitmap(imgPath), outPath, maxSize); // Delete original file if (needsDelete) { File file = new File(imgPath); if (file.exists()) { file.delete(); } } }

组件 ImageEditContainer 添加图片方法介绍:

可添加本地和网络图片

/** * 添加本地图片 * List<String> storePaths 本地图片路径数组 */ public void addNewImages(List<String> storePaths) { } /** * 添加本地图片 */ public void addNewImageItem(ImageItem imageItem) { } /** * 添加网络图片 */ public void addRemoteImageItem(RemoteImageItem remoteImageItem) { }

组件 ImageEditContainer 其他方法介绍:

/** * 设置组件中 选择按钮的宽高 */ public void setImvHeightAndWidth(int height, int width) { }   /** * 设置图片最大数量 */ public void setTotalImageQuantity(int totalImageQuantity) { } /** * 设置图片展示图 */ public void setBtnImageResource(int resid) { }   /** * 获取组件中所有图片对象(本地+网络) */ public List<Object> getAllImageItems() { }   public void updateEditedImageItem(ImageItem imageItem) { }   /** * 更新网络图片 */ public void updateRemoteImageItem(RemoteImageItem remoteImageItem) { }   /** * 删除网络图片 */ public void removeRemoteImageItem(RemoteImageItem remoteImageItem) { }

4. 组件代码

1.ImageEditButton.java

/** * Created by dingzuoqiang on 2017/6/20. * Email: 530858106@qq.com */ public class ImageEditButton extends RelativeLayout { private final static String TAG = "ImageEditButton"; private ImageView imvAddImage; private ImageView imvEdit; private int imvHeight; private int imvWidth; public ImageEditButtonListener editButtonListener; public ImageEditButton(Context context) { this(context, null); } public ImageEditButton(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.image_edit_button_view, this, true); imvHeight = CommonUtil.dip2px(getContext(), 70); imvWidth = imvHeight; imvAddImage = (ImageView) findViewById(R.id.imv_add_image); imvEdit = (ImageView) findViewById(R.id.imv_edit); setImvHeightAndWidth(imvHeight, imvWidth); imvAddImage.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { doEditImage(); } }); imvEdit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { doEditImage2(); } }); } public void setImvHeightAndWidth(int height, int width) { this.imvHeight = height; this.imvWidth = width; ViewGroup.LayoutParams layoutParams = imvAddImage.getLayoutParams(); layoutParams.width = imvHeight; layoutParams.height = imvWidth; imvAddImage.setLayoutParams(layoutParams); } public int getImvHeight() { return imvHeight; } public int getImvWidth() { return imvWidth; } public void setPadding2(int left, int top, int right, int bottom) { this.setPadding(left, top, right, bottom); } public void setBtnImageResource(int resid) { imvAddImage.setImageResource(resid); // ImageLoaderUtils.loadImageFromDrawable(resid, imvAddImage, null); } public void reset() { imvEdit.setVisibility(GONE); } public void setEditButtonListener(ImageEditButtonListener editButtonListener) { this.editButtonListener = editButtonListener; } public BaseImageItem getImageItem() { Object object = this.getTag(); if (object instanceof BaseImageItem) return (BaseImageItem) object; return null; } public void displayUI() { // Object object = this.getTag(); if (object == null) return; if (object instanceof ImageItem) { ImageItem imageItem = (ImageItem) object; if (TextUtils.isEmpty(imageItem.storedPath)) return; File file = new File(imageItem.storedPath); if (file.exists()) { // 其实Glide加载本地图片和加载网络图片调用的方法是一样的,唯一的区别是说加载SD卡的图片需要SD卡的权限,加载网络需要网络权限 Glide.with(getContext()).load(file).crossFade().into(imvAddImage); } } else if (object instanceof RemoteImageItem) { // 如果是 remoteImageItem 则需要从读取图片,同时不可以裁剪 RemoteImageItem remoteImageItem = (RemoteImageItem) object; Glide.with(getContext()).load(remoteImageItem.thumbUrl).centerCrop().crossFade().into(imvAddImage); } // TODO BaseImageItem baseImageItem = (BaseImageItem) object; displayNoteIcons(baseImageItem); } private void displayNoteIcons(BaseImageItem baseImageItem) { imvEdit.setVisibility(VISIBLE); } private void doEditImage() { if (editButtonListener == null) return; Object object = this.getTag(); if (object == null) { // add image editButtonListener.doAddImage(); } else { // if (object instanceof ImageItem) { editButtonListener.doEditLocalImage((ImageItem) object); } else if (object instanceof RemoteImageItem) { editButtonListener.doEditRemoteImage((RemoteImageItem) object); } } } private void doEditImage2() { if (editButtonListener == null) return; Object object = this.getTag(); if (object != null) { // if (object instanceof ImageItem) { ImageItem imageItem = (ImageItem) object; imageItem.isDeleted = true; editButtonListener.doEditLocalImage(imageItem); } else if (object instanceof RemoteImageItem) { RemoteImageItem remoteImageItem = (RemoteImageItem) object; remoteImageItem.isDeleted = true; editButtonListener.doEditRemoteImage(remoteImageItem); } } } public interface ImageEditButtonListener { public void doAddImage(); public void doEditLocalImage(ImageItem imageItem1); public void doEditRemoteImage(RemoteImageItem remoteImageItem); } }

2.ImageEditContainer.java

/** * Created by dingzuoqiang on 2017/6/20. * Email: 530858106@qq.com */ public class ImageEditContainer extends HorizontalScrollView implements ImageEditButton.ImageEditButtonListener { private final static String TAG = "ImageEditContainer"; public ImageEditContainerListener mEditListener; private int idValue = 0; ImageEditButton imbAddImage; ViewGroup buttonsContainer; private int totalImageQuantity = 3;// 总添加数量 private int mBtnBgResid = 0; public ImageEditContainer(Context context) { //super(context); this(context, null); } public ImageEditContainer(Context context, AttributeSet attrs) { super(context, attrs); // LayoutInflater.from(context).inflate(R.layout.image_edit_container, this, true); imbAddImage = (ImageEditButton) findViewById(R.id.imb_add_image); imbAddImage.setEditButtonListener(this); // buttonsContainer = (ViewGroup) findViewById(R.id.lay_container); setHorizontalScrollBarEnabled(false); setHorizontalFadingEdgeEnabled(false); } public void setImvHeightAndWidth(int height, int width) { for (int i = 0; i < buttonsContainer.getChildCount(); i++) { ImageEditButton imageEditButton = (ImageEditButton) buttonsContainer.getChildAt(i); if (imageEditButton == null) continue; imageEditButton.setImvHeightAndWidth(height, width); } } public void setTotalImageQuantity(int totalImageQuantity) { if (totalImageQuantity > 0) this.totalImageQuantity = totalImageQuantity; } public void setBtnImageResource(int resid) { mBtnBgResid = resid; imbAddImage.setBtnImageResource(mBtnBgResid); } public List<Object> getAllImageItems() { List<Object> allItems = new ArrayList<>(); for (int i = 0; i < buttonsContainer.getChildCount(); i++) { ImageEditButton imageEditButton = (ImageEditButton) buttonsContainer.getChildAt(i); if (imageEditButton == null) continue; if (imageEditButton.getTag() == null) continue; allItems.add(imageEditButton.getTag()); } return allItems; } /** * 添加本地图片 */ public void addNewImages(List<String> storePaths) { for (int i = 0; i < storePaths.size(); i++) { String path = storePaths.get(i); ImageItem imageItem = new ImageItem(); imageItem.storedPath = path; imageItem.id = idValue++; Log.i(TAG, "index=" + i + " id=" + imageItem.id); imageItem.index = (buttonsContainer.getChildCount() - 1); addBaseImageItemToContainer(imageItem); } } /** * 添加本地图片 */ public void addNewImageItem(ImageItem imageItem) { if (imageItem == null) return; imageItem.id = idValue++; imageItem.index = (buttonsContainer.getChildCount() - 1); addBaseImageItemToContainer(imageItem); } public void updateEditedImageItem(ImageItem imageItem) { ImageEditButton imageEditButton = getImageEditButtonForImageItemById(imageItem); if (imageEditButton == null) { return; } Object originObj = imageEditButton.getTag(); if (!(originObj instanceof ImageItem)) { if (originObj instanceof RemoteImageItem) { RemoteImageItem remoteItem = (RemoteImageItem) originObj; if (remoteItem.index == imageItem.index) { imageEditButton.setTag(imageItem); imageEditButton.displayUI(); return; } reorderForImageItem(imageItem); } return; } ImageItem originImageItem = (ImageItem) originObj; if (imageItem.isDeleted) { removeButtonContainImageItem(imageItem); resetImageItemIndex(); return; } else { if (originImageItem.index == imageItem.index) { imageEditButton.setTag(imageItem); imageEditButton.displayUI(); return; } reorderForImageItem(imageItem); } } /** * 添加网络图片 */ public void addRemoteImageItem(RemoteImageItem remoteImageItem) { addBaseImageItemToContainer(remoteImageItem); } /** * 更新网络图片 */ public void updateRemoteImageItem(RemoteImageItem remoteImageItem) { ImageEditButton imageEditButton = getImageEditButtonForImageItemById(remoteImageItem); if (imageEditButton == null) { if (getAllImageItems().size() > 0) { List<Object> objectList = getAllImageItems(); for (int i = 0; i < objectList.size(); i++) { BaseImageItem baseImageItem = (BaseImageItem) objectList.get(i); removeButtonContainImageItem(baseImageItem); } // objectList.add(0, remoteImageItem); for (int i = 0; i < objectList.size(); i++) { addRemoteImageItem((RemoteImageItem) objectList.get(i)); } // } else { addRemoteImageItem(remoteImageItem); } return; } BaseImageItem baseImageItem = (BaseImageItem) imageEditButton.getTag(); if (baseImageItem instanceof ImageItem) return; RemoteImageItem originRemoteItem = (RemoteImageItem) baseImageItem; if (remoteImageItem.index == originRemoteItem.index) { // index 相同 只是update imageEditButton.setTag(remoteImageItem); imageEditButton.displayUI(); return; } reorderForImageItem(remoteImageItem); } /** * 删除网络图片 */ public void removeRemoteImageItem(RemoteImageItem remoteImageItem) { ImageEditButton imageEditButton = getImageEditButtonForImageItemById(remoteImageItem); if (null != imageEditButton && null != imageEditButton.getTag()) { BaseImageItem baseImageItem = (BaseImageItem) imageEditButton.getTag(); if (baseImageItem instanceof ImageItem) return; RemoteImageItem originRemoteItem = (RemoteImageItem) baseImageItem; removeButtonContainImageItem(remoteImageItem); resetImageItemIndex(); } } private void reorderForImageItem(BaseImageItem imageItem) { removeButtonContainImageItem(imageItem); List<BaseImageItem> imageItems = new ArrayList<>(); imageItems.add(imageItem); int count = buttonsContainer.getChildCount(); for (int i = imageItem.index; i < count; i++) { ImageEditButton button = (ImageEditButton) buttonsContainer.getChildAt(i); if (button == null) continue; BaseImageItem imageItem1 = (BaseImageItem) button.getTag(); if (imageItem1 == null) continue; imageItems.add(imageItem1); } for (int i = 0; i < imageItems.size(); i++) { BaseImageItem item = imageItems.get(i); removeButtonContainImageItem(item); } // for (int i = 0; i < imageItems.size(); i++) { addBaseImageItemToContainer(imageItems.get(i)); } } private void resetImageItemIndex() { for (int i = 0; i < buttonsContainer.getChildCount(); i++) { try { ImageEditButton button = (ImageEditButton) buttonsContainer.getChildAt(i); if (button == null) continue; BaseImageItem imageItem = (BaseImageItem) button.getTag(); if (imageItem == null) continue; imageItem.index = i; } catch (Exception ignored) { } } } private ImageEditButton getImageEditButtonForImageItemById(BaseImageItem imageItem) { for (int i = 0; i < buttonsContainer.getChildCount(); i++) { ImageEditButton imageEditButton = (ImageEditButton) buttonsContainer.getChildAt(i); if (imageEditButton == null) continue; if (imageEditButton.getImageItem() == null) continue; BaseImageItem searchedImageItem = imageEditButton.getImageItem(); if (imageItem.id.longValue() == searchedImageItem.id.longValue()) { return imageEditButton; } } return null; } /* 删除一个 ImageItem */ private void removeButtonContainImageItem(BaseImageItem imageItem) { ImageEditButton imageEditButton = getImageEditButtonForImageItemById(imageItem); if (imageEditButton == null) return; buttonsContainer.removeView(imageEditButton); resetImageItemIndex(); imbAddImage.setVisibility(buttonsContainer.getChildCount() <= totalImageQuantity ? VISIBLE : GONE); } private void addBaseImageItemToContainer(BaseImageItem imageItem) { buttonsContainer.removeView(imbAddImage); ImageEditButton imageEditButton = new ImageEditButton(getContext()); if (mBtnBgResid != 0) imageEditButton.setBtnImageResource(mBtnBgResid); imageEditButton.setTag(imageItem); imageEditButton.setEditButtonListener(this); // buttonsContainer.addView(imageEditButton, buttonsContainer.getChildCount(), new RelativeLayout.LayoutParams(nSize, imbAddImage.getHeight())); buttonsContainer.addView(imageEditButton, buttonsContainer.getChildCount()); LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imageEditButton.getLayoutParams(); layoutParams.rightMargin = CommonUtil.dip2px(getContext(), 5); imageEditButton.setLayoutParams(layoutParams); imageEditButton.setImvHeightAndWidth(imbAddImage.getImvHeight(), imbAddImage.getImvWidth()); imageEditButton.displayUI(); // buttonsContainer.addView(imbAddImage, buttonsContainer.getChildCount()); // imbAddImage.setVisibility(buttonsContainer.getChildCount() <= totalImageQuantity ? VISIBLE : GONE); resetImageItemIndex(); } /* ImageEditButton listener */ public void doAddImage() { if (mEditListener != null) { mEditListener.doAddImage(); } } public void doEditLocalImage(ImageItem imageItem) { if (mEditListener != null) { mEditListener.doEditLocalImage(imageItem); } } public void doEditRemoteImage(RemoteImageItem remoteImageItem) { if (mEditListener != null) { mEditListener.doEditRemoteImage(remoteImageItem); } } // ----- public void setEditListener(ImageEditContainerListener editListener) { this.mEditListener = editListener; } // public interface ImageEditContainerListener { public void doAddImage(); public void doEditLocalImage(ImageItem imageItem1); public void doEditRemoteImage(RemoteImageItem remoteImageItem); }

项目下载:ImageEditContainer

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

时间: 2024-10-07 19:33:05

Android图片选择器ImageEditContainer的相关文章

Android图片选择器 丰富的配置选项_Android

最近也是刚好项目用到,于是就动手写了一个Android 图片选择器的库.支持图库多选/单选/图片裁剪/拍照/自定义图片加载库,极大程度的简化使用. 截图 优点 1.通过实现ImageLoader接口,可以实现自定义图片加载器的功能.例如可以用Glide.Picasso.ImageLoader,暂不支持Fresco,因为SimpleDraweeView本身并不属于ImageView.当然,也可用相同的思路来实现. 2.可配置的ImgSelConfig.方便进行扩展. 3.简化使用 项目地址:htt

Android图片选择器

1.概述            应公司项目需求,要做一个图片选择器,网上搜索了一些源码,我在别人的基础上进行了修改,另外页面也进行了重整,我的是先加载图片文件夹列表,然后再进入选择图片.            参考博客地址:http://blog.csdn.net/lmj623565791/article/details/39943731            修改bug:可以扫描gif文件,并且后缀名是大写的也可以扫描出来 例如:image.GIF 效果图如下: 2.扫描图片文件夹的核心代码,

Android 高仿QQ图片选择器_Android

当做一款APP,需要选择本地图片时,首先考虑的无疑是系统相册,但是Android手机五花八门,再者手机像素的提升,大图无法返回等异常因数,导致适配机型比较困难,微信.QQ都相继的在自己的APP里集成了图片选择功能,放弃了系统提供的图片选择器,这里仿造QQ做了一个本地图片选择器,PS:之前有人说"仿"写成"防"了,今儿特意注意了下,求不错. 先上一张效果图,无图无真相啊~~~ 实现的效果大概是这样的: 1.单选:跳转到本地图片选择文件夹,选择文件夹后,进入到该文件夹下

Android仿微信图片选择器_Android

很多项目要用到图片选择控件,每次都要写一大堆逻辑.于是基于图片选择组件(PhotoPicker)封装了一个控件PhotoUploadView.方便简易,一键集成,几句代码就可以添加类似微信的图片选择控件了.下面介绍一下该控件有些什么特点以及怎么使用.先看图: 效果如上图,点击加号弹出选择框,目前提供了两种形式,一个如图所见的PopupWindow,另一个是MaterialDialog,选择拍照或者从图库获取,从图库获取后就进入图二,选择完之后就图三或图四这里因为很多项目需要不一样,所以特别封装了

Android 高仿QQ图片选择器

当做一款APP,需要选择本地图片时,首先考虑的无疑是系统相册,但是Android手机五花八门,再者手机像素的提升,大图无法返回等异常因数,导致适配机型比较困难,微信.QQ都相继的在自己的APP里集成了图片选择功能,放弃了系统提供的图片选择器,这里仿造QQ做了一个本地图片选择器,PS:之前有人说"仿"写成"防"了,今儿特意注意了下,求不错. 先上一张效果图,无图无真相啊~~~ 实现的效果大概是这样的: 1.单选:跳转到本地图片选择文件夹,选择文件夹后,进入到该文件夹下

Android图片滚动,加入自动播放功能,使用自定义属性实现,霸气十足!

大家好,记得上次我带着大家一起实现了一个类似与淘宝客户端中带有的图片滚动播放器的效果,但是在 做完了之后,发现忘了加入图片自动播放的功能(或许是我有意忘记加的.....),结果图片只能通过手指滑动 来播放.于是今天我将再次带领大家,添加上之前遗漏的功能,让我们的图片播放器更加完善. 这次 的程序开发将完全基于上一次的代码,如果有朋友还未看过上篇文章,请先阅读 Android实现图片滚动控件 ,含页签功能,让你的应用像淘宝一样炫起来 . 既然是要加入自动播放的功能,那么就有一个非常 重要的问题需要

翻翻git之---史上最强的图片选择器 GalleryFinal(顺带附下麦麦,当当的近照)

转载请注明出处:王亟亟的大牛之路 技术内容在P2,P1是废话可以跳过 P1:(开胃菜) 最近手头上工作的事差不多告一段落了,可以把更多的精力花在学习上了,还是会继续翻git这部分的文章(搬运工),然后准备整整一些之前没接触过的知识点(具体学什么没想好,等会看看去) 先上一下家里宝贝的近照 P2:(正菜) 今天上的是一个自称是"史上最强的图片选择器" GalleryFinal 话不多说先看看效果 作者几乎实现了我们平时所需的"皂片"选择器的所有功能,还带有一定的编辑和

ImageView 实现Android colorPikcer 选择器的示例代码

本文介绍了ImageView 实现Android colorPikcer 选择器的示例代码,分享给大家,具体如下: Android colorPikcer 选择器 环形的ColorPicker,主要思路是: Color 选在放在ImageView 的background上面,根据点击的位置判断选择的颜色. 重写onTouch,在onTouch 里面判断点击点的颜色. 根据当前选择的颜色设置图片的src. 获取Bitmap 在 ColorPickerView 构造函数中初始化 Bitmap.因为g

android 图片显示问题

问题描述 android 图片显示问题 我锁定了activity的横屏显示,但是我在这个activity中有拍照的功能,当我横屏拍照的时候,这张图片会自动竖屏显示,我希望它怎么拍的就怎么显示, 哪位大神可以帮我解答一下呀 解决方案 android图片显示的方向问题Android创建图片图库不显示问题关于android gridview不显示图片,文字等问题 解决方案二: 难道你横屏拍下的照片竖屏显示时视角跟横屏拍照时的视角不一样? 拍完把图片旋转下呢