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.