FLEX tree icon 急。在线等待

huyao_jackey 2009-05-15 12:54:56
我要给tree的每个item定义我自己的icon。因为图片很多,还想动态的加载。但是我得不到表示这个图片的Class。请问有什么办法吗?
...全文
1469 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
jin20000 2010-08-28
学习,
回复
DAVID_DEEYE 2010-08-26
哇, 好负责!!
学习。。。。
回复
ThornBird313 2009-12-02
仿照IconUtility写的,楼主试一下吧:


MyIcon.as:

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>
回复
wanggh_work 2009-12-01
LZ试试
package components.renderers
{
import mx.controls.treeClasses.TreeItemRenderer;
import mx.controls.treeClasses.TreeListData;

public class myTreeRender extends TreeItemRenderer
{
[Embed(source="icon/close_b_p.png")]
private var openedTreeNodePic:Class;

public function myTreeRender()
{
super();
}

override public function set data(value:Object):void {
super.data = value;
this.setStyle("disclosureNullIcon", openedTreeNodePic);
TreeListData(super.listData).icon = this.getStyle("disclosureNullIcon");
}
}
}
回复
zhuegg 2009-07-30
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="InitApp()">
<mx:Script>
<![CDATA[

//第一种方式 这种方式编译以后1.jpg 会直接编译进swf文件中 所以swf可以独立存在
[Bindable]
[Embed(source="1.jpg")]
private var imgClass:Class;

//第2种方式
private var _loader:Loader;

private function InitApp():void{

//第一种方式的代码
_img.source = imgClass;

//第二种方式的代码
_loader = new Loader();
//这里需要注意的是 不是_loader.addEventListener 这样是没有效果的
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event):void{
_img.source = e.currentTarget.content;
});
//这里说一个技巧 当url 中有中文字体的话 使用encodeURI方法 如果没有 则可以不加
_loader.load(new URLRequest(encodeURI("1.jpg")));


//第三种方式比较简单
_img.source = "1.jpg"; //注意这里必须设置img autoLoad属性为true

//最后说明 其中第2 第3种方式中swf都不能独立存在 必须配合1.jpg文件的存在 而第一种方式则不需要
}
]]>
</mx:Script>
<mx:Image x="51" y="62" width="298" height="245" autoLoad="true" id="_img"/>
</mx:Application>
回复
cjd007 2009-07-29
我用了IconUtility 不行啊。
IconUtility.getClass(tree,"http://www.google.cn/intl/zh-CN/images/logo_cn.gif",16,16);
能給个例子吗?
回复
herowach 2009-05-20
你就是不想将图片Embed进SWF吧,想运行时载入图片,是这样吗?在网上搜一下IconUtility.swc这个包吧,可以直接将一个图片路径转成一个CLASS,我曾经也试过很多方法都不行,这个包也是无意看见的,反正能用就行
回复
huyao_jackey 2009-05-15
package
{
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
import flash.display.DisplayObject;
import mx.controls.treeClasses.TreeItemRenderer;
import mx.core.BitmapAsset;

public class myTreeRender extends TreeItemRenderer
{
public function myTreeRender()
{
super();
}

override protected function commitProperties():void
{
super.commitProperties();
if (icon)
{
removeChild(DisplayObject(icon));
icon = null;
}
var iocurl:String=super.data.@ioc;
if(iocurl&&iocurl!=''){
var p:Loader=new Loader();
var url:URLRequest=new URLRequest(encodeURI(iocurl));
p.load(url);

p.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandle);
}

}

private function completeHandle(event:Event):void{
var icon:BitmapAsset=new BitmapAsset(event.target.content.bitmapData);
super.icon=icon;
this.addChild(DisplayObject(super.icon));
invalidateDisplayList();
}
}


}
我扩展了TreeItemRenderer类。但tree显示的时候就出了问题。
回复
相关推荐
发帖
Flex
创建于2007-12-13

4328

社区成员

多媒体/设计 Flex
申请成为版主
帖子事件
创建了帖子
2009-05-15 12:54
社区公告
暂无公告