欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android图片选择器ImageEditContainer

程序员文章站 2022-05-02 21:08:29
1. 简介 本次demo中一共封装了两个组件:imageeditbutton 和 imageeditcontainer。其中imageeditcontainer 是在 i...

1. 简介

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

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

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

2.项目主目录结构

Android图片选择器ImageEditContainer

3. 功能介绍

mainactivity.java 界面效果图:

Android图片选择器ImageEditContainerAndroid图片选择器ImageEditContainer

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);
  }
 }
 }

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

Android图片选择器ImageEditContainer

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

图片可拖拽,缩放

Android图片选择器ImageEditContainer

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

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

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