Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .documentation/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ cropImage.launch(
setMaxCropResultSize(99999, 99999)
setActivityTitle("")
setActivityMenuIconColor(0)
setActivityMenuTextColor(Color.BLACK)
setOutputUri(outputUri)
setOutputCompressFormat(Bitmap.CompressFormat.JPEG)
setOutputCompressQuality(90)
Expand All @@ -73,6 +74,10 @@ cropImage.launch(
setIntentChooserPriorityList(listOf("com.miui.gallery", "com.google.android.apps.photos"))
setActivityBackgroundColor(Color.BLACK)
setToolbarColor(Color.GRAY)
setToolbarColor(Color.WHITE)
setToolbarTitleColor(Color.BLACK)
setToolbarBackButtonColor(Color.BLACK)
setToolbarTintColor(Color.BLACK)
}
)
```
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- `Security` in case of vulnerabilities.

## [x.x.x] - unreleased
### Added
- Added options to customize colors of toolbar back button, title and menu texts. [#437](https://github.com/CanHub/Android-Image-Cropper/issues/437)
### Fixed
- Fixed and issue where setting toolbar color to white would do nothing. [#437](https://github.com/CanHub/Android-Image-Cropper/issues/437)

## [4.3.2] - 08/09/2022
### Fixed
Expand Down
72 changes: 70 additions & 2 deletions cropper/src/main/java/com/canhub/cropper/CropImageActivity.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.canhub.cropper

import android.content.Intent
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.util.Log
import android.view.KeyEvent
import android.view.Menu
Expand Down Expand Up @@ -79,6 +84,10 @@ open class CropImageActivity :
latestTmpUri = savedInstanceState.getString(BUNDLE_KEY_TMP_URI)?.toUri()
}

setCustomizations()
}

private fun setCustomizations() {
cropImageOptions.activityBackgroundColor.let { activityBackgroundColor ->
binding.root.setBackgroundColor(activityBackgroundColor)
}
Expand All @@ -90,9 +99,31 @@ open class CropImageActivity :
else
resources.getString(R.string.crop_image_activity_title)
it.setDisplayHomeAsUpEnabled(true)
cropImageOptions.toolbarColor.takeIf { color -> color != -1 }?.let { toolbarColor ->
cropImageOptions.toolbarColor?.let { toolbarColor ->
it.setBackgroundDrawable(ColorDrawable(toolbarColor))
}
cropImageOptions.toolbarTitleColor?.let { toolbarTitleColor ->
val spannableTitle: Spannable = SpannableString(title)
spannableTitle.setSpan(
ForegroundColorSpan(toolbarTitleColor),
0,
spannableTitle.length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
title = spannableTitle
}
cropImageOptions.toolbarBackButtonColor?.let { backBtnColor ->
try {
val upArrow = ContextCompat.getDrawable(
this,
androidx.appcompat.R.drawable.abc_ic_ab_back_material
)
upArrow?.colorFilter = PorterDuffColorFilter(backBtnColor, PorterDuff.Mode.SRC_ATOP)
it.setHomeAsUpIndicator(upArrow)
} catch (e: Exception) {

Check warning

Code scanning / detekt

Caught exception is too generic. Prefer catching specific exceptions to the case that is currently handled.

Caught exception is too generic. Prefer catching specific exceptions to the case that is currently handled.
e.printStackTrace()
}
}
}
}

Expand All @@ -111,7 +142,9 @@ open class CropImageActivity :
)
cropImageOptions.let { options ->
options.intentChooserTitle
?.takeIf { title -> title.isNotBlank() }
?.takeIf { title ->
title.isNotBlank()
}
?.let { icTitle ->
ciIntentChooser.setIntentChooserTitle(icTitle)
}
Expand Down Expand Up @@ -239,6 +272,19 @@ open class CropImageActivity :
)
}
}
cropImageOptions.activityMenuTextColor?.let { menuItemsTextColor ->
val menuItemIds = listOf(
R.id.ic_rotate_left_24,
R.id.ic_rotate_right_24,
R.id.ic_flip_24,
R.id.ic_flip_24_horizontally,
R.id.ic_flip_24_vertically,
R.id.crop_image_menu_crop
)
for (itemId in menuItemIds) {
updateMenuItemTextColor(menu, itemId, menuItemsTextColor)
}
}
return true
}

Expand Down Expand Up @@ -380,6 +426,28 @@ open class CropImageActivity :
}
}

/**
* Update the color of a specific menu item to the given color.
*/
open fun updateMenuItemTextColor(menu: Menu, itemId: Int, color: Int) {
val menuItem = menu.findItem(itemId)
val menuTitle = menuItem.title
if (menuTitle?.isNotBlank() == true) {
try {
val spannableTitle: Spannable = SpannableString(menuTitle)
spannableTitle.setSpan(
ForegroundColorSpan(color),
0,
spannableTitle.length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
menuItem.title = spannableTitle
} catch (e: Exception) {

Check warning

Code scanning / detekt

Caught exception is too generic. Prefer catching specific exceptions to the case that is currently handled.

Caught exception is too generic. Prefer catching specific exceptions to the case that is currently handled.
Log.w("AIC", "Failed to update menu item color", e)
}
}
}

enum class Source { CAMERA, GALLERY }

private companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,15 @@ data class CropImageContractOptions @JvmOverloads constructor(
return this
}

/**
* the color to use for action bar items texts.<br></br>
* *Default: NONE*
*/
fun setActivityMenuTextColor(activityMenuTextColor: Int): CropImageContractOptions {
cropImageOptions.activityMenuTextColor = activityMenuTextColor
return this
}

/**
* the Android Uri to save the cropped image to.<br></br>
* *Default: NONE, will create a temp file*
Expand Down Expand Up @@ -556,6 +565,35 @@ data class CropImageContractOptions @JvmOverloads constructor(
fun setToolbarColor(@ColorInt color: Int) = cropImageOptions.apply {
this.toolbarColor = color
}

/**
* Sets the toolbar color of the Crop Image Activity screen.
*/
fun setToolbarTitleColor(@ColorInt color: Int) = cropImageOptions.apply {
this.toolbarTitleColor = color
}

/**
* Sets the toolbar back button color of the Crop Image Activity screen.
*/
fun setToolbarBackButtonColor(@ColorInt color: Int) = cropImageOptions.apply {
this.toolbarBackButtonColor = color
}

/**
* Sets the tint color for all items on the toolbar of the Crop Image Activity screen.
* This includes setting the colors of the following together
* 1. Toolbar back button
* 2. Toolbar text
* 3. Toolbar menu icon color
* 4. Toolbar menu text color
*/
fun setToolbarTintColor(@ColorInt color: Int) = cropImageOptions.apply {
this.activityMenuTextColor = color
this.activityMenuIconColor = color
this.toolbarTitleColor = color
this.toolbarBackButtonColor = color
}
}

fun options(
Expand Down
40 changes: 36 additions & 4 deletions cropper/src/main/java/com/canhub/cropper/CropImageOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ open class CropImageOptions : Parcelable {
/** The shape of the cropping window. */
@JvmField
var cropShape: CropShape

/**
* The shape of cropper corners
*/
@JvmField
var cornerShape: CropImageView.CropCornerShape

/**
* The radius of the circular crop corner
*/
Expand Down Expand Up @@ -167,6 +169,7 @@ open class CropImageOptions : Parcelable {
@JvmField
@ColorInt
var borderCornerColor: Int

/**
* The fill color of circle corner
*/
Expand Down Expand Up @@ -235,6 +238,11 @@ open class CropImageOptions : Parcelable {
@ColorInt
var activityMenuIconColor: Int

/** the color to use for action bar items texts */
@JvmField
@ColorInt
var activityMenuTextColor: Int? = null

/** the Android Uri to save the cropped image to */
@JvmField
var customOutputUri: Uri?
Expand Down Expand Up @@ -349,7 +357,19 @@ open class CropImageOptions : Parcelable {

/** Toolbar color **/
@JvmField
var toolbarColor: Int = -1
var toolbarColor: Int? = null

/** Toolbar color **/
@JvmField
var toolbarTitleColor: Int? = null

/** Toolbar color **/
@JvmField
var toolbarBackButtonColor: Int? = null

/** Toolbar tint color **/
@JvmField
var toolbarTintColor: Int? = null

/** Init options with defaults. */
constructor() {
Expand Down Expand Up @@ -393,6 +413,7 @@ open class CropImageOptions : Parcelable {
maxCropResultHeight = 99999
activityTitle = ""
activityMenuIconColor = 0
activityMenuTextColor = null
customOutputUri = null
outputCompressFormat = CompressFormat.JPEG
outputCompressQuality = 90
Expand All @@ -418,7 +439,10 @@ open class CropImageOptions : Parcelable {
cropperLabelTextColor = Color.WHITE
showCropLabel = false
activityBackgroundColor = Color.WHITE
toolbarColor = -1
toolbarColor = null
toolbarTitleColor = null
toolbarBackButtonColor = null
toolbarTintColor = null
}

/** Create object from parcel. */
Expand Down Expand Up @@ -461,6 +485,7 @@ open class CropImageOptions : Parcelable {
maxCropResultHeight = parcel.readInt()
activityTitle = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel)
activityMenuIconColor = parcel.readInt()
activityMenuTextColor = parcel.readValue(Int::class.java.classLoader) as Int?
customOutputUri = parcel.readParcelable(Uri::class.java.classLoader)
outputCompressFormat = CompressFormat.valueOf(parcel.readString()!!)
outputCompressQuality = parcel.readInt()
Expand All @@ -487,7 +512,10 @@ open class CropImageOptions : Parcelable {
cropperLabelText = parcel.readString()!!
showCropLabel = parcel.readByte().toInt() != 0
activityBackgroundColor = parcel.readInt()
toolbarColor = parcel.readInt()
toolbarColor = parcel.readValue(Int::class.java.classLoader) as Int?
toolbarTitleColor = parcel.readValue(Int::class.java.classLoader) as Int?
toolbarBackButtonColor = parcel.readValue(Int::class.java.classLoader) as Int?
toolbarTintColor = parcel.readValue(Int::class.java.classLoader) as Int?
}

override fun writeToParcel(dest: Parcel, flags: Int) {
Expand Down Expand Up @@ -529,6 +557,7 @@ open class CropImageOptions : Parcelable {
dest.writeInt(maxCropResultHeight)
TextUtils.writeToParcel(activityTitle, dest, flags)
dest.writeInt(activityMenuIconColor)
dest.writeValue(activityMenuTextColor)
dest.writeParcelable(customOutputUri, flags)
dest.writeString(outputCompressFormat.name)
dest.writeInt(outputCompressQuality)
Expand All @@ -555,7 +584,10 @@ open class CropImageOptions : Parcelable {
dest.writeString(cropperLabelText)
dest.writeByte((if (showCropLabel) 1 else 0).toByte())
dest.writeInt(activityBackgroundColor)
dest.writeInt(toolbarColor)
dest.writeValue(toolbarColor)
dest.writeValue(toolbarTitleColor)
dest.writeValue(toolbarBackButtonColor)
dest.writeValue(toolbarTintColor)
}

override fun describeContents(): Int {
Expand Down
6 changes: 5 additions & 1 deletion sample/src/main/java/com/canhub/cropper/sample/SampleCrop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ internal class SampleCrop : Fragment() {
// setMaxCropResultSize(999, 999)
// setActivityTitle("CUSTOM title")
// setActivityMenuIconColor(RED)
// setActivityMenuTextColor(Color.BLACK)
// setOutputUri(outputUri)
// setOutputCompressFormat(Bitmap.CompressFormat.PNG)
// setOutputCompressQuality(50)
Expand All @@ -193,7 +194,10 @@ internal class SampleCrop : Fragment() {
))
*/
// setActivityBackgroundColor(Color.BLACK)
// setToolbarColor(Color.GRAY)
// setToolbarColor(Color.WHITE)
// setToolbarTitleColor(Color.BLACK)
// setToolbarBackButtonColor(Color.BLACK)
// setToolbarTintColor(Color.BLACK)
}
)
}
Expand Down