Skip to content

Commit

Permalink
Merge pull request #509 from skydoves/frescowebimage/scaletype
Browse files Browse the repository at this point in the history
Fix the scale type was not properly applied to the FrescoWebImage
  • Loading branch information
skydoves authored Jun 25, 2024
2 parents fe6fa8b + c8d68be commit 9f042d1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion fresco-websupport/api/fresco-websupport.api
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public final class com/skydoves/landscapist/fresco/websupport/FrescoWebImage {
public static final fun FrescoWebImage (Lkotlin/jvm/functions/Function0;Landroidx/compose/ui/Modifier;Ljava/lang/String;Landroidx/compose/ui/Alignment;Landroidx/compose/ui/layout/ContentScale;FLandroidx/compose/ui/graphics/ColorFilter;ILandroidx/compose/runtime/Composer;II)V
public static final fun FrescoWebImage (Lkotlin/jvm/functions/Function0;Landroidx/compose/ui/Modifier;Ljava/lang/String;Landroidx/compose/ui/Alignment;Landroidx/compose/ui/layout/ContentScale;FLandroidx/compose/ui/graphics/ColorFilter;Landroidx/compose/ui/graphics/painter/Painter;Landroidx/compose/runtime/Composer;II)V
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,24 @@

package com.skydoves.landscapist.fresco.websupport

import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.DefaultAlpha
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.res.painterResource
import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder
import com.facebook.drawee.drawable.ScalingUtils
import com.facebook.drawee.generic.GenericDraweeHierarchy
import com.facebook.drawee.generic.GenericDraweeHierarchyInflater
import com.facebook.drawee.view.DraweeHolder
import com.skydoves.landscapist.constraints.constraint
import com.skydoves.landscapist.rememberDrawablePainter

/**
Expand Down Expand Up @@ -87,15 +88,15 @@ public fun FrescoWebImage(
modifier: Modifier = Modifier,
contentDescription: String? = null,
alignment: Alignment = Alignment.Center,
contentScale: ContentScale = ContentScale.Fit,
contentScale: ContentScale = ContentScale.Crop,
alpha: Float = DefaultAlpha,
colorFilter: ColorFilter? = null,
@DrawableRes previewPlaceholder: Int = 0,
previewPlaceholder: Painter? = null,
) {
if (LocalInspectionMode.current && previewPlaceholder != 0) {
if (LocalInspectionMode.current && previewPlaceholder != null) {
Image(
modifier = modifier,
painter = painterResource(id = previewPlaceholder),
painter = previewPlaceholder,
alignment = alignment,
contentScale = contentScale,
alpha = alpha,
Expand All @@ -106,16 +107,18 @@ public fun FrescoWebImage(
}

val context = LocalContext.current
val builder = controllerBuilder()
val hierarchy = GenericDraweeHierarchyInflater.inflateBuilder(context, null).build()
val holder: DraweeHolder<GenericDraweeHierarchy> = DraweeHolder.create(hierarchy, context)
controllerBuilder().oldController = holder.controller
holder.controller = controllerBuilder().build()
builder.oldController = holder.controller.apply { this?.contentDescription = contentDescription }
holder.hierarchy.setActualImageScaleType(getScaleType(contentScale))
holder.controller = builder.build()

val topLevelDrawable = holder.topLevelDrawable
Box(modifier = modifier) {
BoxWithConstraints(modifier = modifier) {
if (topLevelDrawable != null) {
Image(
modifier = modifier,
modifier = Modifier.constraint(this),
painter = rememberDrawablePainter(topLevelDrawable),
contentDescription = contentDescription,
alignment = alignment,
Expand All @@ -126,3 +129,15 @@ public fun FrescoWebImage(
}
}
}

private fun getScaleType(contentScale: ContentScale): ScalingUtils.ScaleType {
return when (contentScale) {
ContentScale.Crop -> ScalingUtils.ScaleType.CENTER_CROP
ContentScale.FillWidth -> ScalingUtils.ScaleType.FIT_X
ContentScale.FillHeight -> ScalingUtils.ScaleType.FIT_Y
ContentScale.FillBounds -> ScalingUtils.ScaleType.FIT_XY
ContentScale.Fit -> ScalingUtils.ScaleType.FIT_CENTER
ContentScale.Inside -> ScalingUtils.ScaleType.CENTER_INSIDE
else -> ScalingUtils.ScaleType.CENTER_CROP
}
}
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
kotlin = "2.0.0"
dokka = "1.9.20"
jvmTarget = "11"
nexusPlugin = "0.26.0"
nexusPlugin = "0.29.0"
kotlinxCoroutines = "1.8.1"
kotlinBinaryCompatibility = "0.14.0"
androidGradlePlugin = "8.5.0"
androidGradlePlugin = "8.4.2"
androidxActivity = "1.9.0"
androidxMaterial = "1.12.0"
androidxComposeBom = "2024.06.00"
Expand Down

0 comments on commit 9f042d1

Please sign in to comment.