Flex動態設定Button中的icon指向外網圖片源

來源:互聯網
上載者:User
IconUtility.as

package
{
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.geom.Matrix;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.utils.Dictionary;
import mx.containers.accordionClasses.AccordionHeader;
import mx.controls.tabBarClasses.Tab;
import mx.core.BitmapAsset;
import mx.core.UIComponent;

public class IconUtility extends BitmapAsset
{
private static var dictionary:Dictionary;
public static function getClass( target:UIComponent, source:String, width:Number = NaN, height:Number = NaN ):Class
{
if(!dictionary)
{
dictionary = new Dictionary(false);
}
//if(source is String)
//{
var loader:Loader = new Loader();
loader.load(new URLRequest(source as String), new LoaderContext(true));
//source = loader;
//}
dictionary[target] = { source:loader, width:width, height:height };
return IconUtility;
}

public function IconUtility():void
{
addEventListener(Event.ADDED, addedHandler, false, 0, true)
}

private function addedHandler(event:Event):void
{
if(parent)
{
if(parent is AccordionHeader)
{
var header:AccordionHeader = parent as AccordionHeader;
getData(header.data);
}
else if(parent is Tab)
{
var tab:Tab = parent as Tab;
getData(tab.data);
}
else
{
getData(parent);
}
}
}

private function getData(object:Object):void
{
var data:Object = dictionary[object];
if(data)
{
var source:Object = data.source;
if(data.width > 0 && data.height > 0)
{
bitmapData = new BitmapData(data.width, data.height, true, 0x00FFFFFF);
}
if(source is Loader)
{
var loader:Loader = source as Loader;
if(!loader.content)
{
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler, false, 0, true);
}
else
{
displayLoader(loader);
}
}
}
}

private function displayLoader( loader:Loader ):void
{
if(!bitmapData)
{
bitmapData = new BitmapData(loader.content.width, loader.content.height, true, 0x00FFFFFF);
}

bitmapData.draw(loader, new Matrix(bitmapData.width/loader.width, 0, 0, bitmapData.height/loader.height, 0, 0));

if(parent is UIComponent)
{
var component:UIComponent = parent as UIComponent;
component.invalidateSize();
}
}

private function completeHandler(event:Event):void
{
if(event && event.target && event.target is LoaderInfo)
{
displayLoader(event.target.loader as Loader);
}
}
}
}

 

main.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import com.IconUtility;
public function open():void
{
navigateToURL(new URLRequest("http://hi.baidu.com/woaidelphi/"), "_blank");
}
]]>
</mx:Script>
<mx:Button id="button" label="Button"
icon="{IconUtility.getClass(button, 'http://hiphotos.baidu.com/woaidelphi/abpic/item/ace9cd3836b88206b9998fe9.jpg')}" x="488" y="233" width="278" height="107"
click="open()"/>
</mx:Application>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.