Skip to content

Commit

Permalink
Support Image cropping
Browse files Browse the repository at this point in the history
  • Loading branch information
brahmkshatriya committed Mar 21, 2024
1 parent 12d55eb commit bd639a4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fun ImageHolder?.createRequest(
request.data(url)
request.headers(headers.toHeaders())
}

is ImageHolder.UriHolder -> request.data(uri)
}
placeholder?.let { request.placeholder(it) }
Expand All @@ -43,6 +44,10 @@ fun ImageHolder?.loadInto(
) {
var request = createRequest(imageView.context, placeholder, errorDrawable)
request = request.target(imageView)
imageView.scaleType = when (this?.crop) {
true -> ImageView.ScaleType.CENTER_CROP
else -> ImageView.ScaleType.FIT_CENTER
}
imageView.context.imageLoader.enqueue(request.build())
}

Expand All @@ -53,6 +58,10 @@ fun ImageHolder?.loadWith(
block: (Drawable?) -> Unit
) {
var request = createRequest(imageView.context, placeholder, errorDrawable)
imageView.scaleType = when (this?.crop) {
true -> ImageView.ScaleType.CENTER_CROP
else -> ImageView.ScaleType.FIT_CENTER
}
val target: (Drawable?) -> Unit = {
imageView.load(it)
block(it)
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/layout/item_error.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:layout_marginBottom="64dp"
android:layout_marginVertical="48dp"
android:gravity="center"
android:orientation="vertical">

<TextView
android:id="@+id/error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_margin="24dp"
android:maxLines="2"
android:text="@string/failed_loading" />

<LinearLayout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,27 @@ import kotlinx.parcelize.Parcelize

@Parcelize
sealed class ImageHolder : Parcelable {
data class UrlHolder(val url: String, val headers: Map<String, String>) : ImageHolder()
data class UriHolder(val uri: Uri) : ImageHolder()
data class BitmapHolder(val bitmap: Bitmap) : ImageHolder()
abstract val crop: Boolean

data class UrlHolder(
val url: String, val headers: Map<String, String>,
override val crop: Boolean
) : ImageHolder()

data class UriHolder(val uri: Uri, override val crop: Boolean) : ImageHolder()
data class BitmapHolder(val bitmap: Bitmap, override val crop: Boolean) : ImageHolder()

companion object {
fun Uri.toImageHolder(): UriHolder {
return UriHolder(this)
fun Uri.toImageHolder(crop: Boolean = false): UriHolder {
return UriHolder(this, crop)
}

fun String.toImageHolder(headers: Map<String, String> = mapOf()): UrlHolder {
return UrlHolder(this, headers)
fun String.toImageHolder(headers: Map<String, String> = mapOf(), crop: Boolean = false): UrlHolder {
return UrlHolder(this, headers, crop)
}

fun Bitmap.toImageHolder(): BitmapHolder {
return BitmapHolder(this)
fun Bitmap.toImageHolder(crop: Boolean = false): BitmapHolder {
return BitmapHolder(this, crop)
}
}
}
Expand Down

0 comments on commit bd639a4

Please sign in to comment.