添加依赖
compile 'com.soundcloud.android:android-crop:1.0.1@aar'
通过相册选择图片进行裁剪
step 1
Crop.pickImage(context);
调用Crop.pick方法来打开相册。
step 2
在onActivityResult中通过requestCode的值来判断
if (requestCode == Crop.REQUEST_PICK && resultCode == RESULT_OK) {
/**
* 当调用相册选择一张图片后,将图片信息通过data传过来
* data.getData()获取到图片URI
*/
beginCrop(data.getData());
}
step 3
/**
* 调用Crop.pickImage后选择的图片调用Crop来裁剪
* @param source 通过相册选择的图片URI
*/
private void beginCrop(Uri source) {
Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
Crop.of(source, destination).asSquare().start(this);
}
step 4
调用Crop.start()后,在onActivityResult中判断requestCode的值
if (requestCode == Crop.REQUEST_CROP) {
//调用Crop.start后来到这
handleCrop(resultCode, data);
}
step 5
private void handleCrop(int resultCode, Intent result) {
if (resultCode == RESULT_OK) {
//如果裁剪正常,resultCode == RESULT_OK则到这里裁剪完成
tempimg.setImageURI(Crop.getOutput(result));
} else if (resultCode == Crop.RESULT_ERROR) {
Logger.d(""+Crop.getError(result).getMessage());
ShowToast.show(SetAfterRegisterActivity.this, Crop.getError(result).getMessage());
}
}
拍照后使用Crop裁剪图片
step 1
调用系统相机拍照,设置拍照后的照片的存储URI
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, snocrop);
startActivityForResult(intent, 1);// 采用ForResult打开
step 2
在onActivityResult中判断requestCode的值,就是刚才打开相机时设置的1.
if (resultCode == RESULT_OK) {
Crop.of(snocrop, scropped).asSquare().start(this);
}
这里传入刚才拍的照片的URI和裁剪后图片存放的URI。
由于调用了Crop.step(),所以接下来的步骤跟调用相册进行裁剪的方法一样了。