图片裁剪之CropImageViewdd

当我设置头像的时候,选择的图片未必适合,所以这个时候需要把图片裁剪一下。下面分两种来简单的介绍一下图片裁剪的方法。

一:调用系统的裁剪api,来完成裁剪

	/**
	 *图片剪切 
	 */
	public static void cropImage(Activity mActivity,Uri uri){
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		intent.putExtra("crop", "true");
		// 裁剪框的比例,1:1
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// 裁剪后输出图片的尺寸大小
		intent.putExtra("outputX", 216);
		intent.putExtra("outputY", 216);
		intent.putExtra("outputFormat", "JPEG");// 图片格式
		intent.putExtra("noFaceDetection", true);// 取消人脸识别
		intent.putExtra("return-data", true);
		// 开启一个带有返回值的Activity
		mActivity.startActivityForResult(intent, SelfCenterActivity.REQUEST_CODE_IMAGE_CROP);
	}
	
二:分享一个第三方的图片裁剪库: Android-Image-Cropper
1.Include the library

compile 'com.theartofdev.edmodo:android-image-cropper:2.0.+'
2.在layout中布置CropImageView

    <com.weight.cropImages.CropImageView
        android:id="@+id/cropImageView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:cropAutoZoomEnabled="false"
        app:cropFixAspectRatio="true"/>
3.设置要裁剪的image

cropImageView.setImageBitmap(bitmap);
or
cropImageView.setImageUriAsync(uri);
4.获取裁剪的图片

Bitmap cropped = cropImageView.getCroppedImage();
// or (must subscribe to async event using cropImageView.setOnGetCroppedImageCompleteListener(listener))
cropImageView.getCroppedImageAsync(CropImageView.CropShape.RECTANGLE, 400, 400);
5.获取源码

(1)源码1:CSDN下载地址

(2)源码2:Git下载地址





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值