noties / Markwon

Android markdown library (no WebView)
https://noties.io/Markwon/
Apache License 2.0
2.76k stars 313 forks source link

[Question] inline images #396

Closed qwerty287 closed 1 year ago

qwerty287 commented 2 years ago

Hello, I'd like to display inline images in my app, similar to emojis. The images are in the PNG format (loaded from the internet) and should have the same size as the text. (I don't care about scaled/too small images, they should be scaled to text size). Is there any simple way to do this?

noties commented 1 year ago

Hello @qwerty287 ,

sure, ImageSizeResolver receives textSize which you can use to adjust image bounds

val markwon = Markwon.builder(context)
  .usePlugin(object : AbstractMarkwonPlugin() {
    override fun configureConfiguration(builder: MarkwonConfiguration.Builder) {
      builder.imageSizeResolver(object : ImageSizeResolverDef() {
        override fun resolveImageSize(
          imageSize: ImageSize?,
          imageBounds: Rect,
          canvasWidth: Int,
          textSize: Float
        ): Rect {
          val side = textSize.roundToInt()
          return Rect(
            0, 0,
            side, side
          )
        }
      })
    }
  })
  .usePlugin(ImagesPlugin.create())
  .usePlugin(HtmlPlugin.create())
  .build()