2

So I have my TextView named 'container'

container.setText(Html.fromHtml(text, new URLImageParser(container, this),null));

with URLImageParser from Android HTML.fromHTML() with images?

this works fine and displayes the image. The problem is that I want to define the size of the image in the html tag. <img src=".." width="30px"> doesn´t work in all kind of variations!

Any idea how to set the image size in the html?

Community
  • 1
  • 1
joohaha
  • 147
  • 2
  • 3
  • 12
  • 1
    *Any idea how to set the image size in the html?* You can't .... but i got an idea ... you could try to append size to the and of url ... fx http://example.com/images/image_1.png#120x100 ... and then make of use it in whithin `ImageGetter.getDrawable` ... – Selvin Jan 29 '16 at 15:26
  • @Selvin thank you that actually worked! Just seems a little bit messy... – joohaha Feb 01 '16 at 08:35
  • *a little bit messy* maybe ... but `Html.fromHtml()` supports only few tags and attributes ... so if you don't wana create ImageSpan by your own (by parsing HTML by yourself) then I think it's a acceptable way of doing this ... just take a look at [Html class source](https://github.com/android/platform_frameworks_base/blob/master/core/java/android/text/Html.java#L635) - the only attribute that it cares its "src" – Selvin Feb 01 '16 at 10:42
  • @Selvin yes I am actually working with this and it works fine. If you put this as an answer I will accept. – joohaha Feb 01 '16 at 11:24

1 Answers1

0

Since Html.fromHtml() only supports a few tags and attributes as noted by @Selvin in comments:

Any idea how to set the image size in the html? You can't .... but i got an idea ... you could try to append size to the and of url ... fx example.com/images/image_1.png#120x100 ... and then make of use it in whithin ImageGetter.getDrawable ... – Selvin Jan 29, 2016 at 15:26

Based on this I amended the text in my resource file where a specified size was desired with a '#' followed by a width value:

Old markup: \<img src="imgBall" width=150>
New markup: <img src="imgBall#150">

Then in the replacement ImageGetter:

class ResourceImageGetter(private val context: Context) : ImageGetter {
    override fun getDrawable(source: String): Drawable {
        val resources = context.resources
        val height = ScreenMetricsCompat.getScreenSize(context).height
        val width = ScreenMetricsCompat.getScreenSize(context).width

        val imgId = resources.getIdentifier(source, "drawable", context.getPackageName())
        val res = resources.getDrawable(imgId, context.theme)
        
        if (res.intrinsicWidth <= width && res.intrinsicHeight <= height) {
            res.setBounds(0, 0, res.intrinsicWidth, res.intrinsicHeight)
        } else if ...
            ....
        }
        return res
   }
}

Insert the code to extract the size from the source name:

class ResourceImageGetter(private val context: Context) : ImageGetter {
    override fun getDrawable(source: String): Drawable {
        val resources = context.resources
        var height = ScreenMetricsCompat.getScreenSize(context).height
        var width = ScreenMetricsCompat.getScreenSize(context).width

        // extract the resource name and size from the input
        var sourceImage = source
        val imgHasWidth = source.indexOf('#')
        if (imgHasWidth > 0) {
            sourceImage = source.take(imgHasWidth)
            width = source.takeLast(source.length - imgHasWidth - 1).toInt()
        }
    
        // change 'source' to 'sourceImage' otherwise results in 'resource not found'
        val imgId = resources.getIdentifier(sourceImage, "drawable", context.getPackageName())
        val res = resources.getDrawable(imgId, context.theme)
        
        if (res.intrinsicWidth <= width && res.intrinsicHeight <= height) {
            res.setBounds(0, 0, res.intrinsicWidth, res.intrinsicHeight)
        } else if ...
            ....
        }
        return res
   }
}            

In the ImageGetter, the screen height and width are obtained to set bounds on the image, that it fits on the screen. This modification replaces the screen width with the specified image width as the new width bound. The aspect ratio remains the same.

Mick
  • 811
  • 14
  • 31