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
除非适用法律要求或以书面形式同意,否则根据许可证分配的软件是按照“原样”分发的,没有任何明示或暗示的任何形式的保证或条件。请参阅许可证,以获取执行许可条款和限制的特定语言。