Flex dynamically sets the icon in the Button to point to the Internet Image Source

Source: Internet
Author: User
IconUtility.

 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>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.