Skip to content

Commit

Permalink
修改README
Browse files Browse the repository at this point in the history
修改README以及DEMO
  • Loading branch information
liujinwei committed Jan 7, 2018
1 parent a12d1e0 commit 4b53aa3
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 191 deletions.
284 changes: 150 additions & 134 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,174 +1,190 @@
# PictureSelector
## PictureSelector

这是一个仿微信的图片选择器
这是仿微信的图片选择器

效果图:
### 简介

* 选择界面
1、来源:相机,本地图片媒体库中jpg和png类型的图片

![](screenshot/main.png)
2、功能:**多选、单选、拍照、预览、裁剪、大图、支持7.0**

* 预览界面
3、TODO:增加**直接拍照裁剪类型,多选时图片编辑功能,视频选择功能**

![](screenshot/review.png)

* 裁剪界面

![](screenshot/crop.png)

* 选择文件目录界面

![](screenshot/folder.png)

## Features

1、图片来源:拍照,本地图片媒体库中jpg和png类型的图片

2、功能:多选、单选、拍照、预览、裁剪

3、todo:优化细节(一些动画)、整理解析文档

## Demo下载地址
### Demo下载

![](screenshot/ps_icon.png)

[PicSelector下载地址](app/PicSelector.apk)

## Usage
![](screenshot/use_sample.png)

1、引用Lib(Android studio)
### 使用方式

通过引用Module方式引用,这样会更方便定制
#### 引用

* 在AndroidManifest.xml文件中添加:
**1、在根目录的build.gradle中加入如下配置**

```
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
...
<!--7.0以上拍照适配-->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="项目包名.takephoto.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<!--图片选择首页-->
<activity
android:name="net.arvin.pictureselector.uis.SelectorActivity"
android:screenOrientation="portrait" />
<!--只拍照和裁剪-->
<activity
android:name="net.arvin.pictureselector.uis.TakePhotoAndCropActivity"
android:screenOrientation="portrait" />
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
maven { url "https://dl.bintray.com/thelasterstar/maven/" }
}
}
```

注意:provider的android:authorities属性把项目包名换成该项目的包名,因为在手机中,这个属性是不能重复的。

2、修改默认配置
**2、在要是用的module中增加如下引用**

```
//这是默认设置
PSConfigUtil.getInstance().setCanCrop(false)
.setCanTakePhoto(true)
.setMaxCount(9)
.setStatusBarColor(R.color.ps_colorPrimaryDark);
dependencies {
...
compile 'com.github.arvinljw:PictureSelector:v2.0.1'
}
```
* setCanCrop 是否能裁剪,只有maxCount为1时才有效;
* setCanTakePhoto 是否支持拍照;
* setMaxCount 设置一次最多可选择图片的数量;
* setStatusBarColor 设置状态栏颜色,默认为值"#2f3034"。

3、启动PictureSelector:
*注:该库引用的第三方代码*

```
private final int REQUEST_CODE_1 = 1001;
private ArrayList<ImageEntity> selectedImages;
...
PSConfigUtil.getInstance().showSelector(MainActivity.this, REQUEST_CODE_1, selectedImages);
//或者不传参数,注意传递已选择图片的大小不能超过设置的最大数量
PSConfigUtil.getInstance().showSelector(MainActivity.this, REQUEST_CODE_1);
```
* v7
* recyclerview
* annotations
* exifinterface
* glide

4、启动拍照
前四个都是com.android.support下边的,版本是**26.1.0**,glide使用版本**4.4.0**

```
private final int REQUEST_CODE_2 = 1002;
PSConfigUtil.getInstance().showTakePhotoAndCrop(MainActivity.this, REQUEST_CODE_2);
```

5、接收选择的图片数据:
若是引用的包重复可使用类似这样使用

```
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE_1: {
selectedImages.clear();
List<ImageEntity> temp = data.getParcelableArrayListExtra(PSConstanceUtil.PASS_SELECTED);
selectedImages.addAll(temp);
PSGlideUtil.loadImage(this, "file://" + temp.get(0).getPath(), img);
for (ImageEntity selectedImage : selectedImages) {
Log.d("back_data", selectedImage.getPath());
}
break;
}
case REQUEST_CODE_2: {
selectedImages.clear();
List<ImageEntity> temp = data.getParcelableArrayListExtra(PSConstanceUtil.PASS_SELECTED);
selectedImages.addAll(temp);
PSGlideUtil.loadImage(this, "file://" + temp.get(0).getPath(), img);
for (ImageEntity selectedImage : selectedImages) {
Log.d("back_data", selectedImage.getPath());
}
break;
}
}
}
dependencies {
...
compile ('com.github.arvinljw:PictureSelector:v2.0.1'){
exclude group: 'com.android.support'
}
}
```

6、清除图片缓存(删除)
**3、在AndroidManifest文件中添加权限以及必须配置**

* 清除裁剪图片:
**权限**

```
PSCropUtil.clear();
```
* 清除拍照图片

```
PSTakePhotoUtil.clear();
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
```

* 清除所有
**配置**

```
PSConfigUtil.clearCache();
```

## Thanks

* 图片预览在[PhotoView](https://github.com/chrisbanes/PhotoView)的基础上做了一定的修改;
* 图片裁剪使用了鸿洋大神的[仿微信头像裁剪](http://blog.csdn.net/lmj623565791/article/details/39761281),加载和裁剪算法并做了一定的修改。
* 大图显示使用了[SubsamplingScaleImageView](https://github.com/davemorrissey/subsampling-scale-image-view),并增加裁剪(还有优化空间)
* 图片加载使用Glide
* 事件传递使用Eventbus
* 异步操作使用了Rxjava和Rxandroid

在此表示感谢!

## License
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="net.arvin.pictureselectordemo.takephoto.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/ps_file_paths"/>
</provider>
<activity
android:name="net.arvin.selector.uis.SelectorActivity"
android:screenOrientation="portrait"
android:theme="@style/TransparentTheme"/>
```

#### 使用

本库依然采用外观模式提供了一个[SelectorHelper](selectorlibrary/src/main/java/net/arvin/selector/SelectorHelper.java),里边有一系列的静态方法去启动选择器,只是传递参数不同而已。

但是到最后都调用一个方法,所以只需要告诉大家这一个方法及其参数就能够比较好的使用了。

```
/**
* 去选择图片或视频
*
* @param type 可选值{@link #VALUE_TYPE_PICTURE}{@link #VALUE_TYPE_VIDEO}{@link #VALUE_TYPE_PICTURE_VIDEO}
* @param singleSelection 是否单选
* @param canCrop 是否裁剪
* @param maxCount 最大数量
* @param withCamera 是否带相机
* @param selectedPictures 已选中图片
* @param selectedVideos 已选中视频
*/
private static void select(Activity activity, int type, boolean singleSelection, boolean canCrop, int maxCount, boolean withCamera,
ArrayList<String> selectedPictures, ArrayList<String> selectedVideos, int requestCode) {
Intent intent = new Intent(activity, SelectorActivity.class);
Bundle bundle = new Bundle();
bundle.putInt(KEY_TYPE_SELECT, type);
bundle.putBoolean(KEY_SINGLE_SELECTION, singleSelection);
bundle.putBoolean(KEY_CAN_CROP, canCrop);
bundle.putInt(KEY_MAX_COUNT, maxCount);
bundle.putBoolean(KEY_WITH_CAMERA, withCamera);
if (selectedPictures != VALUE_SELECTED_PICTURES_NULL) {
bundle.putStringArrayList(KEY_SELECTED_PICTURES, selectedPictures);
}
if (selectedVideos != VALUE_SELECTED_VIDEOS_NULL) {
bundle.putStringArrayList(KEY_SELECTED_VIDEOS, selectedVideos);
}
bundle.putString(KEY_AUTHORITIES, PSUtil.getAuthorities(activity));
intent.putExtras(bundle);
activity.startActivityForResult(intent, requestCode);
}
```

其实方法贴出来,也有注释就不用多说大家都明白了。**这里暂时还只支持选择图片。**

```
/**
* 单选图片,不裁剪,带相机
*/
public static void selectPicture(Activity activity, int requestCode) {
selectPicture(activity, VALUE_CAN_CROP_FALSE, VALUE_WITH_CAMERA_TRUE, requestCode);
}
/**
* 单选图片
*
* @param canCrop 选择是否裁剪
* @param withCamera 选择是否带相机
*/
public static void selectPicture(Activity activity, boolean canCrop, boolean withCamera, int requestCode) {
select(activity, VALUE_TYPE_PICTURE, VALUE_SINGLE_SELECTION_TRUE, canCrop, VALUE_COUNT_SINGLE, withCamera,
VALUE_SELECTED_PICTURES_NULL, VALUE_SELECTED_VIDEOS_NULL, requestCode);
}
/**
* 多选图片,带相机
*
* @param maxCount 多选的最大数量
*/
public static void selectPictures(Activity activity, int maxCount, int requestCode) {
selectPictures(activity, maxCount, VALUE_WITH_CAMERA_TRUE, VALUE_SELECTED_PICTURES_NULL, requestCode);
}
/**
* 多选图片
*
* @param maxCount 多选的最大数量
* @param withCamera 选择是否带相机
* @param selectedPictures 已选中的图片
*/
public static void selectPictures(Activity activity, int maxCount, boolean withCamera, ArrayList<String> selectedPictures, int requestCode) {
select(activity, VALUE_TYPE_PICTURE, VALUE_SINGLE_SELECTION_FALSE, VALUE_CAN_CROP_FALSE, maxCount, withCamera,
selectedPictures, VALUE_SELECTED_VIDEOS_NULL, requestCode);
}
```

也就是说这四个静态方法是可以使用的,多选时暂时不支持裁剪,之后会想微信一样增加编辑功能。

最后在onActivityResult中可以获得选中的图片,当然顺序是选择图片时的顺序。

```
ArrayList<String> backPics = data.getStringArrayListExtra(ConstantData.KEY_BACK_PICTURES);
```
这样就获取到选中的图片了,不管单选多选都这样,只是单选就只有一张。

当然6.0以上的访问相机和本地文件的权限需要自己去实现,demo中也提供了一种方式,仅供参考。

若是有什么问题,希望不吝赐教共同进步~

### License

```
Copyright 2016 arvinljw
Expand Down
13 changes: 7 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
apply plugin: 'com.android.application'

android {
def android = rootProject.ext.android
compileSdkVersion android.compileSdkVersion as int
def version = rootProject.ext.version
compileSdkVersion version.compileSdkVersion as int
buildToolsVersion android.buildToolsVersion as String

defaultConfig {
minSdkVersion android.minSdkVersion as int
targetSdkVersion android.targetSdkVersion as int
versionCode android.versionCode as int
versionName android.versionName as String
applicationId "net.arvin.selectordemp"
minSdkVersion version.minSdkVersion as int
targetSdkVersion version.targetSdkVersion as int
versionCode version.versionCode as int
versionName version.versionName as String
}
buildTypes {
release {
Expand Down
20 changes: 10 additions & 10 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.arvin.pictureselectordemo">
package="net.arvin.selectordemo">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Expand All @@ -14,24 +14,24 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name="net.arvin.selectordemo.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="net.arvin.pictureselectordemo.takephoto.fileprovider"
android:authorities="${applicationId}.takephoto.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/ps_file_paths"/>
</provider>

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<activity
android:name="net.arvin.selector.uis.SelectorActivity"
android:screenOrientation="portrait"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.arvin.pictureselectordemo;
package net.arvin.selectordemo;

import android.content.Intent;
import android.support.annotation.NonNull;
Expand Down
Loading

0 comments on commit 4b53aa3

Please sign in to comment.