
在日常的Android
开发中,在做一些发布功能的时候,我们可能需要调用相册去选择图片或者视频。这里分享一个图片选择库,帮大家快速实现。
TakePhoto
TakePhoto
是一款用于在Android设备上获取照片(拍照或从相册、文件中选择)、裁剪图片、压缩图片的开源工具库,目前最新版本4.1.0。开源库的源码以及wiki
开源库的特性
- 支持通过相机拍照获取图片
- 支持从相册选择图片
- 支持从文件选择图片
- 支持批量图片选取
- 支持图片压缩以及批量图片压缩
- 支持图片裁切以及批量图片裁切
- 支持照片旋转角度自动纠正
- 支持自动权限管理(无需关心SD卡及摄像头权限等问题)
- 支持对裁剪及压缩参数个性化配置
- 提供自带裁剪工具(可选)
- 支持智能选取及裁剪异常处理
- 支持因拍照Activity被回收后的自动恢复
- 支持多种压缩工具
- 支持多种图片选择工具
导入项目
implementation 'com.jph.takephoto:takephoto_library:4.1.0'
截止我博客开源库的版本是4.1.0
版本,想要查看最新的版本需要去开源库查看。
使用方式1
通过继承的方式实现:
- 继承
TakePhotoActivity
、TakePhotoFragmentActivity
、TakePhotoFragment
三者之一. - 通过
getTakePhoto()
获取TakePhoto
实例进行相关操作。 - 重写以下方法获取结果:
void takeSuccess(TResult result);
void takeFail(TResult result,String msg);
void takeCancel();
这种方法能满足大部分的需求。
通过组装的方式
1.实现TakePhoto.TakeResultListener,InvokeListener接口。
2.在 onCreate,onActivityResult,onSaveInstanceState方法中调用TakePhoto对用的方法。
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动态权限所需
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
实例:
/**
* 获取TakePhoto实例
* @return
*/
public TakePhoto getTakePhoto(){
if (takePhoto==null){
takePhoto= (TakePhoto) TakePhotoInvocationHandler.of(this).bind(new TakePhotoImpl(this,this));
}
return takePhoto;
}
这种方式是建立在第一种方式没有办法满足我们的需求,所以使用第二种方式。
自定义UI
自定义UI
是比较常见的需求,因为每个UI设计师的风格不一样。
自定义Toolbar
在res/layout
目录中创建一个名为toolbar.xml
的布局文件,内容如下:
<?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>
自定义状态栏
在res/values
目录中创建一个名为colors.xml
的资源文件,内容如下:
<?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
目录中创建一个名为crop__activity_crop.xml
与crop__layout_done_cancel.xml
的布局文件,内容如下:
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>
网友评论