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

首頁 > 網站 > WEB開發 > 正文

7.9.為渲染器設置高效圖像

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

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

還有另一種方式,renderer 通過訪問它的父組件,從而得到Image class,值得注意的是這種方式中,image 并沒有傳到renderer 中。在我們的例子中,renderer 通過data 訪問父組件,從而得到內嵌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,然后結合listData 確定要訪問父組件的,哪個方法或屬性。這樣,我們即可以用傳入的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 去調用DataGridColumn 的labelFunction 方法,得到一個默認的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>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 横山县| 庄浪县| 琼中| 长海县| 阿拉善右旗| 许昌市| 滦平县| 陵水| 襄垣县| 永仁县| 谢通门县| 中西区| 澄江县| 抚宁县| 阜南县| 沂源县| 淮安市| 安达市| 淮南市| 霍州市| 纳雍县| 晋江市| 边坝县| 福安市| 阳信县| 香港 | 平遥县| 邹城市| 赤城县| 文山县| 监利县| 天等县| 常熟市| 吴旗县| 晋州市| 高密市| 清水河县| 中超| 岳阳县| 闽清县| 盐津县|