国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > WEB開發(fā) > 正文

4.10.把字符當(dāng)作獨(dú)立圖像處理

2024-04-27 13:51:54
字體:
供稿:網(wǎng)友
4.10.1. 問題
我想把獨(dú)立的字符當(dāng)作圖像一樣進(jìn)行特效處理。
4.10.2. 解決辦法
使getCharBoundaries 方法來取得TextField 中字符的實(shí)際寬高xy 值。然后從TextField 創(chuàng)建一個(gè)包含你想要的字符的位圖。
4.10.3. 討論
getCharBoundaries 返回一個(gè)形容TextField 中字符索引的寬高xy 值的矩形。你可以用這個(gè)信息創(chuàng)建一個(gè)字符的位圖,保留它的全部可視信息,并使這些位圖看上去很真實(shí)。下面這段代碼就是處理的關(guān)鍵:
+展開
-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, nullnullnulltrue);
bitmap = new Bitmap(bitmapData);

char_bounds 對象是一個(gè)將要儲(chǔ)存所有這個(gè)字符相關(guān)信息的矩形。這個(gè)信息用來在創(chuàng)建位圖時(shí)創(chuàng)建一個(gè)flash.display.BitmapData 對象以便顯示在位圖內(nèi)。BitmapData 的構(gòu)造器接收4個(gè)參數(shù):
+展開
-ActionScript
BitmapData(width:Number, height:Number, transparency:boolean, fillColor:Number);

現(xiàn)在你擁有了這個(gè)位圖對象,創(chuàng)建一個(gè)Matrix 來儲(chǔ)存那個(gè)你想捕捉的TextField 中特殊區(qū)域的信息,也就是,getCharBoundaries 方法返回的綁定了TextField 的矩形區(qū)域。這個(gè)Matrix 被傳給BitmapData draw 方法,這個(gè)方法同時(shí)也從載入的DisplayObject 中提取像素?cái)?shù)據(jù)。當(dāng)你畫完實(shí)際的位圖數(shù)據(jù)后,通過使用最新最流行的BitmapData 對象,你就可以創(chuàng)建一個(gè)Bitmap 對象,這個(gè)對象也同時(shí)是一個(gè)DisplayObject,可以被添加到顯示列表中。

剩下的例子就是處理TextField 中的字符循環(huán)了,對每一個(gè)字符都執(zhí)行前面的操作,然后一個(gè)又一個(gè)栩栩如生的位圖對象就被創(chuàng)建了:
+展開
-XML
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxmlwidth="600"
height="300xmlns: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, nullnull,nulltrue);
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="holdery="200"/>
</mx:Canvas>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 巫溪县| 翁牛特旗| 高州市| 千阳县| 莫力| 井冈山市| 林口县| 大港区| 长顺县| 门源| 那曲县| 新兴县| 乐亭县| 玉龙| 红河县| 科技| 车致| 吐鲁番市| 吉隆县| 房产| 巴彦县| 繁峙县| 达日县| 西乡县| 弋阳县| 留坝县| 巢湖市| 高阳县| 颍上县| 图片| 台北县| 栾城县| 廊坊市| 玉树县| 宜良县| 淮北市| 民乐县| 富裕县| 泗水县| 叶城县| 恩施市|