Giter Site home page Giter Site logo

pictureselector's Introduction

PictureSelector

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

效果图:

  • 选择界面

  • 预览界面

  • 裁剪界面

  • 选择文件目录界面

Features

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

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

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

Usage

1、引用Lib(Android studio)

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

  • 在AndroidManifest.xml文件中添加:
<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.PictureSelectorActivity"
    android:screenOrientation="portrait" />
    
<activity
	android:name="net.arvin.pictureselector.uis.TakePhotoAndCropActivity"
	android:screenOrientation="portrait" />

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

  • 在res文件夹下创建xml文件夹,再创建file_paths.xml文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <paths>
        <external-path
            name="camera_photos"
            path="" />
    </paths>
</resources>

该配置的含义这里就不解释了,想了解的可以去看看这里

2、修改默认配置

//这是默认设置
PSConfigUtil.getInstance().setCanCrop(false)
                .setCanTakePhoto(true)
                .setMaxCount(9);
  • setCanCrop 是否能裁剪,只有maxCount为1时才有效;
  • setCanTakePhoto 是否支持拍照;
  • setMaxCount 设置一次最多可选择图片的数量

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

4、启动拍照

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

6、清除图片缓存(删除)

  • 清除裁剪图片:
PSCropUtil.clear();
  • 清除拍照图片
PSTakePhotoUtil.clear();
  • 清除所有
PSConfigUtil.clearCache();

Thanks

  • 图片预览在PhotoView的基础上做了一定的修改;
  • 图片裁剪使用了鸿洋大神的仿微信头像裁剪,加载和裁剪算法并做了一定的修改。
  • 大图显示使用了SubsamplingScaleImageView,并增加裁剪(还有优化空间)
  • 图片加载使用Glide
  • 事件传递使用Eventbus
  • 异步操作使用了Rxjava和Rxandroid

在此表示感谢!

License

   Copyright 2016 arvinljw

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

pictureselector's People

Contributors

arvinljw avatar

Watchers

James Cloos avatar tianzc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.