Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.open-android:uCrop:v1.0.0'
}
dependencies {
implementation("com.github.open-android:uCrop:v1.0.0")
}
<dependency>
<groupId>com.github.open-android</groupId>
<artifactId>uCrop</artifactId>
<version>v1.0.0</version>
</dependency>
libraryDependencies += "com.github.open-android" % "uCrop" % "v1.0.0"
:dependencies [[com.github.open-android/uCrop "v1.0.0"]]
开源地址:https://github.com/open-android/uCrop
PS:如果觉得文章太长,你也可观看该课程的视频,亲,里面还有高清,无码的福利喔
一款极具观赏性的图片裁剪库
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
compile 'com.github.open-android:uCrop:v1.0.0'
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:onClick="start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始裁剪图片"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv"/>
</LinearLayout>
private ImageView mIv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIv = (ImageView) findViewById(R.id.iv);
}
public void start(View view){
//需要裁剪的图片路径
Uri sourceUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory() , "icon_18.jpg"));
//裁剪完毕的图片存放路径
Uri destinationUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory() , "icon_18_2.jpg"));
UCrop.of(sourceUri, destinationUri) //定义路径
.withAspectRatio(4, 3) //定义裁剪比例 4:3 , 16:9
.withMaxResultSize(100, 100) //定义裁剪图片宽高最大值
.start(this);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//裁剪成功后调用
if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
final Uri resultUri = UCrop.getOutput(data);
//设置裁剪完成后的图片显示
mIv.setImageURI(resultUri);
//出错时进入该分支
} else if (resultCode == UCrop.RESULT_ERROR) {
final Throwable cropError = UCrop.getError(data);
}
}
<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
详细的使用方法在DEMO里面都演示啦,如果你觉得这个库还不错,请赏我一颗star吧~~~
欢迎关注微信公众号