├── .gitignore ├── .idea ├── gradle.xml ├── inspectionProfiles │ └── Project_Default.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── qiaomu │ │ └── cropimage │ │ └── MainActivity.java │ └── res │ ├── drawable-hdpi │ ├── camera_crop_height.png │ ├── camera_crop_width.png │ ├── detail_photo_border.9.png │ ├── ic_menu_3d_globe.png │ ├── ic_menu_camera_video_view.png │ ├── ic_menu_view_details.png │ └── indicator_autocrop.png │ ├── drawable-xhdpi │ ├── camera_crop_height.png │ ├── camera_crop_width.png │ ├── detail_photo_border.9.png │ ├── ic_menu_3d_globe.png │ ├── ic_menu_camera_video_view.png │ ├── ic_menu_view_details.png │ └── indicator_autocrop.png │ ├── layout │ ├── activity_main.xml │ ├── cropimage.xml │ └── detailsview.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml │ └── xml │ └── file_paths.xml ├── art ├── 1.gif └── 2.gif ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── libcrop ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── android │ │ └── gallery3d │ │ └── crop │ │ ├── BoundedRect.java │ │ ├── CropActivity.java │ │ ├── CropDrawingUtils.java │ │ ├── CropExtras.java │ │ ├── CropMath.java │ │ ├── CropObject.java │ │ ├── CropView.java │ │ ├── GeometryMathUtils.java │ │ ├── ImageLoader.java │ │ ├── SaveImage.java │ │ └── Utils.java │ └── res │ ├── drawable-xhdpi │ ├── camera_crop.png │ ├── ic_crop_cancel.png │ ├── ic_crop_ok.png │ ├── ic_menu_savephoto.png │ └── ic_menu_savephoto_disabled.png │ ├── drawable │ ├── filtershow_button_background.xml │ ├── filtershow_button_selected_background.9.png │ ├── geometry_shadow.9.png │ └── menu_save_photo.xml │ ├── layout │ ├── crop_activity.xml │ └── filtershow_actionbar.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ └── strings.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 26 | 27 | 28 | 29 | 30 | 31 | 33 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 mrme2014 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # imageCrop 2 | # android6.0原生系统相册裁剪源码抽取改进 3 | 4 | [![996.icu](https://img.shields.io/badge/link-996.icu-red.svg)](https://996.icu) 5 | [![LICENSE](https://img.shields.io/badge/license-MIT%20(The%20996%20Prohibited%20License)-blue.svg)](https://github.com/996icu/996.ICU/blob/master/LICENSE) 6 | 7 | ### 由于安卓手机各个ROM从4.0到7.0 相册功能各有差异,从4.0以后原生都不在支持圆形裁剪,而各个厂家可能,注意可能会把自己ROM中相册增加圆形裁剪功能。 8 | ### 这样的话,调用原生系统裁剪就会有兼容问题。 9 | 10 | ### 于是乎,我把android6.0原生系统相册裁剪源码抽取了出来并改进,使用方式沿用Intent意图。 11 | 12 | - 支持圆形裁剪 13 | - 支持裁剪框宽高最小值的设定 14 | - 支持裁剪框网格是否显示 15 | - 优化裁剪框缩放到很小的时候,拖动不灵敏 16 | 17 | ### 矩形裁剪 18 | ![image](https://github.com/mrme2014/imageCrop/raw/master/art/1.gif) 19 | 20 | ### 圆形裁剪 21 | ![image](https://github.com/mrme2014/imageCrop/raw/master/art/2.gif) 22 | 23 | 24 | ### 添加依赖 25 | ```java 26 | dependencies{ 27 | 28 | compile 'com.qiaomu.library:imagecrop:1.0.1' 29 | } 30 | ``` 31 | 32 | ### 使用方式 33 | 34 | ```java 35 |        Intent intent = new Intent(this, CropActivity.class);//替换成“com.android.camera.action.CROP” 模拟器运行可查看原生裁剪是什么样子的 36 |        intent.setDataAndType(getUri("/sdcard/download/1.png"), "image/*"); 37 | intent.putExtra("crop", "true"); 38 | intent.putExtra("aspectX", 1); 39 | intent.putExtra("aspectY", 1); 40 | intent.putExtra("outputX", 300); 41 | intent.putExtra("outputY", 300); 42 | intent.putExtra("scale", false);//看源码,加不加问题不大,不加还会快一些,默认false 43 | intent.putExtra("return-data", false);//是否返回bitmap,建议不要用true,图片过大会崩溃的,默认false 44 | intent.putExtra("scaleUpIfNeeded", false); // 可避免莫名的黑边,加不加其实无所谓,默认false 45 | //intent.putExtra(CropActivity.MIN_CROP_WIDTH, 1080); //矩形裁剪情况下的 最下宽度度值px ,默认是40px 46 | //intent.putExtra(CropActivity.MIN_CROP_HEIGHT, 300);//矩形裁剪情况下的 最下高度值px,默认是40px 47 | intent.putExtra(CropActivity.CIRCLE_CROP, true); //是否是圆形裁剪,默认false 48 | intent.putExtra(CropActivity.DRAW_GRID, true); //是否显示裁剪网格,默认false 49 | intent.putExtra(MediaStore.EXTRA_OUTPUT, getUri("/sdcard/output.png")); 50 | 51 | if (Build.VERSION.SDK_INT > 23) { 52 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 53 | } 54 | 55 | startActivityForResult(intent, 1); 56 | ``` 57 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "26.0.1" 6 | defaultConfig { 7 | applicationId "com.qiaomu.cropimage" 8 | minSdkVersion 15 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | compile 'com.android.support:appcompat-v7:25.3.1' 25 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 26 | compile project(':libcrop') 27 | } 28 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Users\mrs\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 31 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /app/src/main/java/com/qiaomu/cropimage/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.qiaomu.cropimage; 2 | 3 | import android.Manifest; 4 | import android.content.Intent; 5 | import android.content.pm.PackageManager; 6 | import android.graphics.Bitmap; 7 | import android.graphics.BitmapFactory; 8 | import android.net.Uri; 9 | import android.os.Build; 10 | import android.provider.MediaStore; 11 | import android.support.annotation.NonNull; 12 | import android.support.v4.app.ActivityCompat; 13 | import android.support.v4.content.FileProvider; 14 | import android.support.v7.app.AppCompatActivity; 15 | import android.os.Bundle; 16 | import android.widget.ImageView; 17 | 18 | import com.android.gallery3d.crop.CropActivity; 19 | 20 | import java.io.File; 21 | import java.io.FileNotFoundException; 22 | 23 | public class MainActivity extends AppCompatActivity { 24 | 25 | @Override 26 | protected void onCreate(Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.activity_main); 29 | 30 | 31 | int check = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); 32 | if (check != PackageManager.PERMISSION_GRANTED) { 33 | ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100); 34 | } else { 35 | crop(); 36 | } 37 | } 38 | 39 | @Override 40 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 41 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 42 | crop(); 43 | } 44 | 45 | private void crop() { 46 | //还是按照原生Intent的使用方式,【MIN_CROP_WIDTH】 【MIN_CROP_HEIGHT】 【CIRCLE_CROP】 【DRAW_GRID】 新加的功能。 47 | // 【set-as-wallpaper】支持裁剪完后设置成壁纸,需要权限-----android.permission.SET_WALLPAPER 48 | // 其他的配置都是原生。 49 | 50 | Intent intent = new Intent(this, CropActivity.class);// 51 | intent.setDataAndType(getUri("/sdcard/download/1.png"), "image/*"); 52 | intent.putExtra("crop", "true"); 53 | intent.putExtra("aspectX", 1); 54 | intent.putExtra("aspectY", 1); 55 | intent.putExtra("outputX", 300); 56 | intent.putExtra("outputY", 300); 57 | intent.putExtra("scale", false);//看源码,加不加问题不大,不加还会快一些,默认false 58 | intent.putExtra("return-data", false);//是否返回bitmap,建议不要用true,图片过大会崩溃的,默认false 59 | intent.putExtra("scaleUpIfNeeded", false); // 可避免莫名的黑边,加不加其实无所谓,默认false 60 | // intent.putExtra(CropActivity.MIN_CROP_WIDTH, 1080); //矩形裁剪情况下的 最下宽度度值px ,默认是40px 61 | // intent.putExtra(CropActivity.MIN_CROP_HEIGHT, 300);//矩形裁剪情况下的 最下高度值px,默认是40px 62 | intent.putExtra(CropActivity.CIRCLE_CROP, true); //是否是圆形裁剪,默认false 63 | intent.putExtra(CropActivity.DRAW_GRID, true); //是否显示裁剪网格,默认false 64 | intent.putExtra(MediaStore.EXTRA_OUTPUT, getUri("/sdcard/output.png")); 65 | 66 | 67 | if (Build.VERSION.SDK_INT > 23) { 68 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 69 | } 70 | startActivityForResult(intent, 1); 71 | } 72 | 73 | private Uri getUri(String filepath) { 74 | if (Build.VERSION.SDK_INT >= 24) { 75 | //7.0以上的读取文件uri要用这种方式了 76 | return FileProvider.getUriForFile(this, "com.qiaomu.fileprovider", new File(filepath)); 77 | } else { 78 | return Uri.parse("file://" + filepath); 79 | } 80 | } 81 | 82 | 83 | @Override 84 | public void onActivityResult(int requestCode, int resultCode, Intent data) { 85 | switch (requestCode) { 86 | case 1: 87 | // 从剪切图片返回的数据 88 | if (resultCode == RESULT_OK) { 89 | if (data != null) { 90 | Uri data1 = data.getData(); 91 | try { 92 | Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(getUri("/sdcard/output.png"))); 93 | if (bitmap != null) { 94 | ImageView image = (ImageView) findViewById(R.id.image); 95 | image.setImageBitmap(bitmap); 96 | } 97 | } catch (FileNotFoundException e) { 98 | e.printStackTrace(); 99 | } 100 | } 101 | 102 | } 103 | break; 104 | } 105 | 106 | super.onActivityResult(requestCode, resultCode, data); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/camera_crop_height.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrme2014/imageCrop/a8b3490dd9a0352c3f0eea9e0f0c0eacd5aa6c7d/app/src/main/res/drawable-hdpi/camera_crop_height.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/camera_crop_width.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrme2014/imageCrop/a8b3490dd9a0352c3f0eea9e0f0c0eacd5aa6c7d/app/src/main/res/drawable-hdpi/camera_crop_width.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/detail_photo_border.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrme2014/imageCrop/a8b3490dd9a0352c3f0eea9e0f0c0eacd5aa6c7d/app/src/main/res/drawable-hdpi/detail_photo_border.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_menu_3d_globe.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrme2014/imageCrop/a8b3490dd9a0352c3f0eea9e0f0c0eacd5aa6c7d/app/src/main/res/drawable-hdpi/ic_menu_3d_globe.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_menu_camera_video_view.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrme2014/imageCrop/a8b3490dd9a0352c3f0eea9e0f0c0eacd5aa6c7d/app/src/main/res/drawable-hdpi/ic_menu_camera_video_view.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_menu_view_details.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrme2014/imageCrop/a8b3490dd9a0352c3f0eea9e0f0c0eacd5aa6c7d/app/src/main/res/drawable-hdpi/ic_menu_view_details.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/indicator_autocrop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrme2014/imageCrop/a8b3490dd9a0352c3f0eea9e0f0c0eacd5aa6c7d/app/src/main/res/drawable-hdpi/indicator_autocrop.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/camera_crop_height.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrme2014/imageCrop/a8b3490dd9a0352c3f0eea9e0f0c0eacd5aa6c7d/app/src/main/res/drawable-xhdpi/camera_crop_height.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/camera_crop_width.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrme2014/imageCrop/a8b3490dd9a0352c3f0eea9e0f0c0eacd5aa6c7d/app/src/main/res/drawable-xhdpi/camera_crop_width.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/detail_photo_border.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrme2014/imageCrop/a8b3490dd9a0352c3f0eea9e0f0c0eacd5aa6c7d/app/src/main/res/drawable-xhdpi/detail_photo_border.9.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/ic_menu_3d_globe.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrme2014/imageCrop/a8b3490dd9a0352c3f0eea9e0f0c0eacd5aa6c7d/app/src/main/res/drawable-xhdpi/ic_menu_3d_globe.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/ic_menu_camera_video_view.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrme2014/imageCrop/a8b3490dd9a0352c3f0eea9e0f0c0eacd5aa6c7d/app/src/main/res/drawable-xhdpi/ic_menu_camera_video_view.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/ic_menu_view_details.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrme2014/imageCrop/a8b3490dd9a0352c3f0eea9e0f0c0eacd5aa6c7d/app/src/main/res/drawable-xhdpi/ic_menu_view_details.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/indicator_autocrop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrme2014/imageCrop/a8b3490dd9a0352c3f0eea9e0f0c0eacd5aa6c7d/app/src/main/res/drawable-xhdpi/indicator_autocrop.png -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/res/layout/cropimage.xml: -------------------------------------------------------------------------------- 1 | 15 | 16 | 19 | 20 | 24 | 25 | 33 | 34 | 42 | 43 |