思远(BonaShen)博客

朋友们,我有了新了家,就在http://www.bonashen.com

导航

FLEX-蒙板处理

同事在处理绘图时,总有些超出边界,flex可不管你超不超,照样show出来,而在window编程中有API
selectClipRng来处理,在flex中可以实现,示例如下:

in maskExample.mxml
 1 <?xml version="1.0"?>
 2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
 3      width="100%" height="100%">
 4     <mx:CheckBox id="cb1" label="enable mask" x="26" y="258"
selected
="false" click="enableMask(cb1.selected);"/>
 5     <mx:TextArea id="t1" x="26" y="58" width="352" height="192" wordWrap="true">
 6         <mx:text><![CDATA[
       省略
12 ]]></mx:text>
13     </mx:TextArea>
14     <mx:creationComplete>
15         <![CDATA[
16             //enableMask(cb1.selected);
17         ]]>
18     </mx:creationComplete>
19     <mx:Script>
20         <![CDATA[
21 
22             private function enableMask(mode:Boolean):void
23             {
24                 var mask:Sprite = t1.getChildByName("_masker") as Sprite;
25                 if(mode){
26                     if(!mask){
27                         mask = new Sprite();
28                         mask.name ="_masker";
29                         t1.addChild(mask);
30                         t1.mask = mask;
31                         
32                         var w:int = t1.width;
33                         var h:int = t1.height;
34                         var g:Graphics = mask.graphics;
35                         g.beginFill(0x000000);
36                         g.drawRect(20,20,w-10,h-10);
37                         g.endFill();
38                     }
39                 }else{
40                     if(mask){
41                         t1.mask = null;
42                         t1.removeChild(mask);
43                     }
44                 }
45             }
46         ]]>
47     </mx:Script>
48     <mx:Label x="26" y="21" text="蒙板示例" color="#D72626" width="112" fontSize="19"/>
49     
50 
51 </mx:Application>
52 
29行:蒙板也必须是目标对象的child.故采用addChild;
30行:就是将sprite设置为蒙板。


posted on 2007-09-11 14:40  Bona shen  阅读(2758)  评论(1编辑  收藏  举报