Android-分享一个图片选择库(TakePhoto)

2022-08-09  本文已影响0人  阿博聊编程
图片来源网络,入侵必删

在日常的Android开发中,在做一些发布功能的时候,我们可能需要调用相册去选择图片或者视频。这里分享一个图片选择库,帮大家快速实现。

TakePhoto

TakePhoto是一款用于在Android设备上获取照片(拍照或从相册、文件中选择)、裁剪图片、压缩图片的开源工具库,目前最新版本4.1.0。开源库的源码以及wiki

开源库的特性

导入项目

implementation 'com.jph.takephoto:takephoto_library:4.1.0'

截止我博客开源库的版本是4.1.0版本,想要查看最新的版本需要去开源库查看。

使用方式1

通过继承的方式实现:

  1. 继承TakePhotoActivityTakePhotoFragmentActivityTakePhotoFragment三者之一.
  2. 通过getTakePhoto()获取TakePhoto实例进行相关操作。
  3. 重写以下方法获取结果:
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.xmlcrop__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>
上一篇 下一篇

猜你喜欢

热点阅读