1.获取图片框架:https://github.com/LuckSiege/PictureSelector
2.图片裁剪框架:https://github.com/jdamcd/android-crop
3.Glide图片加载框架:https://github.com/bumptech/glide
2.build.gradle依赖:
Groovy
dependencies {
// PictureSelector basic (Necessary)
implementation 'io.github.lucksiege:pictureselector:v3.11.2'
// image compress library (Not necessary)
implementation 'io.github.lucksiege:compress:v3.11.2'
// uCrop library (Not necessary)(图片裁剪框架,不支持横屏)
//implementation 'io.github.lucksiege:ucrop:v3.11.2'
//android-crop 图片裁剪框架,支持横屏
implementation 'com.soundcloud.android:android-crop:1.0.1@aar'
// simple camerax library (Not necessary)
implementation 'io.github.lucksiege:camerax:v3.11.2'
//Glide
implementation 'com.github.bumptech.glide:glide:4.16.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0'
}
2.Activity 设置:
java
/**
* 获取本地图片或拍照
* @param ivBigCover
*/
private void pickerImage(ImageView ivBigCover) {
PictureSelector.create(this)
.openGallery(SelectMimeType.ofImage())
.setImageEngine(new GlideEngine())
.setMaxSelectNum(1)
.setCropEngine(new CropFileEngine() {
@Override
public void onStartCrop(Fragment fragment, Uri srcUri, Uri destinationUri, ArrayList<String> dataSource, int requestCode) {
//android-crop图片裁剪
Crop.of(srcUri, destinationUri)
.asSquare()
.start(fragment.getContext(), fragment, requestCode);
}
})
.forResult(new OnResultCallbackListener<LocalMedia>() {
@Override
public void onResult(ArrayList<LocalMedia> result) {
Log.i(TAG, "onResult=" + result);
if (result != null && result.get(0).getCutPath() != null) {
ivBigCover.setImageURI(Uri.parse(result.get(0).getCutPath()));
Log.i(TAG, "onResult=" + result.get(0).getCutPath());
}
}
@Override
public void onCancel() {
Log.i(TAG, "onCancel=");
}
});
}
3.GlideEngine:
java
package com.example.mygtest;
import android.content.Context;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.luck.picture.lib.engine.ImageEngine;
import com.luck.picture.lib.utils.ActivityCompatHelper;
/**
* @author:luck
* @date:2019-11-13 17:02
* @describe:Glide加载引擎
*/
public class GlideEngine implements ImageEngine {
/**
* 加载图片
*
* @param context 上下文
* @param url 资源url
* @param imageView 图片承载控件
*/
@Override
public void loadImage(Context context, String url, ImageView imageView) {
if (!ActivityCompatHelper.assertValidRequest(context)) {
return;
}
Glide.with(context)
.load(url)
.into(imageView);
}
@Override
public void loadImage(Context context, ImageView imageView, String url, int maxWidth, int maxHeight) {
if (!ActivityCompatHelper.assertValidRequest(context)) {
return;
}
Glide.with(context)
.load(url)
//.override(maxWidth, maxHeight)
.into(imageView);
}
/**
* 加载相册目录封面
*
* @param context 上下文
* @param url 图片路径
* @param imageView 承载图片ImageView
*/
@Override
public void loadAlbumCover(Context context, String url, ImageView imageView) {
if (!ActivityCompatHelper.assertValidRequest(context)) {
return;
}
Glide.with(context)
.asBitmap()
.load(url)
//.override(180, 180)
//.sizeMultiplier(0.5f)
//.transform(new CenterCrop(), new RoundedCorners(8))
//.placeholder(R.mipmap.ic_launcher)
.into(imageView);
}
/**
* 加载图片列表图片
*
* @param context 上下文
* @param url 图片路径
* @param imageView 承载图片ImageView
*/
@Override
public void loadGridImage(Context context, String url, ImageView imageView) {
if (!ActivityCompatHelper.assertValidRequest(context)) {
return;
}
Glide.with(context)
.load(url)
// .override(200, 200)
// .centerCrop()
// .placeholder(R.mipmap.ic_launcher)
.into(imageView);
}
@Override
public void pauseRequests(Context context) {
if (!ActivityCompatHelper.assertValidRequest(context)) {
return;
}
Glide.with(context).pauseRequests();
}
@Override
public void resumeRequests(Context context) {
if (!ActivityCompatHelper.assertValidRequest(context)) {
return;
}
Glide.with(context).resumeRequests();
}
private static final class InstanceHolder {
static final GlideEngine instance = new GlideEngine();
}
public static GlideEngine createGlideEngine() {
return InstanceHolder.instance;
}
}