flex如何绘制图像

时间:2012-03-29 06:53:00

标签: actionscript-3 flex flex4

我想要实现一些简单的事情(我想!)但不是。我想画一幅图像。我创建了一个新的组件DrawingImg,它扩展了s:Image

public class DrawingImg extends Image

{
    private var isDrawing:Boolean = false;
    private var x1:int;
    private var y1:int;
    private var x2:int;
    private var y2:int;

    public var drawColor:uint = 0x000000;

    public function DrawingImg()
    {
        super();

        addEventListener(FlexEvent.CREATION_COMPLETE, function(event:FlexEvent):void {
            erase();
        });

        addEventListener(MouseEvent.MOUSE_DOWN, function(event:MouseEvent):void {
            x1 = mouseX;
            y1 = mouseY;
            isDrawing = true;
        });

        addEventListener(MouseEvent.MOUSE_MOVE, function(event:MouseEvent):void {
            if (!event.buttonDown){
                isDrawing = false;
            }
            x2 = mouseX;
            y2 = mouseY;
            if (isDrawing){
                graphics.lineStyle(2, drawColor);
                graphics.moveTo(x1, y1);
                graphics.lineTo(x2, y2);
                x1 = x2;
                y1 = y2;
            }
        });

        addEventListener(MouseEvent.MOUSE_UP, function(event:MouseEvent):void {
            isDrawing = false;
        });
    }

    public function erase():void  {
        graphics.beginFill(0xffffff, 0.00001);
        graphics.drawRect(0, 0, width, height);
        graphics.endFill();
    }
}
}

这是我的观点:

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="GiftCertificateView" 
actionBarVisible="false" xmlns:local="*">
<local:DrawingImg source="/assets/dessin.png"  width="100%" height="100%"/>
</s:View>

工作“接近”,当我的鼠标不在图像中时,我可以绘制我的线条,但是当我在图像上方时,什么都没有出现? 有人有想法吗?

1 个答案:

答案 0 :(得分:0)

问题是您无法使用图形API绘制Spark Image。我最近没看过来源,所以我无法确切地告诉你原因。

快速解决方案是让您的DrawingImg课程延长UIComponent而不是Image

public class DrawingImage extends UIComponent

现在将绘图组件分层放在视图中的图像顶部:

<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"xmlns:s="library://ns.adobe.com/flex/spark" title="GiftCertificateView" actionBarVisible="false" xmlns:local="*">
    <s:Image source="@Embed('Grand Prismatic Spring.jpg')" width="100%" height="100%" />
    <local:DrawingImg width="100%" height="100%"/>
</s:View>