您的位置:首页 > 其它

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

2016-11-20 15:17 281 查看
项目:影像插件(Flex版)
功能:在影像上添加年份文字


代码:
<?xml version="1.0" encoding="utf-8"?> 

<s:Group xmlns:fx="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)"> 

<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> 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐