bumptech / glide

An image loading and caching library for Android focused on smooth scrolling
https://bumptech.github.io/glide/
Other
34.67k stars 6.12k forks source link

[Enhancement] Request to add 'clipToBounds: Boolean = true' parameter to Compose's GlideImage #5336

Open panpf opened 12 months ago

panpf commented 12 months ago

An image zoom component ZoomImage that I am developing that supports Compose encountered problems when adapting to GlideImage.

Because ContentScale.None needs to be supported, and the user can also drag to view content that cannot be seen at first, GlideImage needs to add a clipToBounds: Boolean = true parameter to control not clip the content.

The final modified location is at GlideModifier.kt at line 324:

private fun ContentDrawScope.drawOne(
    painter: Painter?,
    cache: CachedPositionAndSize?,
    drawOne: DrawScope.(Size) -> Unit
  ): CachedPositionAndSize? {
    ...

    clipRect {
      translate(currentPositionAndSize.position.x, currentPositionAndSize.position.y) {
        drawOne.invoke(this, currentPositionAndSize.size)
      }
    }
    return currentPositionAndSize
  }

The modification is as follows:

private fun ContentDrawScope.drawOne(
    painter: Painter?,
    cache: CachedPositionAndSize?,
    drawOne: DrawScope.(Size) -> Unit
  ): CachedPositionAndSize? {
    ...

    if (clipToBounds) {
      clipRect {
        translate(currentPositionAndSize.position.x, currentPositionAndSize.position.y) {
          drawOne.invoke(this, currentPositionAndSize.size)
        }
      }
    } else {
      translate(currentPositionAndSize.position.x, currentPositionAndSize.position.y) {
        drawOne.invoke(this, currentPositionAndSize.size)
      }
    }
    return currentPositionAndSize
  }

GlideImage needs to add the clipToBounds: Boolean = true parameter:

@Composable
public fun GlideImage(
  model: Any?,
  contentDescription: String?,
  modifier: Modifier = Modifier,
  alignment: Alignment = Alignment.Center,
  contentScale: ContentScale = ContentScale.Fit,
  alpha: Float = DefaultAlpha,
  colorFilter: ColorFilter? = null,
  clipToBounds: Boolean = true,
  // TODO(judds): Consider using separate GlideImage* methods instead of sealed classes.
  // See http://shortn/_x79pjkMZIH for an internal discussion.
  loading: Placeholder? = null,
  failure: Placeholder? = null,
  transition: Transition.Factory? = null,
  // TODO(judds): Consider defaulting to load the model here instead of always doing so below.
  requestBuilderTransform: RequestBuilderTransform<Drawable> = { it },
)

Please also consider my request!

If can't add parameters to support my request, I will have to clone all the source code of GlideImage and modify the implementation myself, but this will not be able to synchronize the updates of GlideImage in time, resulting in a split situation, which I don't want to see.