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

Android微信图片浏览框架设计

程序员文章站 2024-03-07 21:19:51
一、业务场景 1、聊天界面发送图片==>多选点选+有右上角”发送”+图片预览==>图片预览支持右上角”发送”逻辑 2、发表动态==>多选点选+右上角...

一、业务场景

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

}


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