开发者

Which event to use to draw a grid inside a Flex UI Component

开发者 https://www.devze.com 2023-02-24 06:47 出处:网络
I am in need to provide a Snap To Grid functionality inside a BorderContainer. All the functionality is done but I am stuck with one 开发者_如何转开发issue which is drawing the grid.

I am in need to provide a Snap To Grid functionality inside a BorderContainer. All the functionality is done but I am stuck with one 开发者_如何转开发issue which is drawing the grid.

  1. Tried using the Creation_Complete event, works fine but as soon as a child is added, the grid vanishes.

  2. Overrode the updateDisplayList - This was the best option so far (everything works fine), but the issue is whenever I move (or rather when moving) a control inside (I use ObjectHandles library) the updateDisplayList method is called which redraws the grid -> makes the moving process VERY laggy.

This is how I fill the grid.

numRows = Math.ceil(this.unscaledHeight/gridSize);
numCols = Math.ceil(this.unscaledWidth/gridSize);

this.graphics.beginFill(0x000000, 1.0);

for (var i:int= 0; i < numRows; i++)
{               
    for (var j:int = 0; j < numCols; j++)
    {       

        this.graphics.drawCircle(j * gridSize, i * gridSize, 1);
    }
}

this.graphics.endFill();

I only want to draw it once, and draw it only when the container is resized. Is there any event that will help me achieve this? Or is there any efficient method of that will help me draw a grid? Any comments and suggestions are welcome. Thanx a lot in advance :)


updateDisplayList() is the method which has unscaledWidth and unscaledHeight as parameters.

So you can avoid unnecessary redrawing the following way:

private var previousUnscaledWidth:Number;
private var previousUnscaledHeight:Number;

override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
    …
    if (unscaledWidth != previousUnscaledWidth || unscaledHeight != previousUnscaledHeight)
    {
        // Your drawing routine here

        previousUnscaledWidth = unscaledWidth;
        previousUnscaledHeight = unscaledHeight;
    }
}

You can place this code inside your custom ActionScript component which has UIComponent as base class. And place this grid inside BorderContainer:

<s:BorderContainer>
    <MyGrig left="0" right="0" top="0" bottom="0" />
    <!-- Another container's content here -->
</s:BorderContainer>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号