dependencies {
implementation( " com.vanniktech:android-image-cropper:4.6.0 " )
}
使用庫有3種方法。查看示例應用程序以獲取所有詳細信息。
注意:這種方式將被棄用,並將在以後的版本中刪除。前進的道路是編寫自己的活動,自己處理所有Uri
東西並使用CropImageView
。
class MainActivity : AppCompatActivity () {
private val cropImage = registerForActivityResult( CropImageContract ()) { result ->
if (result.isSuccessful) {
// Use the cropped image URI.
val croppedImageUri = result.uriContent
val croppedImageFilePath = result.getUriFilePath( this ) // optional usage
// Process the cropped image URI as needed.
} else {
// An error occurred.
val exception = result.error
// Handle the error.
}
}
private fun startCrop () {
// Start cropping activity with guidelines.
cropImage.launch(
CropImageContractOptions (
cropImageOptions = CropImageOptions (
guidelines = Guidelines . ON
)
)
)
// Start cropping activity with gallery picker only.
cropImage.launch(
CropImageContractOptions (
pickImageContractOptions = PickImageContractOptions (
includeGallery = true ,
includeCamera = false
)
)
)
// Start cropping activity for a pre-acquired image with custom settings.
cropImage.launch(
CropImageContractOptions (
uri = imageUri,
cropImageOptions = CropImageOptions (
guidelines = Guidelines . ON ,
outputCompressFormat = Bitmap . CompressFormat . PNG
)
)
)
}
// Call the startCrop function when needed.
}
注意:這是前進的唯一途徑,在自己的活動中添加CropImageView
並做任何您想做的事。查看樣本以獲取更多詳細信息。
<!-- Image Cropper fill the remaining available height -->
< com .canhub.cropper.CropImageView
android : id = " @+id/cropImageView "
android : layout_width = " match_parent "
android : layout_height = " 0dp "
android : layout_weight = " 1 "
/>
cropImageView.setImageUriAsync(uri)
// Or prefer using uri for performance and better user experience.
cropImageView.setImageBitmap(bitmap)
// Subscribe to async event using cropImageView.setOnCropImageCompleteListener(listener)
cropImageView.getCroppedImageAsync()
// Or.
val cropped : Bitmap = cropImageView.getCroppedImage()
注意:這種方式也被棄用,並將在以後的版本中刪除。前進的道路是編寫自己的活動,自己處理所有Uri
東西並使用CropImageView
。
如果您想擴展CropImageActivity
請注意,您需要設置您的CropImageView
CropImageActivity
添加到您的androidManifest.xml中 <!-- Theme is optional and only needed if default theme has no action bar. -->
< activity
android : name = " com.canhub.cropper.CropImageActivity "
android : theme = " @style/Base.Theme.AppCompat "
/>
super.onCreate(savedInstanceState)
之後,設置您的CropImageView
override fun onCreate ( savedInstanceState : Bundle ? ) {
super .onCreate(savedInstanceState)
setCropImageView(binding.cropImageView)
}
直接調用作物時,庫將促使用戶在畫廊或相機之間進行選擇(如果兩者都保持啟用)。我們為此使用Android默認AlertDialog。如果您想通過應用程序主題對其進行自定義,則需要在擴展活動時覆蓋方法showImageSourceDialog(..)
(上圖)
override fun showImageSourceDialog ( openSource : ( Source ) -> Unit ) {
super .showImageSourceDialog(openCamera)
}
首先使用版本4.3.3:
dependencies {
implementation( " com.vanniktech:android-image-cropper:4.3.3 " )
}
- import com.theartofdev.edmodo.cropper.CropImage
- import com.theartofdev.edmodo.cropper.CropImageActivity
+ import com.canhub.cropper.CropImage
+ import com.canhub.cropper.CropImageActivity
- <com.theartofdev.edmodo.cropper.CropImageView
+ <com.canhub.cropper.CropImageView
使用活動合同時,請諮詢示例應用程序,以了解如何使用我們的活動合同,因為onActivityResult
被棄用了。
4.3.3之後的版本已更改了API,最好單獨升級到每個次要版本,刪除不推薦使用的API使用情況並繼續升級。因此,使用4.3.3後,升級到4.4.0,升級到4.5.0、4.6.0,等等。
源自Arthurhub最初從Edmodo/Cropper分叉。
2016年版權所有,Arthur Teplitzki,2013年,Edmodo,Inc.。
根據Apache許可證(版本2.0(“許可”)獲得許可;除了符合許可外,您不得使用此工作。您可以在許可證文件中獲得許可證的副本,也可以在:
http://www.apache.org/licenses/license-2.0
除非適用法律要求或以書面形式同意,否則根據許可證分配的軟件是按照“原樣”分發的,沒有任何明示或暗示的任何形式的保證或條件。請參閱許可證,以獲取執行許可條款和限制的特定語言。