Tree動態顯示Icon
MyIcon.as:
Test.mxml:
參照這篇文章寫的:
http://blog.chinaunix.net/u/21752/showart_2089230.html
package controls
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Graphics;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.TimerEvent;
import flash.net.URLRequest;
import flash.utils.Dictionary;
import flash.utils.Timer;
import mx.core.UIComponent;
import mx.skins.ProgrammaticSkin;
import mx.utils.NameUtil;
import mx.utils.StringUtil;
public class MyIcon extends ProgrammaticSkin
{
public static var showWaitIcon:Boolean = true;
private static var timer:Timer = null;
private static var bitmapDataIdList:Array = [];
private static var bitmapDatas:Dictionary = new Dictionary();
public static function getIconClass(target:UIComponent, url:String,
imageWidth:Number, imageHeight:Number):Class
{
url = StringUtil.trim(url);
imageWidth = (isNaN(imageWidth) ? 16 : imageWidth);
imageHeight = (isNaN(imageHeight) ? 16 : imageHeight);
var targetId:String = NameUtil.displayObjectToString(target);
var bitmapDataId:String = url + "(" + imageWidth + "*" + imageHeight + ")";
bitmapDataIdList.push({targetId: targetId, bitmapDataId: bitmapDataId});
if (!bitmapDatas[bitmapDataId])
bitmapDatas[bitmapDataId] = {url: url, imageWidth: imageWidth,
imageHeight: imageHeight, bitmapData: null};
return MyIcon;
}
public function MyIcon()
{
super();
if (!timer)
{
timer = new Timer(200);
timer.start();
}
timer.addEventListener(TimerEvent.TIMER, timerHandler);
addEventListener(Event.ADDED, addedHandler);
addEventListener(Event.REMOVED, removedHandler);
}
private var url:String = null;
private var imageWidth:Number = 16;
private var imageHeight:Number = 16;
private var bitmapDataId:String = null;
private var bitmapData:BitmapData = null;
override public function get measuredWidth():Number
{
return imageWidth;
}
override public function get measuredHeight():Number
{
return imageHeight;
}
private function requestImage():void
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
var request:URLRequest = new URLRequest(url);
loader.load(request);
}
override protected function updateDisplayList(w:Number, h:Number):void
{
graphics.clear();
if (isNaN(w) || isNaN(h))
return;
if (bitmapData)
{
graphics.beginBitmapFill(bitmapData, null, false, true);
graphics.drawRect(0, 0, w, h);
graphics.endFill();
return;
}
if (!showWaitIcon)
return;
drawWaitIcon(graphics, timer.currentCount % 8);
}
private function drawWaitIcon(g:Graphics, state:int):void
{
var color:uint = 0xE7E7E7;
var alpha:Number = 0.35;
var colors:Array = [0x6C7577, 0xC5CED0, 0xB9C1C3, 0xADB5B7,
0x9FA8AA, 0x929B9D, 0x868F90, 0x798284];
var alphas:Array = [0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7];
var newColors:Array = [0, 0, 0, 0, 0, 0, 0, 0];
var newAlphas:Array = [0, 0, 0, 0, 0, 0, 0, 0];
var positions:Array = [{x: 0, y: 6}, {x: 2, y: 2}, {x: 6, y: 0}, {x: 10, y: 2},
{x: 12, y: 6}, {x: 10, y: 10}, {x: 6, y: 12}, {x: 2, y: 10}];
var i:int = 0;
var index:int = 0;
var x:Number = 0;
var y:Number = 0;
for (i = 0; i < 8; i++)
{
index = i + state;
index = (index >= 8 ? index - 8 : index);
newColors[index] = colors[i];
newAlphas[index] = alphas[i];
}
for (i = 0; i < 8; i++)
{
x = positions[i].x;
y = positions[i].y;
g.beginFill(color, alpha);
g.drawRect(x, y, 4, 4);
g.endFill();
g.beginFill(newColors[i], newAlphas[i]);
g.drawRect(x, y + 1, 4, 2);
g.endFill();
g.beginFill(newColors[i], newAlphas[i]);
g.drawRect(x + 1, y, 2, 4);
g.endFill();
}
}
private function addedHandler(event:Event):void
{
var targetId:String = NameUtil.displayObjectToString(parent);
var bitmapDataInfo:Object = null;
var i:int = 0;
for (i = bitmapDataIdList.length - 1; i >= 0; i--)
{
if (bitmapDataIdList[i].targetId == targetId ||
bitmapDataIdList[i].targetId == null)
{
bitmapDataId = bitmapDataIdList[i].bitmapDataId
bitmapDataInfo = bitmapDatas[bitmapDataId];
bitmapDataIdList.splice(i, 1);
}
}
if (bitmapDataInfo)
{
url = bitmapDataInfo.url;
imageWidth = bitmapDataInfo.imageWidth;
imageHeight = bitmapDataInfo.imageHeight;
bitmapData = bitmapDataInfo.bitmapData;
width = imageWidth;
height = imageHeight;
if (!bitmapData && url)
requestImage();
}
}
private function removedHandler(event:Event):void
{
if (timer)
timer.removeEventListener(TimerEvent.TIMER, timerHandler);
}
private function timerHandler(event:TimerEvent):void
{
updateDisplayList(measuredWidth, measuredHeight);
}
private function completeHandler(event:Event):void
{
var loaderInfo:LoaderInfo = event.target as LoaderInfo;
if (loaderInfo.content)
{
bitmapData = (loaderInfo.content as Bitmap).bitmapData;
bitmapDatas[bitmapDataId].bitmapData = bitmapData;
if (parent)
(parent as UIComponent).validateNow();
updateDisplayList(measuredWidth, measuredHeight);
}
}
private function ioErrorHandler(event:IOErrorEvent):void
{
}
}
}
Test.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import controls.MyIcon;
import mx.core.UIComponent;
private function treeIconFunction(data:Object):Class
{
if (!data)
return null;
if (!data.@icon)
return null;
return MyIcon.getIconClass(tree.itemToItemRenderer(data) as UIComponent, data.@icon, 16, 16);
}
]]>
</mx:Script>
<mx:XMLList id="treeData">
<item label="node1" icon="http://c.csdn.net/bbs/t/5/i/search1.gif">
<item label="node2" icon="http://topic.csdn.net/u/ui/images/blank.gif" />
<item label="node3" icon="http://csdnimg.cn/bbs/m/i/yellow_1.gif" />
<item label="node4" icon="http://csdnimg.cn/www/images/dnspic_csdn.gif">
<item label="node5" icon="http://csdnimg.cn/www/images/csdnindex_pic1.gif" />
<item label="node6" icon="http://csdnimg.cn/www/images/csdnindex_pic2.gif" />
</item>
</item>
<item label="node7" icon="http://csdnimg.cn/bbs/m/i/red_2.gif" />
</mx:XMLList>
<mx:Tree id="tree" width="200" height="200" dataProvider="{treeData}"
labelField="@label" iconFunction="treeIconFunction" />
</mx:Application>
參照這篇文章寫的:
http://blog.chinaunix.net/u/21752/showart_2089230.html
相關文章
- vue3 動態載入el-icon圖示Vue
- jQuery 動態數字顯示jQuery
- alv動態顯示列
- 動態內表及動態ALV顯示(轉)
- iOS 動態更換iconiOS
- 靜態數碼管顯示、動態數碼管顯示、矩陣按鍵矩陣
- javaScript動態顯示當前時間JavaScript
- 數碼管顯示動態資料
- 動態顯示資料庫圖片資料庫
- 動態隱藏/顯示選擇螢幕
- javascript動態隱藏顯示技術(轉)JavaScript
- jqgrid動態顯示,隱藏指定列
- 動態修改應用icon和name
- vue中:is的用法,動態顯示需要的元件Vue元件
- 8位數碼管動態掃描顯示
- QT實現動態顯示系統時間QT
- JavaScript操作下拉框動態顯示內容JavaScript
- Android動態生成控制元件居中顯示Android控制元件
- 控制檯中動態顯示系統時間
- [Java Swing] 在mac中不顯示java的dock iconJavaMac
- Cell 動態行高文字顯示不全問題探索
- 在 Laravel 中動態 隱藏 / 顯示 API 欄位LaravelAPI
- javascript動態設定div的顯示和隱藏JavaScript
- js動態控制表單的顯示和隱藏JS
- Vue引入icon圖示Vue
- 【IOS】自己動手豐衣足食-圖示篇ICONiOS
- 收藏 不顯示刪除回覆顯示所有回覆顯示星級回覆顯示得分回覆 動態網站部署到伺服器出現了問題網站伺服器
- layui 輪播圖動態資料不顯示問題UI
- 動態的實現任意表的ALV顯示02
- 動態的實現任意表的ALV顯示01
- iOS神技之動態更換APP的Icon圖iOSAPP
- WIN10動態磁貼不顯示內容怎麼設定 WIN10設定動態磁貼不顯示內容方法Win10
- icon 圖示css實現CSS
- SAP ABAP 動態內表實現 ALV橫向按月份動態顯示資料
- elementui 如果變數為1 顯示 開啟,2顯示關閉。表格怎麼根據狀態碼顯示狀態值UI變數
- 動態顯示:文字框字數(輸入還剩多少字)
- 手把手教你完成數字動態變化顯示效果
- Element 利用Tooltip提示框實現動態顯示文字提示