Fix*_*int 1 apache-flex actionscript-3
我是Flex的新手,我正在尝试编写一个简单的应用程序.我有一个带图像的文件,我想在图形上显示这个图像.我该怎么做呢?我尝试[嵌入] - 并将其作为子项添加到拥有图形的组件中,但是我得到了"类型强制失败:无法转换为...到mx.core.IUIComponent"错误.
在我的脑海中,我可以想到两件可能对你有帮助的事情(取决于你想要实现的目标):
如果您只想显示已嵌入的图像,可以将图像组件添加到舞台,并将其值设置source为Class您要嵌入的图形资源():
[Bindable]
[Embed(source="assets/image.png")]
private var MyGfx:Class;
myImage.source = MyGfx;
Run Code Online (Sandbox Code Playgroud)
如果您确实想要将位图绘制到Graphics对象上,可以使用beginBitmapFill()方法执行此操作:
[Bindable]
[Embed(source="assets/image.png")]
private var MyGfx:Class;
var myBitmap:BitmapData = new MyGfx().bitmapData;
myGraphics.beginBitmapFill(myBitmap);
myGraphics.endFill();
Run Code Online (Sandbox Code Playgroud)
您可能会发现Adobe网站上的Flex Quick Starts文章很有用,尤其是"嵌入资源"部分.
接受的答案是不完整的,因为没有任何呼吁drawRect()将被绘制.我已经用这种方式实现了它并且它有效.请注意,我添加了矩阵转换,否则必须在0,0处绘制图像,否则会被错误地剪裁.
[Embed(source='assets/land-field.png')]
private var ImgField:Class;
private var field:BitmapData = new ImgField().bitmapData;
public static function drawImage(g:Graphics, image:BitmapData, x:int, y:int):void {
var mtx:Matrix = new Matrix();
mtx.translate(x, y);
g.beginBitmapFill(image, mtx, false, false);
g.drawRect(x, y, image.width, image.height);
g.endFill();
}
Run Code Online (Sandbox Code Playgroud)