-
Notifications
You must be signed in to change notification settings - Fork 155
开始使用
qingmei2 edited this page May 19, 2018
·
30 revisions
下文将根据不同情境,提供在3~5分钟之内通过 RxImagePicker 快速实现 图片选择需求的代码,如果想获取更详细的代码,请参考sample中的代码:
在这之前,您需参考 开发准备 模块,确认配置好权限并添加对应的依赖。
public interface SystemImagePicker {
@Gallery //打开相册
@AsFile
Observable<File> openGallery();
@Camera //打开相机
@AsBitmap
Observable<File> openCamera();
}
SystemImagePicker imagePicker = new RxImagePicker.Builder()
.with(this)
.build()
.create(SystemImagePicker.class);
比如在Button的点击监听回调方法中添加如下代码:
imagePicker.openGallery() //打开系统相册选取图片
.subscribe(new Consumer<File>() {
@Override
public void accept(File file) throws Exception {
// 做您想做的,比如将选取的图片展示在ImageView中
}
});
public interface WechatImagePicker {
String KEY_WECHAT_PICKER_ACTIVITY = "key_wechat_picker";
@AsBitmap
@Gallery(viewKey = KEY_WECHAT_PICKER_ACTIVITY)
Observable<Bitmap> openGallery();
@AsFile
@Camera
Single<File> openCamera();
}
WechatImagePicker imagePicker = new RxImagePicker.Builder()
.with(this)
.addCustomGallery(
WechatImagePicker.KEY_WECHAT_PICKER_ACTIVITY,
WechatImagePickerActivity.class,
new WechatConfigrationBuilder(MimeType.ofAll(), false)
.maxSelectable(9) //最大可选图片数
.spanCount(4) //每行展示四张图片
.countable(false) //关闭计数模式
.theme(R.style.Wechat) //微信主题
.build()
)
.build()
.create(WechatImagePicker.class);
比如在Button的点击监听回调方法中添加如下代码:
imagePicker.openGallery() //打开微信相册选取图片
.subscribe(new Consumer<Bitmap>() {
@Override
public void accept(Bitmap bitmap) throws Exception {
// 做您想做的,比如将选取的图片展示在ImageView中
}
});
public interface ZhihuImagePicker {
String KEY_ZHIHU_PICKER_NORMAL = "key_zhihu_picker_theme_normal";
String KEY_ZHIHU_PICKER_DRACULA = "key_zhihu_picker_theme_dracula";
@AsBitmap
@Gallery(viewKey = KEY_ZHIHU_PICKER_NORMAL) //日间主题
Observable<Bitmap> openGalleryAsNormal();
@AsBitmap
@Gallery(viewKey = KEY_ZHIHU_PICKER_DRACULA) //夜间主题
Observable<Bitmap> openGalleryAsDracula();
@AsFile
@Camera //打开相机
Single<File> openCamera();
}
ZhihuImagePicker imagePicker = new RxImagePicker.Builder()
.with(this)
.addCustomGallery( //注入日间模式主题的UI
ZhihuImagePicker.KEY_ZHIHU_PICKER_NORMAL,
ZhihuImagePickerActivity.class,
new ZhihuConfigurationBuilder(MimeType.ofAll(), false)
.maxSelectable(9) //最大可选择数量
.countable(true) //可计数
.spanCount(4) //每行图片数量
.theme(R.style.Zhihu_Normal) //日间主题
.build()
)
.addCustomGallery( //注入夜间模式主题的UI
ZhihuImagePicker.KEY_ZHIHU_PICKER_DRACULA,
ZhihuImagePickerActivity.class,
new ZhihuConfigurationBuilder(MimeType.ofAll(), false)
.spanCount(3) //每行图片数量
.maxSelectable(1) //最大可选择数量
.theme(R.style.Zhihu_Dracula) //夜间主题
.build()
)
.build()
.create(ZhihuImagePicker.class);
比如在Button的点击监听回调方法中添加如下代码:
//打开日间主题相册选取图片,或者调用imagePicker.openGalleryAsDracula()打开夜间主题
imagePicker.openGalleryAsNormal()
.subscribe(new Consumer<Bitmap>() {
@Override
public void accept(Bitmap bitmap) throws Exception {
// 做您想做的,比如将选取的图片展示在ImageView中
}
});
- 主页
- 2.x更新日志(RxJava2)
- 3.x更新日志(RxJava3 + AndroidX)
- 常见问题
- 开发准备
-
开始使用
- 1 快速实现
- 2 进阶使用
- 设计思想
- 2.1 行为注解
- 2.2 ICustomPickerView接口
- 2.3 【必要参数】Context
- 2.4 【可选参数】ICustomPickerConfiguration
- 2.5 实现完全自定义UI
- 2.6 选择结果Result和扩展数据
- 3 已废弃API