一、业务场景
1、聊天界面发送图片==>多选点选+有右上角”发送”+图片预览==>图片预览支持右上角”发送”逻辑
2、发表动态==>多选点选+右上角”完成” + 图片预览==>图片预览支持右上角”完成”逻辑
3、个人资料更改头像==>单选点选 + 图片预览截图==>图片预览截图支持右上角”使用”逻辑
二、Intent参数设计; (Intent传参原则:尽量不要使用自定义类型专递参数)
1、最基本的参数传递(可选图片最大数目、右上角的、单选模式还是多选模式、返回结果的参数)
public static final String EXTRA_SELECT_COUNT = "max_select_count";
public static final String EXTRA_SUBMIT_TITLE = "extra_submit_title";
public static final String EXTRA_SELECT_MODE = "select_count_mode";
public static final String EXTRA_RESULT = "select_result";
2、扩展部分参数传递(图片选择界面的第一个Item是否显示拍照功能、预览的activity是否支持”勾选取消结束”勾选按钮)
public static final String EXTRA_SHOW_CAMERA = "show_camera";
public static final String EXTRA_JUMPTOCHECKPICS = "is_jumpto_checkpics";
3、在”发表动态选择”已经选了5张图片,再去选择图片–图片浏览器需要将已选的图片勾选状态设置为勾选
public static final String EXTRA_DEFAULT_SELECTED_LIST = "default_list";
4、截图功能(单选模式下,如果启动截图模式,那么选择后跳转截图界面)
public static final String RESIZEBIT = "RESIZEBIT";
public static final String RESULT = "resultPath";
public static final String MODE_SCALE_BOOLEAN = "MODE_SCALE_BOOLEAN";
public static final String SCALEW = "scaleW";
public static final String SCALEH = "SCALEH";
public static final String LIMITW = "limitWidth";
public static final String LIMITH = "limitHight";
public static final String MODE_SCALE_PATH = "MODE_SCALE_PATH";
三、理清楚需求之后进行设计
此处省略;分享的代码实现的功能,尚未设计优化代码可读性较差
代码项目github地址IMAlbum
四、代码使用方法
public static void jumpToSelectPics(Activity mContext, boolean isShowCamera, int selectMaxCount, int selectMode,
String submitBtnStr, boolean isJumpToCheckPics, ArrayList<String> hasSelectList) {
Intent intent = new Intent(mContext, MultiImageSelectorActivity.class);
// whether show camera
intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, isShowCamera);
// String of submit button
intent.putExtra(MultiImageSelectorActivity.EXTRA_SUBMIT_TITLE, submitBtnStr);
// String of submit button
intent.putExtra(MultiImageSelectorFragment.EXTRA_JUMPTOCHECKPICS, isJumpToCheckPics);
// max select image amount
intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, selectMaxCount);
// select mode (MultiImageSelectorActivity.MODE_SINGLE OR MultiImageSelectorActivity.MODE_MULTI)
intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, selectMode);
// EXTRA_DEFAULT_SELECTED_LIST
if (hasSelectList != null) {
intent.putStringArrayListExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, hasSelectList);
}
//
mContext.startActivityForResult(intent, REQUEST_IMAGE);
}
public static void jumpToSelecAndCropImg(Activity mContext, String picResultPath, int scaleW, int scaleH,
int limtWidth, int limitHight, boolean isScaleImg) {
Intent intent = new Intent(mContext, MultiImageSelectorActivity.class);
// whether show camera
intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, false);
// max select image amount
intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 1);
// select mode (MultiImageSelectorActivity.MODE_SINGLE OR MultiImageSelectorActivity.MODE_MULTI)
intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE);
// intent.putExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_CROP_CAMERA, isScaleImg);
intent.putExtra(CropImageAct.RESIZEBIT, isScaleImg);
intent.putExtra(CropImageAct.RESULT, picResultPath);
intent.putExtra(CropImageAct.SCALEW, scaleW);
intent.putExtra(CropImageAct.SCALEH, scaleH);
intent.putExtra(CropImageAct.LIMITW, limtWidth);
intent.putExtra(CropImageAct.LIMITH, limitHight);
mContext.startActivityForResult(intent, REQUEST_CROPIMAGE);
}
public static void jumpToSelectNoCropImg(Activity mContext) {
jumpToSelectPics(mContext, false, 2, MultiImageSelectorActivity.MODE_SINGLE);
}
五、baseselectactivity类
public abstract class BaseSelectPicAct extends Activity {
private final int CROPCAMARA_TAKEPHOTO = 1003;
private final int CROPCAMARA_SELECTFROMALBUM = 1004;
private final int ACTIVITY_RESULT_CROPCAMARA_WITH_DATA = 1;
private File mPicFile; // 选择图片路径
private String picPath;
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) { return; }
switch (requestCode) {
case IntentUtils.REQUEST_IMAGE: // 选择照片
if (null != data) {
ArrayList<String> resultList = data
.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
hasSelectPicsNoCrop(resultList);
}
break;
case ACTIVITY_RESULT_CROPCAMARA_WITH_DATA: // 拍照
if (mPicFile == null && !TextUtils.isEmpty(picPath)) mPicFile = new File(picPath);
if (null != mPicFile && mPicFile.exists()) {
hasTakePhotoPic(mPicFile);
}
break;
case IntentUtils.REQUEST_CROPIMAGE:
if (null == data) { return; }
if (TextUtils.isEmpty(mPicFile.toString()) || !mPicFile.exists()) {
Toast.makeText(this, "没有选择图片", Toast.LENGTH_LONG).show();
return;
}
String mPicNativePath = Environment.getExternalStorageDirectory().getAbsolutePath() +"/image" + mPicFile.getName();
try {
FilecacheTool.copyFile(mPicFile.getAbsolutePath(), mPicNativePath);
} catch (Exception e) {
e.printStackTrace();
}
hasPicsCroped(mPicNativePath);
break;
}
};
protected void selectPhoto() {
ImageTool.jumpToSelectNoCropImg(this);
}
protected void selectPhotoAndCrop() {
mPicFile = ImageTools.initTempFile();
ImageTool.jumpToSelecAndCropImg(this, mPicFile.toString(), 300, 300, 600, 600, true);
}
protected void takePhoto() {
if (!ImageTools.isSDCardExist()) {
Toast.makeText(this, "没有内存卡", Toast.LENGTH_LONG).show();
return;
}
mPicFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
+ System.currentTimeMillis() + ".jpg");
picPath = mPicFile.getAbsolutePath();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPicFile));
startActivityForResult(intent, ACTIVITY_RESULT_CROPCAMARA_WITH_DATA);
}
protected abstract void hasSelectPicsNoCrop(ArrayList<String> resultList);
protected abstract void hasTakePhotoPic(File mPicFile);
protected abstract void hasPicsCroped(String mPicNativePath);
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("picPathCache", picPath);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
picPath = savedInstanceState.getString("picPathCache");
super.onRestoreInstanceState(savedInstanceState);
}
}
六、图库选择simple
public class AlbumTestAct extends BaseSelectPicAct implements OnClickListener {
private ImageView iv_showresult;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.act_albumtest);
findViewById(R.id.tv_takephoto).setOnClickListener(this);
findViewById(R.id.tv_selectphoto).setOnClickListener(this);
findViewById(R.id.tv_selectandcrop).setOnClickListener(this);
iv_showresult = (ImageView) findViewById(R.id.iv_showresult);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.tv_takephoto:
takePhoto();
break;
case R.id.tv_selectphoto:
selectPhoto();
break;
case R.id.tv_selectandcrop:
selectPhotoAndCrop();
break;
default:
break;
}
}
@Override
protected void hasSelectPicsNoCrop(ArrayList<String> resultList) {
// TODO Auto-generated method stub
if (null != resultList) {
if(resultList.size() > 0)
{
ImageLoader.getInstance().displayImage(Scheme.FILE.wrap(resultList.get(0)), iv_showresult);
}
}
}
@Override
protected void hasTakePhotoPic(File mPicFile) {
// TODO Auto-generated method stub
if(null != mPicFile && mPicFile.exists())
{
ImageLoader.getInstance().displayImage(Scheme.FILE.wrap(mPicFile.getPath()), iv_showresult);
}
}
@Override
protected void hasPicsCroped(String mPicNativePath) {
// TODO Auto-generated method stub
if(null != mPicNativePath && new File(mPicNativePath).exists())
{
ImageLoader.getInstance().displayImage(Scheme.FILE.wrap(mPicNativePath), iv_showresult);
}
}
}
您可能感兴趣的文章:Android实现网页图片浏览功能Android实现网络图片浏览器Android使用gallery和imageSwitch制作可左右循环滑动的图片浏览器Android应用中图片浏览时实现自动切换功能的方法详解Android编程实现的超炫图片浏览器Android实现图片浏览器示例Android实现网络图片浏览功能