文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android微信图片浏览框架设计

2022-06-06 07:50

关注

一、业务场景

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实现网络图片浏览功能


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯