Flex 图片和文字转化为BitmapData数据

项目:影像插件(Flex版)
功能:在影像上添加年份文字

代码:
 
http://ns.adobe.com/mxml/2009
"
xmlns:s="library://ns.adobe.com/flex/spark" 
xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="group1_creationCompleteHandler(event)"> 
 
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(); 


]]> 
 
 
 
 
 
 
 

你可能感兴趣的:(Flex)