-
-
Notifications
You must be signed in to change notification settings - Fork 250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pr/cropper text label support #382
Changes from 3 commits
79e1d93
649c0ef
0ff3afa
f517a56
bec2ef1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,18 @@ class CropOverlayView | |
|
||
companion object { | ||
|
||
/** | ||
* Creates the paint object for drawing text label over crop overlay | ||
*/ | ||
private fun getTextPaint(options: CropImageOptions): Paint { | ||
return Paint().apply { | ||
strokeWidth = 1f | ||
textSize = options.cropperLabelTextSize | ||
style = Paint.Style.FILL | ||
textAlign = Paint.Align.CENTER | ||
this.color = options.cropperLabelTextColor | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change to:
|
||
/** Creates the Paint object for drawing. */ | ||
private fun getNewPaint(color: Int): Paint = | ||
Paint().apply { | ||
|
@@ -92,6 +104,8 @@ class CropOverlayView | |
/** The Paint used to darken the surrounding areas outside the crop area. */ | ||
private var mBackgroundPaint: Paint? = null | ||
|
||
private var mTextLabelPaint: Paint? = null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this |
||
|
||
/** Used for oval crop window shape or non-straight rotation drawing. */ | ||
private val mPath = Path() | ||
|
||
|
@@ -164,6 +178,14 @@ class CropOverlayView | |
var cornerShape: CropImageView.CropCornerShape? = null | ||
private set | ||
|
||
/** To show the text label over crop overlay **/ | ||
private var isCropLabelEnabled: Boolean = false | ||
/** Text to show over text label over crop overlay */ | ||
private var cropLabelText: String = "" | ||
/** Text color to apply over text label over crop overlay */ | ||
private var cropLabelTextSize: Float = 20f | ||
Check warning Code scanning / detekt Report magic numbers. Magic number is a numeric literal that is not defined as a constant and hence it's unclear what the purpose of this number is. It's better to declare such numbers as constants and give them a proper name. By default, -1, 0, 1, and 2 are not considered to be magic numbers.
This expression contains a magic number. Consider defining it to a well named constant.
|
||
/** Text color to apply over text label over crop overlay */ | ||
private var cropLabelTextColor = Color.WHITE | ||
/** the initial crop window rectangle to set */ | ||
private val mInitialCropWindowRect = Rect() | ||
|
||
|
@@ -253,7 +275,37 @@ class CropOverlayView | |
invalidate() | ||
} | ||
} | ||
/** | ||
* Sets the cropper label if it is enabled | ||
*/ | ||
fun setCropperTextLabelVisibility(isEnabled: Boolean) { | ||
this.isCropLabelEnabled = isEnabled | ||
invalidate() | ||
} | ||
|
||
/** | ||
* Sets the copy text for cropper text | ||
*/ | ||
fun setCropLabelText(textLabel: String?) { | ||
textLabel?.let { | ||
this.cropLabelText = it | ||
} | ||
} | ||
|
||
/** | ||
* Sets the text size for cropper text | ||
*/ | ||
fun setCropLabelTextSize(textSize: Float) { | ||
this.cropLabelTextSize = textSize | ||
invalidate() | ||
} | ||
/** | ||
* Sets the text color for cropper text | ||
*/ | ||
fun setCropLabelTextColor(textColor: Int) { | ||
this.cropLabelTextColor = textColor | ||
invalidate() | ||
} | ||
/** | ||
* Sets the guidelines for the CropOverlayView to be either on, off, or to show when resizing the | ||
* application. | ||
|
@@ -410,6 +462,10 @@ class CropOverlayView | |
fun setInitialAttributeValues(options: CropImageOptions) { | ||
mOptions = options | ||
mCropWindowHandler.setInitialAttributeValues(options) | ||
setCropLabelTextColor(options.cropperLabelTextColor) | ||
setCropLabelTextSize(options.cropperLabelTextSize) | ||
setCropLabelText(options.cropperLabelText) | ||
setCropperTextLabelVisibility(options.showCropLabel) | ||
setCropCornerRadius(options.cropCornerRadius) | ||
setCropCornerShape(options.cornerShape) | ||
setCropShape(options.cropShape) | ||
|
@@ -430,6 +486,7 @@ class CropOverlayView | |
getNewPaintOrNull(options.borderCornerThickness, options.borderCornerColor) | ||
mGuidelinePaint = getNewPaintOrNull(options.guidelinesThickness, options.guidelinesColor) | ||
mBackgroundPaint = getNewPaint(options.backgroundColor) | ||
mTextLabelPaint = getTextPaint(options) | ||
} | ||
|
||
/** | ||
|
@@ -570,10 +627,26 @@ class CropOverlayView | |
} | ||
// To retain the changes in Paint object when the App goes background this is required | ||
mBorderCornerPaint = getNewPaintOrNull(mOptions?.borderCornerThickness ?: 0.0f, mOptions?.borderCornerColor ?: Color.WHITE) | ||
drawCropLabelText(canvas) | ||
drawBorders(canvas) | ||
drawCorners(canvas) | ||
} | ||
|
||
/** Draws a text label (which can acts an helper text) on top of crop overlay **/ | ||
private fun drawCropLabelText(canvas: Canvas) { | ||
if (isCropLabelEnabled) { | ||
val rect = mCropWindowHandler.getRect() | ||
var xCoordinate = (rect.left + rect.right) / 2 | ||
var yCoordinate = rect.top - 50 | ||
|
||
mTextLabelPaint?.apply { | ||
textSize = cropLabelTextSize | ||
color = cropLabelTextColor | ||
} | ||
canvas.drawText(cropLabelText, xCoordinate, yCoordinate, mTextLabelPaint!!) | ||
canvas.save() | ||
} | ||
} | ||
|
||
/** Draw shadow background over the image not including the crop area. */ | ||
private fun drawBackground(canvas: Canvas) { | ||
val rect = mCropWindowHandler.getRect() | ||
|
Check warning
Code scanning / detekt
Report magic numbers. Magic number is a numeric literal that is not defined as a constant and hence it's unclear what the purpose of this number is. It's better to declare such numbers as constants and give them a proper name. By default, -1, 0, 1, and 2 are not considered to be magic numbers.