Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android微信圖片浏覽框架設計

Android微信圖片浏覽框架設計

編輯:關於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);
    }
  }

}


以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved