在AS3中创建图像

时间:2011-03-18 18:17:21

标签: flash actionscript-3

是否可以在Flash AS3中创建包含特定背景颜色和用户输入的文本的图像?然后在运行时使用此图像?

// nicke

2 个答案:

答案 0 :(得分:1)

这里有一些伪代码可以帮助你绘制图形并创建它的位图对象。

w = 400;
h = 400;

var shape:Shape = new Shape();
shape.graphics.beginFill( 0xFF0000, 1 );
shape.graphics.drawRect( 0, 0, w, h );
shape.graphics.endFill();

var field:TextField = new TextField();
field.text = someUserInput;
shape.addChild( field );

var bitmap:Bitmap = new Bitmap();
var bitmapData:BitmapData = new BitmapData( w, h );
bitmapData.draw( shape );
bitmap.bitmapData = bitmapData;

addChild( bitmap );

从这里,您可以使用Adobe核心库来创建实际的jpeg或png文件:

var jpgEncoder:JPGEncoder = new JPGEncoder( quality );
var jpgStream:ByteArray = jpgEncoder.encode( bitmapData );

答案 1 :(得分:0)

不确定

function createTextBox
(
 width : Number,
 height : Number, 
 bgColor : uint, 
 text : String, 
 texFormat : TextFormat ) : Sprite
{
 var sprite : Sprite = new Sprite();
 var shape : Shape = new Shape();
 var textField : TextField = new TextField();

 //background
 shape.graphics.beginFill( bgColor );
 shape.graphics.drawRect( 0, 0, width, height );
 sprite.addChild( shape );

 //textfield
 textfield.width = width;
 textfield.height = height;
 textfield.multiline = true;
 textfield.wordWrap = true;

 //up to you if it serves your needs
 textfield.embedFonts = true;
 textField.selectable = false;

 textField.defaultTextFormat = textFormat;
 sprite.addChild( textField );

 return sprite;
}

var format : TextFormat = new TextFormat();
format.color = 0xFF0000;
format.size = 12;

var myText : String = "A woman is just a woman but a good cigar is a smoke!"

var sprite : Sprite = createTextBox( 200, 200, 0x999999, myText, format );
addChild( sprite );

//if you want, you can covert this to a bitmap:

var bitmapData : BitmapData = new BitmapData( 200, 200, false );
bitmapData.draw( sprite );

var bitmap : Bitmap = new Bitmap( bitmapData );
bitmap.x = 200;
addChild( bitmap );