This repository hosts the cropView
module, a customizable view for croppingimages in Android applications.
- Flexible cropping for users
- Double tap focus
- Pinch Zoom
- Free Mode
- Size Displayer
- Auto Centered
- Animations
- Customizable corner styles and colors
- Adjustable gridlines
- Drag-to-crop functionality
- Easy integration with JitPack
- Aspect Ratio Mode
- Rotations
In your project-level build.gradle or settings.gradle file, add the JitPack repository:
repositories {
google()
mavenCentral()
maven { url "https://jitpack.io" }
}
In your app-level build.gradle file, add the library dependency. Use the latest version:
Groovy Version
implementation 'com.github.hypersoftdev:CropView:x.x.x'
Kts Version
implementation("com.github.hypersoftdev:CropView:x.x.x")
To integrate the CropView
into your XML layout, use the following code snippet:
<com.hypersoft.cropview.CropView
android:id="@+id/crop_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cropBackgroundColor="@color/white"
app:cropCornerColor="@color/black"
app:cropCornerLength="16dp"
app:cropCornerWidth="4dp"
app:cropDraggingState="drag_all"
app:cropGridlineColor="@color/gray"
app:cropGridlineWidth="1dp"
app:cropMargin="16dp" />
You can set the bitmap to the CropView using the following line of code:
cropView.setBitmap(it)
To initialize the CropView
, implement the onInitialized
callback and observe live updates of the crop view resolution (width, height):
fun initCropView() = with(cropView) {
onInitialized = {
updatePixels(getCropSizeOriginal())
}
observeCropViewResolution = {
updatePixels(it)
}
}
To retrieve and display the width and height of the cropped area, use the following method:
private fun updatePixels(rectF: RectF) {
val cropWidth = "W: ${rectF.width().toInt()}"
val cropHeight = "H: ${rectF.height().toInt()}"
binding.mtvWidthCrop.text = cropWidth
binding.mtvHeightCrop.text = cropHeight
}
You can customize the CropView settings as follows:
cropView.setCropCornerLength(value.toPx())
cropView.setCropCornerWidth(value.toPx())
cropView.setCropCornerColor(Color.BLUE)
cropView.onGridLineWidthSliderChange(value.toPx())
cropView.setGridlineColor(Color.WHITE)
You can manage dragging by setting conditions such as "All", "Corners only", "Edge only", "Image only", or "Corners & Edge only":
cropView.setDragState(position)
To obtain the cropped image as a Bitmap, use the following method:
val croppedImage = cropView.getCroppedImage()
screen-recording_compressed.mp4
Contributions are welcome! Feel free to open issues or submit pull requests.
This work would not have been possible without the invaluable contributions of Sohaib Ahmed. His expertise, dedication, and unwavering support have been instrumental in bringing this project to fruition.
We are deeply grateful for Sohaib Ahmed involvement and his belief in the importance of this work. His contributions have made a significant impact, and we are honored to have had the opportunity to collaborate with him.
Copyright 2023 Hypersoft Inc
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.