實現效果
--------------------------------------------------------------------------------------------------------------------
請在Github下查閱用法(最新更新和用法都在github上第一時間更新):
--------------------------------------------------------------------------------------------------------------------
Android-ImagesPickers是一個集圖片選擇(單選/多選)、拍照、裁剪、圖片預覽、圖片顯示容器的圖片選擇顯示工具。使用方便,開發者僅需要幾行的代碼就可以集成Android整套圖片“選裁顯刪”功能,可以通過設置參數選擇自己想要使用的功能,Android-ImagesPickers自身並沒有強制綁定某個圖片加載器(如UIL,Glide,Fresco,Picasso),開發者可以根據自己項目需求給Android-ImagesPickers配置圖片加載器。
也許有人會問:系統不是自帶相冊選擇器嗎,為什么還有做一個圖片選擇器呢,有必要嗎?我告訴你很有必要。微信,QQ等等App它們都是自己帶圖片選擇器,並沒有直接調系統的圖片選擇器。為什么要這么做呢?我總結出以下幾點,使用本圖片選擇器下面的問題你都不用考慮,就是這么的任性:
· 最大的問題就是兼容性了,手機廠商那么多,相冊軟件那么多從而引起各種奇葩的問題
· 有些手機拍照圖片倒立情況(如三星和魅族)
· 拿到的bitmap或uri為空
· 非常頻繁出現OOM
· 不支持多選
· 拍照/選擇圖片/裁剪視乎用起來有些麻煩,加上處理一些旋轉、裁剪、壓縮就更加麻煩了,代碼多得不行不行的。
· 系統的圖片選擇UI上與自己APP樣式不統一
· 有些不支持圖片旋轉
· ....
· 最大的問題就是兼容性了,手機廠商那么多,相冊軟件那么多從而引起各種奇葩的問題
· 有些手機拍照圖片倒立情況(如三星和魅族)
· 拿到的bitmap或uri為空
· 非常頻繁出現OOM
· 不支持多選
· 拍照/選擇圖片/裁剪視乎用起來有些麻煩,加上處理一些旋轉、裁剪、壓縮就更加麻煩了,代碼多得不行不行的。
· 系統的圖片選擇UI上與自己APP樣式不統一
· 有些不支持圖片旋轉
· ....
- //目前只上傳到了jcenter,在項目gradle下使用jcenter
- //Currently only uploaded to the jcenter, under the project gradle use jcenter
- allprojects {
- repositories {
- jcenter()
- }
- }
- //在module模塊的gradle中添加依賴
- //Add dependencies in the module's gradle
- dependencies {
- compile 'com.jaikydota.imagespickers:imagespickers:1.0.6'
- //如果使用圖片加載框架,添加依賴,下面用Glide示例
- compile 'com.github.bumptech.glide:glide:3.6.1'
- }
二:在 AndroidManifest.xml 中 添加 如下權限
- <!-- 從sdcard中讀取數據的權限 -->
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- <!-- 往sdcard中寫入數據的權限 -->
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
三:創建圖片加載器 (其中可以按照 喜好 使用不同的 第三方圖片加載框架 以下為Glide示例)
- public class GlideLoader implements ImageLoader {
- @Override
- public void displayImage(Context context, String path, ImageView imageView) {
- Glide.with(context)
- .load(path)
- .placeholder(com.jaiky.imagespickers.R.drawable.global_img_default)
- .centerCrop()
- .into(imageView);
- }
- }
四:配置 ImageConfig
UI 視圖配置
- ImageConfig imageConfig
- = new ImageConfig.Builder(new GlideLoader())
- // 修改狀態欄顏色
- .steepToolBarColor(getResources().getColor(R.color.blue))
- // 標題的背景顏色
- .titleBgColor(getResources().getColor(R.color.blue))
- // 提交按鈕字體的顏色
- .titleSubmitTextColor(getResources().getColor(R.color.white))
- // 標題顏色
- .titleTextColor(getResources().getColor(R.color.white))
- .build();
- ImageConfig imageConfig
- = new ImageConfig.Builder(new GlideLoader())
- .steepToolBarColor(getResources().getColor(R.color.blue))
- .titleBgColor(getResources().getColor(R.color.blue))
- .titleSubmitTextColor(getResources().getColor(R.color.white))
- .titleTextColor(getResources().getColor(R.color.white))
- // 開啟多選 (默認為多選)
- .mutiSelect()
- // 多選時的最大數量 (默認 9 張)
- .mutiSelectMaxSize(9)
- // 開啟拍照功能 (默認關閉)
- .showCamera()
- // 已選擇的圖片路徑
- .pathList(path)
- // 拍照后存放的圖片路徑(默認 /temp/picture) (會自動創建)
- .filePath("/temp/picture")
- .build();
- mageSelector.open(MainActivity.this, imageConfig); // 開啟圖片選擇器
- ImageConfig imageConfig
- = new ImageConfig.Builder(new GlideLoader())
- .steepToolBarColor(getResources().getColor(R.color.blue))
- .titleBgColor(getResources().getColor(R.color.blue))
- .titleSubmitTextColor(getResources().getColor(R.color.white))
- .titleTextColor(getResources().getColor(R.color.white))
- // 開啟單選 (默認為多選)
- .singleSelect()
- // 開啟拍照功能 (默認關閉)
- .showCamera()
- // 拍照后存放的圖片路徑(默認 /temp/picture) (會自動創建)
- .filePath("/temp/picture")
- .build();
- mageSelector.open(MainActivity.this, imageConfig); // 開啟圖片選擇器
單選1:1便捷裁剪
- //配置ImageConfig添加方法
- // (裁剪默認配置:關閉 比例 1:1 輸出分辨率 500*500)
- .crop()
- //配置ImageConfig添加方法
- // (裁剪默認配置:關閉 比例 1:2 輸出分辨率 500*1000)
- .crop(1, 2, 500, 1000)
- //配置ImageConfig添加方法
- // (設置容器,默認會添加一個子視圖到容器布局,繼承自ViewGroup如Linearlayout
- // 注意容器布局中不要有其他子視圖,可自己對容器布局設置寬度、Margin)
- // 默認每行顯示4個,不帶刪除
- .setContainer(ViewGroup container)
- //配置ImageConfig添加方法
- //參數:1、顯示容器,2、每行顯示數量(建議不要超過8個),是否可刪除(默認不帶刪除)
- .setContainer(linearLayout, 6, true)
- //配置ImageConfig添加方法
- // (關閉圖片預覽功能,默認開啟)
- .closePreview()
五:在 onActivityResult 中獲取選中的照片路徑 數組 :
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == ImageSelector.IMAGE_REQUEST_CODE && resultCode == RESULT_OK && data != null) {
- // 獲取選中的圖片路徑列表 Get Images Path List
- List<String> pathList = data.getStringArrayListExtra(ImageSelectorActivity.EXTRA_RESULT);
- for (String path : pathList) {
- Log.i("ImagePath", path);
- }
- }
- }
- public class MainActivity extends AppCompatActivity {
- private Button btn1, btn2;
- private TextView tv1;
- private ArrayList<String> path = new ArrayList<>();
- public static final int REQUEST_CODE = 123;
- private ImageConfig imageConfig;
- private LinearLayout llContainer;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btn1 = (Button) findViewById(R.id.btn1);
- btn2 = (Button) findViewById(R.id.btn2);
- tv1 = (TextView) findViewById(R.id.tv1);
- llContainer = (LinearLayout) findViewById(R.id.llContainer);
- btn1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- imageConfig = new ImageConfig.Builder(
- new GlideLoader())
- .steepToolBarColor(getResources().getColor(R.color.titleBlue))
- .titleBgColor(getResources().getColor(R.color.titleBlue))
- .titleSubmitTextColor(getResources().getColor(R.color.white))
- .titleTextColor(getResources().getColor(R.color.white))
- // 開啟單選 (默認為多選)
- .singleSelect()
- // 裁剪 (只有單選可裁剪)
- //.crop()
- // 開啟拍照功能 (默認關閉)
- .showCamera()
- // 設置顯示容器
- .setContainer(llContainer)
- .requestCode(REQUEST_CODE)
- .build();
- ImageSelector.open(MainActivity.this, imageConfig);
- }
- });
- btn2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- imageConfig = new ImageConfig.Builder(
- new GlideLoader())
- .steepToolBarColor(getResources().getColor(R.color.titleBlue))
- .titleBgColor(getResources().getColor(R.color.titleBlue))
- .titleSubmitTextColor(getResources().getColor(R.color.white))
- .titleTextColor(getResources().getColor(R.color.white))
- // 開啟多選 (默認為多選)
- .mutiSelect()
- // 多選時的最大數量 (默認 9 張)
- .mutiSelectMaxSize(9)
- // 設置圖片顯示容器,參數:(1、顯示容器,2、每行顯示數量(建議不要超過8個),是否可刪除)
- .setContainer(llContainer, 4, true)
- // 已選擇的圖片路徑
- .pathList(path)
- // 拍照后存放的圖片路徑(默認 /temp/picture)
- .filePath("/temp")
- // 開啟拍照功能 (默認關閉)
- .showCamera()
- .requestCode(REQUEST_CODE)
- .build();
- ImageSelector.open(MainActivity.this, imageConfig);
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null) {
- List<String> pathList = data.getStringArrayListExtra(ImageSelectorActivity.EXTRA_RESULT);
- tv1.setText("");
- for (String path : pathList) {
- tv1.append(path);
- tv1.append("\n");
- }
- path.clear();
- path.addAll(pathList);
- }
- }
- }
--------------------------------------------------------------------------------------------------------------------
查看及獲取源代碼:
--------------------------------------------------------------------------------------------------------------------
