Android微信图片浏览框架设计
一、业务场景
1、聊天界面发送图片==>多选点选+有右上角”发送”+图片预览==>图片预览支持右上角”发送”逻辑
2、发表动态==>多选点选+右上角”完成” + 图片预览==>图片预览支持右上角”完成”逻辑
3、个人资料更改头像==>单选点选 + 图片预览截图==>图片预览截图支持右上角”使用”逻辑
二、Intent参数设计; (Intent传参原则:尽量不要使用自定义类型专递参数)
1、最基本的参数传递(可选图片最大数目、右上角的标题、单选模式还是多选模式、返回结果的参数)
/** 最大图片选择次数,int类型,默认9 */ 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"; /** 选择结果,返回为 ArrayList<String> 图片路径集合 */ public static final String EXTRA_RESULT = "select_result";
2、扩展部分参数传递(图片选择界面的第一个Item是否显示拍照功能、预览的activity是否支持”勾选取消结束”勾选按钮)
/** 是否显示相机,默认显示 */ public static final String EXTRA_SHOW_CAMERA = "show_camera"; /** 是否跳转到ImageDetailCheckPicsAct上*/ 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"; /** 单选 截图区域蓝色框的宽;单位dp */ public static final String SCALEW = "scaleW"; /** 单选 截图区域蓝色框的高;单位dp */ public static final String SCALEH = "SCALEH"; /** 单选 截图后图片保存的像素宽;单位sp */ public static final String LIMITW = "limitWidth"; /** 单选 截图后图片保存的像素高;单位sp */ 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); } /** * 图片格式jpeg * @param picResultPath 图片的要保存的地址 * @param scaleW 截图区域的宽度 单位 dp * @param scaleH 截图区域的高度 单位 dp * @param limtWidth 裁剪出来的图的宽度 * @param limitHight 裁剪出来的图的高度 * @param isScaleImg 是否按照 limtWidth、limitHight的比例进行放大缩小 * @description: * @author zhangjianlin (990996641) * @date 2015年6月11日 下午4:25:00 */ 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); } /** * @Description:选择单张图片不剪切 * 返回值 ArrayList switch (requestCode) { case IntentUtils.REQUEST_IMAGE: if (data != null) { ArrayList<String> resultList = data .getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT); } break; } * @param mContext */ public static void jumpToSelectNoCropImg(Activity mContext) { jumpToSelectPics(mContext, false, 2, MultiImageSelectorActivity.MODE_SINGLE); }
五、baseselectactivity类
/** @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); } <activity android:name="com.huika.huixin.control.me.activity.PersonalInfoAct" android:launchMode="singleTask" android:configChanges="orientation|keyboardHidden|screenSize" > </activity> * @Description: 选择图片的act * @author zhangjianlin * @date 2015-9-24 下午2:06:28 */ 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: // 选择照片 /** add by zjl */ if (null != data) { ArrayList<String> resultList = data .getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT); hasSelectPicsNoCrop(resultList); /* if (null != resultList) { * * } */ } break; case ACTIVITY_RESULT_CROPCAMARA_WITH_DATA: // 拍照 /* if (mPicFile.exists()) { * * } */ 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(); /* photoIntent = ImageTools.cropPhotoOfCompressFromGalleryIntent(Uri.fromFile(mPicFile)); startActivityForResult(photoIntent, Constant.ACTIVITY_RESULT_CROPIMAGE_WITH_DATA); */ 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); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)