TextFlow装载网页内容
示例代码:
textFlow = TextConverter.importToFlow(chapter.content,TextConverter.TEXT_FIELD_HTML_FORMAT,_config);
传入的第一个参数:包含html标签的字符串。
传入的第二个参数:第一个参数的格式。
传入的第三个参数:创建TextFlow的配置。
TextFlow中显示图片
TextFlow中图片资源会被处理为:InlineGraphicElement类的实例。这个类的source属性,如果只是给了一个URL,需要异步加载图片资源,资源加载情况会影响这个类的status属性 ,在status属性发生变化后,会抛出StatusChangeEvent事件。想要让TextFlow显示图片,需要监听TextFlow抛出的这个事件,并调用 IFlowComposer.updateAllControllers()。
TextFlow中控制图片的尺寸
上述InlineGraphicElement 类中,有几个属性和图片的尺寸相关,分别是:
- width 和 height 属性
- measuredWidth 和 measuredHeight 属性,表示加载时图形的宽度或高度
- actualWidth 和 actualHeight 属性,表示通过 width 或 height 以及 measuredWidth 或 measuredHeight 计算得到的图形的实际显示和合成宽度和高度
加载完图片后,图片的measuredWidth和measuredHeight是图片的实际尺寸,如果这两个值大于显示的限制,修改这个类实例的 width 和 height 值,就可以控制图片实际显示的大小。
以下示例代码,判断如果图片如果超出尺寸,做相应的缩小处理:
var imgElement:InlineGraphicElement=evt.element as InlineGraphicElement;
trace(">>>>>>"+imgElement.source);
trace("0w:"+imgElement.width,imgElement.measuredWidth,imgElement.actualWidth);
trace("0h:"+imgElement.height,imgElement.measuredHeight,imgElement.actualHeight);
var wFlag:Boolean=imgElement.measuredWidth>=cWidth;
var hFlag:Boolean=imgElement.measuredHeight>=_containerHeight;
if(wFlag||hFlag)
{
trace("1w:"+imgElement.width,imgElement.measuredWidth,imgElement.actualWidth);
trace("1h:"+imgElement.height,imgElement.measuredHeight,imgElement.actualHeight);
if(wFlag&&hFlag)
{
var wRate:Number=imgElement.measuredWidth/cWidth;
var hRate:Number=imgElement.measuredHeight/cHeight;
if(wRate>=hRate)
{
imgElement.width=cWidth;
imgElement.height=imgElement.measuredHeight/wRate;
}else
{
imgElement.height=cHeight;
imgElement.width=imgElement.measuredWidth/hRate;
}
}else
{
if(hFlag)
{
imgElement.height=cHeight;
}else if(wFlag)
{
imgElement.width=cWidth;
}
}
trace("2w:"+imgElement.width,imgElement.measuredWidth,imgElement.actualWidth);
trace("2h:"+imgElement.height,imgElement.measuredHeight,imgElement.actualHeight);
}