+展开-ActionScript
char_bounds = addCharacters.getTextField().getCharBoundaries(i);
bitmapData=new BitmapData(char_bounds.width, char_bounds.height, true, 0);
matrix = new Matrix(1, 0, 0, 1, -char_bounds.x, char_bounds.y);
bitmapData.draw(addCharacters.getTextField(), matrix, null, null, null, true);
bitmap = new Bitmap(bitmapData);
+展开-ActionScript
BitmapData(width:Number, height:Number, transparency:boolean, fillColor:Number);
+展开-XML
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="600"
height="300" xmlns:cookbook="oreilly.cookbook.*">
<mx:Script>
<![CDATA[
import flash.display.Sprite;
import mx.core.UIComponent;
import flash.text.TextField;
import flash.text.TextFormat;
private var characterArray:Array = new Array();
//set up our master character index we're going to use when animating the characters
private var charIndex:int = 0;
//keep track of the final position we're going to place all the characters in
private var finalX:Number = 0;
private function addNewCharacters():void
{
render();
invalidateDisplayList();
invalidateProperties();
}
public function render():void
{
//define all the variables that we'll need
var bitmapData:BitmapData;
var bitmap:Bitmap;
var char_bounds:Rectangle;
var matrix:Matrix;
var component:UIComponent;
//get the text format and set the
//tf.defaultTextFormat = addCharacters.getTextField().defaultTextFormat;
//tf.text = addCharacters.text;
for each(component in characterArray)
{
holder.removeChild(component);
}
characterArray.length = 0;
for(var i:int=0; i<addCharacters.text.length; i++)
{
char_bounds =
addCharacters.getTextField().getCharBoundaries(i);
bitmapData=new
BitmapData(char_bounds.width,char_bounds.height,
true,0);
matrix = new Matrix(1,0,0,1,-char_bounds.x,char_bounds.y);
bitmapData.draw(addCharacters.getTextField(),
matrix, null, null,null, true);
bitmap = new Bitmap(bitmapData);
component = new UIComponent();
component.width = bitmapData.width;
component.height = bitmapData.height;
component.addChild(bitmap);
holder.addChild(component);
component.x=char_bounds.x;
characterArray[i] = component;
}
holder.invalidateDisplayList();
startEffect();
}
private function startEffect():void
{
addEventListener(Event.ENTER_FRAME, moveCharacter);
}
private function moveCharacter(event:Event):void
{
var comp:UIComponent = (characterArray[charIndex] as
UIComponent);
if(comp)
{
if(comp.x < 200 - finalX)
{
(characterArray[charIndex] as Sprite).x+=2;
}
else
{
if(charIndex == characterArray.length - 1)
{
removeEventListener(Event.ENTER_FRAME,
moveCharacter);
return;
}
finalX += comp.width+2;
charIndex++;
}
}
}
]]>
</mx:Script>
<mx:HBox>
<cookbook:AccessibleTextInput id="addCharacters"
fontSize="18"/>
<mx:Button label="add characters"
click="addNewCharacters()"/>
</mx:HBox>
<mx:Canvas id="holder" y="200"/>
</mx:Canvas>