简体   繁体   English

如何在Flex 3中的图形上放置图像(例如PNG)?

[英]How to put an image (say, PNG) on a graphics in Flex 3?

I'm new to Flex, and I'm trying to write a simple application. 我是Flex的新手,我正在尝试编写一个简单的应用程序。 I have a file with an image and I want to display this image on a Graphics. 我有一个带有图像的文件,我想将此图像显示在图形上。 How do I do this? 我该怎么做呢? I tried [Embed]-ding it and adding as a child to the component owning the Graphics', but I'm getting a "Type Coercion failed: cannot convert ... to mx.core.IUIComponent" error. 我尝试将其嵌入[Embed],然后将其作为子级添加到拥有Graphics的组件中,但是出现“ Type Coercion失败:无法转换为...到mx.core.IUIComponent”错误。

Off the top of my head I can think of two things that might help you (depending on what it is exactly that you're trying to achieve): 我脑海中浮现出两点可能对您有帮助的事情(取决于您要实现的目标是什么):

If you just want to display an image you've embedded, you can add an Image component to the stage and set the value of its source as the graphical asset ( Class ) that you're embedding: 如果只想显示已嵌入的图像,则可以在舞台上添加一个Image组件,并将其source的值设置为要嵌入的图形资产( Class ):

[Bindable]
[Embed(source="assets/image.png")]
private var MyGfx:Class;

myImage.source = MyGfx;

If you actually want to draw a bitmap onto a Graphics object, you can do this with the beginBitmapFill() method: 如果您确实想在Graphics对象上绘制位图,则可以使用beginBitmapFill()方法执行此操作:

[Bindable]
[Embed(source="assets/image.png")]
private var MyGfx:Class;

var myBitmap:BitmapData = new MyGfx().bitmapData;
myGraphics.beginBitmapFill(myBitmap);
myGraphics.endFill();

You might find the Flex Quick Starts articles on Adobe's site useful, especially the "Embedding Assets" section. 您可能会发现Adobe网站上的“ Flex快速入门”文章很有用,尤其是“嵌入资产”部分。

The accepted answer is incomplete because without a call to drawRect() nothing will be drawn. 接受的答案是不完整的,因为如果不调用drawRect()则不会绘制任何内容。 I have implemented it this way and it works. 我已经以这种方式实现了它,并且可以正常工作。 Notice that I added a matrix translation, otherwise images would have to be drawn either at 0,0 or would be clipped incorrectly. 请注意,我添加了矩阵转换,否则图像必须以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();
}

You can also add a regular AS3 displayobject to a Flex component by adding it to the rawChildren collection (myComponent.rawChildren.addChild(myPNG)), but that's a bit of a hack. 您也可以通过将常规AS3显示对象添加到Flex组件中,方法是将其添加到rawChildren集合(myComponent.rawChildren.addChild(myPNG))中,但这有点麻烦。

If you're trying to do this via myDisplayObject.graphics, please post some sample source. 如果您尝试通过myDisplayObject.graphics执行此操作,请发布一些示例源。

Since you're a beginner, here's an easy way. 由于您是初学者,因此这是一种简单的方法。

In your application, switch to Design View. 在您的应用程序中,切换到“设计视图”。

Drag an Image Component from the Component Panel onto the main Application Canvas. 将图像组件从“组件面板”拖到主“应用程序画布”上。

Size the Image on the Canvas and leave it selected. 在“画布”上调整图像大小并保持选中状态。 On the right hand side, in the properties panel, click the Folder button on the "Source" field. 在右侧的属性面板中,单击“源”字段上的“文件夹”按钮。

Choose your image. 选择您的图片。 If you place the image in your project's folder, it will be included in your build folder. 如果将图像放置在项目的文件夹中,它将包含在构建文件夹中。

Then take a look at the source view. 然后看一下源视图。 You will see MXML code that reflects what you've done. 您将看到反映您已完成工作的MXML代码。 You can change this by hand as well. 您也可以手动更改。 The source="blah.png" part can also be pointed to a remote URL. source =“ blah.png”部分也可以指向远程URL。

好的,还有另一种方法可以遵循http://livedocs.adobe.com/flex/3/langref/flash/display/Graphics.html#includeExamplesSummary并使用beginBitmapFill()方法查看示例,非常有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM