1.先導入函示庫(build.gradle: Project)
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
2.先導入函示庫(build.gradle: Module)
dependencies {
implementation 'com.github.wzgl5533:CropView:1.1'
}
3.程式碼範例
a.Layout(遮罩要跟圖片大小一樣)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/dinosaur" />
<com.qlh.crop.cropviewlibrary.view.CropView
android:id="@+id/cropView"
android:layout_width="0dp"
android:layout_height="0dp"
app:cv_crop_model="rect "
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="@+id/imageView"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toTopOf="@+id/imageView" />
<ImageView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
b.設定要裁切的圖片
cropView.setSourceBitmap(BitmapFactory.decodeResource(resources, R.drawable.dinosaur))
cropView.setCropBitmapCallBack {
result.setImageBitmap(it)
}
4.效果展示
作者的Github
CropView