1. 程式人生 > >Flex 圖片和文字轉化為BitmapData資料

Flex 圖片和文字轉化為BitmapData資料

xmlns:s="library://ns.adobe.com/flex/spark" 
xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="group1_creationCompleteHandler(event)"> 
<fx:Script> 
<![CDATA[ 
import mx.binding.utils.ChangeWatcher; 
import mx.controls.Alert; 
import mx.controls.Image; 
import mx.core.IVisualElement; 
import mx.core.UIComponent; 
import mx.events.FlexEvent; 

[Bindable] 
public var sourceImg:String=""; 
[Bindable] 
public var sourceLabel:String=""; 

private var ld:Loader
 = new Loader(); 
protected function group1_creationCompleteHandler(event:FlexEvent):void 

ld.load(new URLRequest(sourceImg)); 
ld.contentLoaderInfo.addEventListener(Event.COMPLETE , loadComplete123); 
ChangeWatcher.watch(this,"sourceImg",changeImg); 

public function changeImg(event:Event):void 

ld.load(new URLRequest(sourceImg)); 


public var bitmap:Bitmap; 
public var bitmapdata:BitmapData;//圖片 
public var myBitmapData:BitmapData;//文字 
public var bmp:Bitmap; 
public var bmp1:Bitmap; 
public var content:UIComponent = new UIComponent();//師兄用的Canvas,我用了一下UIComponent替換;它們都在mx.core.Container下 
private function loadComplete123(event:Event):void 

try{ 
content.removeChild(bmp); 
content.removeChild(bmp1); 
}catch(e:Error){} 

var loaderInfo:LoaderInfo = event.target as LoaderInfo; 
bitmap = loaderInfo.content as Bitmap; 
if(bitmap) 
leftImg.visible=false; 
else 
leftImg.visible=true; 
bitmapdata = new BitmapData(bitmap.width, bitmap.height, true, 16777215); 
bitmapdata.draw
(bitmap.bitmapData, null, null, BlendMode.NORMAL, null, true); 
//轉化成圖片 
bmp = new Bitmap(bitmapdata); 
bmp.width=bcContent.width*0.99; 
bmp.height=bcContent.height*.98; 
content.addChild(bmp); 

//文字:年份 
var tf:TextField = new TextField(); 
tf.text = sourceLabel; 
tf.textColor=16711680; 
tf.border = false; 
tf.background = false; 
myBitmapData = new BitmapData(250,120 ,true,0x00000000); 
myBitmapData.draw
(tf); 
bmp1= new Bitmap(myBitmapData); 
bmp1.width=this.width; 
bmp1.height=this.height; 
content.addChild(bmp1); //文字後新增,否則會被圖片覆蓋

bcContent.addElement(content); 
bitmap.bitmapData.dispose(); 


]]> 
</fx:Script> 
<fx:Declarations> 
</fx:Declarations> 
<s:BorderContainer id="bcContent" backgroundImage="{sourceImg}" width="100%" height="100%" backgroundAlpha="0" borderVisible="false" horizontalCenter="0" verticalCenter="0"> 
<mx:Image id="leftImg" width="100%" height="100%" source="images/noImage.jpg" visible="false"/> 
</s:BorderContainer> 
</s:Group>