Android开发经验总结篇

仿微信多图片选择器

2018-02-01  本文已影响9人  zhengLH

【图片裁剪三方库链接】 https://github.com/jeasonlzy/ImagePicker

【Gradle依赖包】 compile 'com.lzy.widget:imagepicker:0.5.5'

【核心代码】

@Override

public void onItemClick(View view, int position) {

switch (position) {

case IMAGE_ITEM_ADD:

SelectPhotosPopup selectPhotosPopup =new SelectPhotosPopup(this, view, R.layout.pop_select_photo_pic);

            selectPhotosPopup.setOnHeadSelectListener(new SelectPhotosPopup.OnHeadSelectListener() {

@Override

                public void onTack() {//照相

                    ImagePicker.getInstance().setSelectLimit(maxImgCount -selImageList.size());

                    Intent intent =new Intent(PhotoSelectorActivity.this, ImageGridActivity.class);

                    intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, true); // 是否是直接打开相机

                    startActivityForResult(intent, REQUEST_CODE_SELECT);

                }

@Override

                public void onPhotoList() {//选择图库

                    //打开选择, 本次允许选择的数量

                    ImagePicker.getInstance().setSelectLimit(maxImgCount -selImageList.size());

                    Intent intent1 =new Intent(PhotoSelectorActivity.this, ImageGridActivity.class);

                                                    /* 如果需要进入选择的时候显示已经选中的图片,                                                    * 详情请查看ImagePickerActivity

* */

                    //intent1.putExtra(ImageGridActivity.EXTRAS_IMAGES,images);

                    startActivityForResult(intent1, REQUEST_CODE_SELECT);

                }

});

break;

        default:

//打开预览

            Intent intentPreview =new Intent(this, ImagePreviewDelActivity.class);

            intentPreview.putExtra(ImagePicker.EXTRA_IMAGE_ITEMS, (ArrayList)adapter.getImages());

            intentPreview.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, position);

            intentPreview.putExtra(ImagePicker.EXTRA_FROM_ITEMS, true);

            startActivityForResult(intentPreview, REQUEST_CODE_PREVIEW);

break;

    }

}

ArrayListimages =null;

@Override

public void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {

//添加图片返回

        if (data !=null && requestCode ==REQUEST_CODE_SELECT) {

images = (ArrayList) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);

            if (images !=null) {

selImageList.addAll(images);

                adapter.setImages(selImageList);

            }

}

}else if (resultCode == ImagePicker.RESULT_CODE_BACK) {

//预览图片返回

        if (data !=null && requestCode ==REQUEST_CODE_PREVIEW) {

images = (ArrayList) data.getSerializableExtra(ImagePicker.EXTRA_IMAGE_ITEMS);

            if (images !=null) {

selImageList.clear();

                selImageList.addAll(images);

                adapter.setImages(selImageList);

            }

}

}

}

上一篇下一篇

猜你喜欢

热点阅读