国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > WEB開發(fā) > 正文

7.9.為渲染器設(shè)置高效圖像

2024-04-27 13:52:01
字體:
供稿:網(wǎng)友
7.9.1.問題
你需要在通過data 把圖片傳入itemRenderer 里并顯示圖片。
7.9.2.解決辦法
創(chuàng)建一個新的renderer 類,并且利用commitProperties 方法和owner 屬性可以完成這些工作。我們可以在renderer 擁有者所在的文件里定義一個方法,用于返回我們要圖示的圖片。
7.9.3.討論
List 和ListBase 的設(shè)計者Alex Harui,在自己的blog 里寫到:“你可以把Image 標(biāo)簽放到Canvas 里,但這不是一個最好的方式。”為什么?因?yàn)镕lex 框架為了簡化我的開發(fā),通過大量很復(fù)雜的操作來計算組件的大小,并且在刷新子組件和刷新自己時,都會再做這些復(fù)雜的操作。也就是說,我們可以多做一些事情,從而減輕框架的工作,這樣就可以為FlashPlayer 減少很多不必要的負(fù)擔(dān)。

本例中的renderer 實(shí)現(xiàn)了IListItemRenderer 和IDropInListItemRenderer 接口。因?yàn)檫@個組件繼承了UIComponent,我們需要重寫measure 方法這樣我們就可以指定組件的明確大小。你需要通過實(shí)現(xiàn)measure,測量出image 的大小,并賦值給組件的measuredWidth和measuredHeight。

還有另一種方式,renderer 通過訪問它的父組件,從而得到Image class,值得注意的是這種方式中,image 并沒有傳到renderer 中。在我們的例子中,renderer 通過data 訪問父組件,從而得到內(nèi)嵌image 的引用。
+展開
-ActionScript
if(listData) {
// remove the old child if we have one
if(img) {
removeChild(img);
}
if(_imgClass == null) {
var _imgClass:Class =UIComponent(owner).document[listData.label];
}
img = new _imgClass();
addChild(img);
}

訪問父組件可以通過owner 的document,然后結(jié)合listData 確定要訪問父組件的,哪個方法或?qū)傩浴_@樣,我們即可以用傳入的image 也可以直接去父組件里取image。下面是renderer 的代碼:
+展開
-ActionScript
package oreilly.cookbook {
import flash.display.DisplayObject;
import mx.events.FlexEvent;
import mx.controls.listClasses.BaseListData;
import mx.controls.listClasses.IDropInListItemRenderer;
import mx.controls.listClasses.IListItemRenderer;
import mx.core.UIComponent;
public class SevenSixRenderer extends UIComponent implements IDropInListItemRenderer, IListItemRenderer {
private var _data:Object;
private var img:DisplayObject;
private var _listData:BaseListData;
private var _imgClass:Class;
[Bindable("dataChange")]
public function get data():Object {return _data;}
public function set data(value:Object):void {
_data = value;
if(_data.imgClass != null) {
_imgClass =_data.imgClass;
}
invalidateProperties(); // invalidate properties so that we'recertainthatthey'll be updated.
dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
}
[Bindable("dataChange")]
public function get listData():BaseListData { return _listData; }
public function set listData(value:BaseListData):void { _listData = value; }
override protected function commitProperties():void {
super.commitProperties();
// sometimes the listdata of the renderer can be null, in which case we
//certainly don't
// want to throw runtime errors
if (listData) {
// remove the old child if we have one
if (img) {
removeChild(img);
}
if (_imgClass == null ) {
var _imgClass:Class = UIComponent(owner).document[ listData.label];
}
img = new _imgClass();
addChild(img);
}
}
/* create the image instance now that we know what it is */
override protected function measure():void {
super.measure();
if (img) {
measuredHeight = img.height;
measuredWidth = img.width;
}
}
/* make sure the image is positioned correctly */
override protected function updateDisplayList(w:Number,h:Number):void{
super.updateDisplayList(w, h);
if (img) { img.x = (w - img.width) / 2;
}
}
}
}

在上邊的代碼中我們重寫了commitProperties,如果傳入的Image 為null,我們就利用owner 去調(diào)用DataGridColumn 的labelFunction 方法,得到一個默認(rèn)的Image,如果傳入的Image 不為null,則直接利用傳入的Image。
+展開
-XML
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxmlwidth="700"
height="300">

<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Embed(source="../assets/foo.jpeg")]
private var img:Class;
[Embed(source="../assets/bar.jpeg")]
public var img2:Class;
//just a generic collection to store some plain old info
[Bindable]
private var genericCollectionOne:ArrayCollection = new ArrayCollection([{name:"josh noble"
, age:30, appearance:"Somewhat wild"},{name:"Abey George", age:32, appearance:"Pretty tight", imgClass:img},{name:"Todd Anderson", age:31,appearance:"Intimidating"},{name:"Ryan
Taylor", age:25, appearance:"Boyishly Handsome", imgClass:img},{name:"Steve Weiss", age:36, appearance:"George Clooney-ish"}]);
// for our itemRenderer we use the call into this method if the imgClass
//property is null
private function getImage(o:Object, c:DataGridColumn):String { return "img2";}

]]>
</mx:Script>
<mx:DataGrid dataProvider="{genericCollectionOne}">
<mx:columns>
<mx:DataGridColumn dataField="age"/>
<mx:DataGridColumn dataField="name"/>
<mx:DataGridColumn dataField="appearance"/>
<mx:DataGridColumn
itemRenderer="oreilly.cookbook.SevenSixRenderer"
labelFunction="getImage dataField="imgClass"/>

</mx:columns>
</mx:DataGrid>
</mx:HBox>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 鄄城县| 鄱阳县| 丰都县| 新营市| 探索| 志丹县| 青铜峡市| 奉贤区| 沅江市| 颍上县| 邵东县| 大方县| 全南县| 会昌县| 阳高县| 莲花县| 南宫市| 井研县| 高密市| 德惠市| 年辖:市辖区| 望谟县| 阿勒泰市| 滕州市| 惠水县| 玛多县| 松原市| 青浦区| 巴塘县| 前郭尔| 新闻| 潍坊市| 鹤峰县| 长宁县| 东阳市| 沙雅县| 呼和浩特市| 新平| 乐清市| 黔西| 淳安县|