在日常的Android
開發(fā)中托启,在做一些發(fā)布功能的時(shí)候肋殴,我們可能需要調(diào)用相冊(cè)去選擇圖片或者視頻洽沟。這里分享一個(gè)圖片選擇庫(kù)以故,幫大家快速實(shí)現(xiàn)。
TakePhoto
TakePhoto
是一款用于在Android設(shè)備上獲取照片(拍照或從相冊(cè)裆操、文件中選擇)怒详、裁剪圖片、壓縮圖片的開源工具庫(kù)跷车,目前最新版本4.1.0棘利。開源庫(kù)的源碼以及wiki
開源庫(kù)的特性
- 支持通過(guò)相機(jī)拍照獲取圖片
- 支持從相冊(cè)選擇圖片
- 支持從文件選擇圖片
- 支持批量圖片選取
- 支持圖片壓縮以及批量圖片壓縮
- 支持圖片裁切以及批量圖片裁切
- 支持照片旋轉(zhuǎn)角度自動(dòng)糾正
- 支持自動(dòng)權(quán)限管理(無(wú)需關(guān)心SD卡及攝像頭權(quán)限等問(wèn)題)
- 支持對(duì)裁剪及壓縮參數(shù)個(gè)性化配置
- 提供自帶裁剪工具(可選)
- 支持智能選取及裁剪異常處理
- 支持因拍照Activity被回收后的自動(dòng)恢復(fù)
- 支持多種壓縮工具
- 支持多種圖片選擇工具
導(dǎo)入項(xiàng)目
implementation 'com.jph.takephoto:takephoto_library:4.1.0'
截止我博客開源庫(kù)的版本是4.1.0
版本,想要查看最新的版本需要去開源庫(kù)查看朽缴。
使用方式1
通過(guò)繼承的方式實(shí)現(xiàn):
- 繼承
TakePhotoActivity
善玫、TakePhotoFragmentActivity
、TakePhotoFragment
三者之一. - 通過(guò)
getTakePhoto()
獲取TakePhoto
實(shí)例進(jìn)行相關(guān)操作密强。 - 重寫以下方法獲取結(jié)果:
void takeSuccess(TResult result);
void takeFail(TResult result,String msg);
void takeCancel();
這種方法能滿足大部分的需求茅郎。
通過(guò)組裝的方式
1.實(shí)現(xiàn)TakePhoto.TakeResultListener,InvokeListener接口或渤。
2.在 onCreate,onActivityResult,onSaveInstanceState方法中調(diào)用TakePhoto對(duì)用的方法系冗。
3.重寫onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults),添加如下代碼:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//以下代碼為處理Android6.0薪鹦、7.0動(dòng)態(tài)權(quán)限所需
TPermissionType type=PermissionManager.onRequestPermissionsResult(requestCode,permissions,grantResults);
PermissionManager.handlePermissionsResult(this,type,invokeParam,this);
}
4.重寫TPermissionType invoke(InvokeParam invokeParam)
方法奔害,添加如下代碼:
@Override
public TPermissionType invoke(InvokeParam invokeParam) {
TPermissionType type=PermissionManager.checkPermission(TContextWrap.of(this),invokeParam.getMethod());
if(TPermissionType.WAIT.equals(type)){
this.invokeParam=invokeParam;
}
return type;
}
5.添加如下代碼獲取TakePhoto
實(shí)例:
/**
* 獲取TakePhoto實(shí)例
* @return
*/
public TakePhoto getTakePhoto(){
if (takePhoto==null){
takePhoto= (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this,this));
}
return takePhoto;
}
這種方式是建立在第一種方式?jīng)]有辦法滿足我們的需求端考,所以使用第二種方式。
自定義UI
自定義UI
是比較常見的需求却特,因?yàn)槊總€(gè)UI設(shè)計(jì)師的風(fēng)格不一樣扶供。
自定義Toolbar
在res/layout
目錄中創(chuàng)建一個(gè)名為toolbar.xml
的布局文件,內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="@style/CustomToolbarTheme"
android:background="#ffa352">
</android.support.v7.widget.Toolbar>
自定義狀態(tài)欄
在res/values
目錄中創(chuàng)建一個(gè)名為colors.xml
的資源文件裂明,內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="multiple_image_select_primaryDark">#212121</color>
</resources>
自定義提示文字
在res/values
目錄的string.xml
文件沖添加如下代碼:
<resources>
<string name="album_view">選擇圖片</string>
<string name="image_view">單擊選擇</string>
<string name="add">確定</string>
<string name="selected">已選</string>
<string name="limit_exceeded">最多能選 %d 張</string>
</resources>
自定義裁切工具
在res/layout
目錄中創(chuàng)建一個(gè)名為crop__activity_crop.xml
與crop__layout_done_cancel.xml
的布局文件诚欠,內(nèi)容如下:
crop__activity_crop.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.soundcloud.android.crop.CropImageView
android:id="@+id/crop_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:background="@drawable/crop__texture"
android:layout_above="@+id/done_cancel_bar" />
<include
android:id="@+id/done_cancel_bar"
android:layout_alignParentBottom="true"
layout="@layout/crop__layout_done_cancel"
android:layout_height="50dp"
android:layout_width="match_parent" />
</RelativeLayout>
crop__layout_done_cancel.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Crop.DoneCancelBar">
<FrameLayout
android:id="@+id/btn_cancel"
style="@style/Crop.ActionButton">
<TextView style="@style/Crop.ActionButtonText.Cancel" />
</FrameLayout>
<FrameLayout
android:id="@+id/btn_done"
style="@style/Crop.ActionButton">
<TextView style="@style/Crop.ActionButtonText.Done" />
</FrameLayout>
</LinearLayout>