不幸的是,过滤器在 GPU 模式下不起作用(阴影、发光)。我正在寻找机会在这种模式下将这些效果用于文本。我会欢迎任何建议。
最佳答案
正如 Astraport 所提到的,每次使用 bitmapData.draw()
更新文本时,您都需要将文本字段绘制到位图数据中。 .
如果您使用 textField.getBounds
为了确定您需要的位图数据的大小,生成的边界矩形将不包括由于过滤器而产生的额外大小(例如,根据“距离”和“模糊”,DropShadowFilter 会以某些像素突出文本框的一侧)。为了确保在绘制位图时包含过滤器,您还需要使用 bitmapData.generateFilterRect()
以获得正确的尺寸矩形。
代码片段(未经测试,但总体思路):
// Remember the transform matrix of the text field
var offset : Matrix = myTextField.transform.matrix.clone();
// Get the bounds of just the textfield (does not include filters)
var tfBounds : Rectangle = myTextField.getBounds( myTextField.parent );
// Create a bitmapData that is used just to calculate the size of the filters
var tempBD : BitmpaData = new BitmapData( Math.ceil(tfBounds.width), Math.ceil(tfBounds.height) );
// Make a copy of the textField bounds. We'll adjust this with the filters
var finalBounds : rectangle = tfBounds.clone();
// Step through each filter in the textField and adjust our bounds to include them all
var filterBounds : rectangle;
for each (var filter : BitmapFilter in myTextField.filters) {
filterBounds = tempBD.generateFilterRect( tfBounds, filter );
finalBounds.left = Math.min( finalBounds.left, filterBounds.left );
finalBounds.right = Math.max( finalBounds.right, filterBounds.right );
finalBounds.top = Math.min( finalBounds.top, filterBounds.top );
finalBounds.bottom = Math.max( finalBounds.bottom, filterBounds.bottom );
}
// Now draw the textfield to a new bitmpaData
var textFieldBD : BitmpaData = new BitmapData( Math.ceil(finalBounds.width), math.ceil(finalBounds.height) );
offset.tx = -finalBounds.x;
offset.ty = -finalBounds.y;
textFieldBD.draw( myTextField.parent, offset, myTextField.transform.colorTransform );
// Create a bitmap and add the bitmap data. Note: normally you would create a
// bitmap once and just update the bitmpaData
var bitmap : Bitmap = new Bitmap();
myTextField.parent.addChild( bitmap );
// Position the bitmap in same place as textField
bitmap.bitmapData = textFieldBD;
bitmap.x = myTextField.x - finalBounds.x;
bitmap.y = myTextField.y - finalBounds.y;
myTextField.visible = false;
关于actionscript-3 - 在 GPU 模式下对文本使用过滤器 AIR mobile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12407265/