版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Android中如何使用ImageEditContainer图片选择器
Android中如何使用ImageEditContainer图片选择器,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。1.简介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
{
BitmapUpressAndGenImage(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@
*/
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@
*/
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);
ImageEd
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 证券公司围护桩施工合同
- 道路施工队合作协议
- 农村房屋拆迁补偿合同
- 剧院排水设施安装合同
- 培训零售环境防疫措施
- 医疗器械招投标规范解读
- 无抵押企业借款合同
- 通信设备质量管理办法
- 商业综合体二手房交易合同范文
- 制造执行系统操作与应用课件 3-4-2典型离散制造工艺
- 请款单(可直接打印-标准模板)
- 班主任德育工作:班主任培训讲课件
- RJ45插拔测试报告
- 2022年病历质控考核标准完整
- 教师争做新时代的“大先生”心得体会8篇
- 腰椎间盘突出健康教育课件
- 大学生劳动教育PPT完整全套教学课件
- 公共事务管理专业
- 高中思想政治-高三一轮复习课文化创新教学设计学情分析教材分析课后反思
- 高中美术-文化变革 美术发展教学设计学情分析教材分析课后反思
- 品管圈标准化作业书模板
评论
0/150
提交评论