我在这篇文章http://www.adobe.com/devnet/flex/articles/flex-mobile-performance-checklist.html中读到我不应该在 creationComplete 处理程序中初始化视图的外观。相反,我应该在重写的数据设置器中更改视图的外观。
文章中的部分是:
覆盖数据设置器,而不是使用绑定或在 creationComplete 处理程序中初始化视图的外观
1-首先,我想知道我是否做对了以下操作:
//My code is loading a set of images and adding them in a View.
//On creationComplete of the View I am adding the images in case this is the first time
//the view is shown. In case the view has been already accessed I use the data:
protected function view1_creationCompleteHandler(event:FlexEvent):void
{
if(!data) //On first creation of the view I create the data object
{
data = new Object();
data.imageArray = new Array(); //set an array that will cache my images.
for(var i:int = 0; i<36;i++)
{
var img:Image = new Image();
img.source = 'assets/0'+i.toString()+'.png';
container.addElement(img);
(data.imageArray as Array).push(img);//Override the data for next time!
}
}
else//Next time use the save images
{
for(var ix:int = 0; ix<(data.imageArray as Array).length;ix++)
{
container.addElement((data.imageArray as Array)[ix]);
}
}
}
如果我这样做正确,我想知道哪种方法最好。上面的一个,或者我要展示的下一个,它使用图像 contentLoader 并通过 ContentCache 启用缓存和排队:
protected function view1_creationCompleteHandler(event:FlexEvent):void
{
{
for(var i:int = 0; i<36;i++)
{
var img:Image = new Image();
img.contentLoader = ldr;
img.contentLoaderGrouping = 'gr1';
img.source = 'assets/0'+i.toString()+'.png';
container.addElement(img);
}
}
<fx:Declarations>
<s:ContentCache id="ldr" enableQueueing="true"
maxActiveRequests="1" maxCacheEntries="36"/>
</fx:Declarations>
另外,如果有人能告诉我 contentLoaderGrouping 的用途。我会很感激。非常感谢!!!
PS:顺便说一句,这两种方法都有效。第一种方法是即时的,而第二种方法以非常平滑的方式显示添加的图像,实际上产生了很酷的效果。