Android 裁剪ucrop 工具

github地址:https://github.com/Yalantis/uCrop

1.导入

    implementation 'com.github.yalantis:ucrop:2.2.4'
allprojects {
   repositories {
      jcenter()
      maven { url "https://jitpack.io" }
   }
}

2.编译后使用

AndroidManifest:

<activity
    android:name="com.yalantis.ucrop.UCropActivity"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

调用方法(记得申请6.0 7.0权限):

  //裁剪后保存到文件中
                            Uri destinationUri = Uri.fromFile(new File(getCacheDir(), photos.get(0).name + "XXXCroppedImage.jpg"));
                            //图片裁剪

                            UCrop.Options options = new UCrop.Options();
                            //设置裁剪图片可操作的手势
                            options.setAllowedGestures(UCropActivity.SCALE, UCropActivity.ROTATE, UCropActivity.ALL);
                            //设置toolbar颜色
                            options.setToolbarColor(ActivityCompat.getColor(photo.getContext(), R.color.colorAccent));
                            //设置状态栏颜色
                            options.setStatusBarColor(ActivityCompat.getColor(photo.getContext(), R.color.colorAccent));
                            //是否能调整裁剪框
                            // options.setFreeStyleCropEnabled(true);
                            UCrop.of(photos.get(0).uri, destinationUri)
                                    .withAspectRatio(9, 9)
                                    .withMaxResultSize(200, 200)
                                    .withOptions(options)
                                    .start(UploadPhotoActivity.this);

裁剪后回调:

 


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
            final Uri resultUri = UCrop.getOutput(data);
            try {
//                Bitmap bit = BitmapFactory.decodeStream(getContentResolver().openInputStream(resultUri));
                //打印裁剪后路径
                LogUtils.debugInfo(resultUri.getPath());

                 //裁剪后加载  
                mImageLoader.loadImage(photo.getContext(), ImageConfigImpl
                        .builder()
                        .url(resultUri.getPath())
                        .imageView(photo)
                        .isCircle(true)
                        .build());
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else if (resultCode == UCrop.RESULT_ERROR) {
            final Throwable cropError = UCrop.getError(data);
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值