3

我有一个带有 Sprite 符号的 Flash 库,该符号由其他带有设计时应用过滤器的精灵组成。我将这些符号嵌入到 Flex 应用程序中,如下所示:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script>
        <![CDATA[
            [Bindable]
            [Embed(source="Resources.swf", symbol="SquareContainer")]
            private var squareContainer_class:Class;

            private function log(msg:String):void {
                    output.text = output.text + "\n" + msg;
            }
        ]]>
    </mx:Script>

    <mx:VBox horizontalAlign="center" width="100%" height="100%" >
        <mx:Image id="squareContainer" source="{squareContainer_class}"/>
        <mx:Button click="log(squareContainer.width + ', ' + squareContainer.height);"/>
        <mx:TextArea id="output" width="100%" height="100%" />
    </mx:VBox>

</mx:Application>

在本例中,SquareContainer 符号为 100px 宽 x 100px 高;但是它包含一个带有发光和模糊滤镜的子精灵,这会导致精灵看起来明显大于 100x100。由于我无法确定容器的组成,因此我无法使用 BitmapData.generateFilterRect() 来获取应用于嵌套精灵的过滤器。

我怎样才能得到精灵的大小加上它的过滤器?

4

4 回答 4

4

哦,甜蜜的成功!(感谢您的提示)一位朋友通过一个很好的递归函数帮助解决了这个问题,以处理嵌套精灵上可能存在的过滤器:

private function getDisplayObjectRectangle(container:DisplayObjectContainer, processFilters:Boolean):Rectangle {
    var final_rectangle:Rectangle = processDisplayObjectContainer(container, processFilters);

    // translate to local
    var local_point:Point = container.globalToLocal(new Point(final_rectangle.x, final_rectangle.y));
    final_rectangle = new Rectangle(local_point.x, local_point.y, final_rectangle.width, final_rectangle.height);       

    return final_rectangle;
}

private function processDisplayObjectContainer(container:DisplayObjectContainer, processFilters:Boolean):Rectangle {
    var result_rectangle:Rectangle = null;

    // Process if container exists
    if (container != null) {
        var index:int = 0;
        var displayObject:DisplayObject;

        // Process each child DisplayObject
        for(var childIndex:int = 0; childIndex < container.numChildren; childIndex++){
            displayObject = container.getChildAt(childIndex);

            //If we are recursing all children, we also get the rectangle of children within these children.
            if (displayObject is DisplayObjectContainer) {

                // Let's drill into the structure till we find the deepest DisplayObject
                var displayObject_rectangle:Rectangle = processDisplayObjectContainer(displayObject as DisplayObjectContainer, processFilters);

                // Now, stepping out, uniting the result creates a rectangle that surrounds siblings
                if (result_rectangle == null) { 
                    result_rectangle = displayObject_rectangle.clone(); 
                } else {
                    result_rectangle = result_rectangle.union(displayObject_rectangle);
                }                       
            }                       
        }

        // Get bounds of current container, at this point we're stepping out of the nested DisplayObjects
        var container_rectangle:Rectangle = container.getBounds(container.stage);

        if (result_rectangle == null) { 
            result_rectangle = container_rectangle.clone(); 
        } else {
            result_rectangle = result_rectangle.union(container_rectangle);
        }


        // Include all filters if requested and they exist
        if ((processFilters == true) && (container.filters.length > 0)) {
            var filterGenerater_rectangle:Rectangle = new Rectangle(0,0,result_rectangle.width, result_rectangle.height);
            var bmd:BitmapData = new BitmapData(result_rectangle.width, result_rectangle.height, true, 0x00000000);

            var filter_minimumX:Number = 0;
            var filter_minimumY:Number = 0;

            var filtersLength:int = container.filters.length;
            for (var filtersIndex:int = 0; filtersIndex < filtersLength; filtersIndex++) {                      
                var filter:BitmapFilter = container.filters[filtersIndex];

                var filter_rectangle:Rectangle = bmd.generateFilterRect(filterGenerater_rectangle, filter);

                filter_minimumX = filter_minimumX + filter_rectangle.x;
                filter_minimumY = filter_minimumY + filter_rectangle.y;

                filterGenerater_rectangle = filter_rectangle.clone();
                filterGenerater_rectangle.x = 0;
                filterGenerater_rectangle.y = 0;

                bmd = new BitmapData(filterGenerater_rectangle.width, filterGenerater_rectangle.height, true, 0x00000000);                      
            }

            // Reposition filter_rectangle back to global coordinates
            filter_rectangle.x = result_rectangle.x + filter_minimumX;
            filter_rectangle.y = result_rectangle.y + filter_minimumY;

            result_rectangle = filter_rectangle.clone();
        }               
    } else {
        throw new Error("No displayobject was passed as an argument");
    }

    return result_rectangle;
}
于 2009-01-29T16:30:26.807 回答
1

这里有一个稍微不同的方法:只需将整个对象绘制进去BitmapData,然后计算位图不透明区域的边界。这种方法可能更高效,尤其是在复杂对象上。

package lup.utils
{
    import flash.display.BitmapData;
    import flash.display.DisplayObject;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;

    public class BoundsHelper
    {
        private var _hBmd:BitmapData;
        private var _hBmdRect:Rectangle;
        private var _hMtr:Matrix;
        private var _hPoint:Point;

        private var _xMin:Number;
        private var _xMax:Number;
        private var _yMin:Number;
        private var _yMax:Number;

        /**
         * Specify maxFilteredObjWidth and maxFilteredObjHeight to match the maximum possible size
         * of a filtered object. Performance of the helper is inversely proportional to the product
         * of these values.
         *
         * @param maxFilteredObjWidth Maximum width of a filtered object.
         * @param maxFilteredObjHeight Maximum height of a filtered object.
         */
        public function BoundsHelper(maxFilteredObjWidth:Number = 500, maxFilteredObjHeight:Number = 500) {
            _hMtr = new Matrix();
            _hPoint = new Point();
            _hBmd = new BitmapData(maxFilteredObjWidth, maxFilteredObjHeight, true, 0);
            _hBmdRect = new Rectangle(0, 0, maxFilteredObjWidth, maxFilteredObjHeight);
        }

        /**
         * Calculates the boundary rectangle of an object relative to the given coordinate space.
         *
         * @param obj The object which bounds are to be determined.
         *
         * @param space The coordinate space relative to which the bounds should be represented.
         *        If you pass null or the object itself, then the bounds will be represented
         *        relative to the (untransformed) object.
         *
         * @param dst Destination rectangle to store the result in. If you pass null,
         *        new rectangle will be created and returned. Otherwise, the passed
         *        rectangle will be updated and returned.
         */
        public function getRealBounds(obj:DisplayObject, space:DisplayObject = null, dst:Rectangle = null):Rectangle {
            var tx:Number = (_hBmdRect.width  - obj.width ) / 2,
                ty:Number = (_hBmdRect.height - obj.height) / 2;

            // get transformation matrix that translates the object to the center of the bitmap
            _hMtr.identity();
            _hMtr.translate(tx, ty);

            // clear the bitmap so it will contain only pixels with zero alpha channel
            _hBmd.fillRect(_hBmdRect, 0);
            // draw the object; it will be drawn untransformed, except for translation
            // caused by _hMtr matrix
            _hBmd.draw(obj, _hMtr);

            // get the area which encloses all pixels with nonzero alpha channel (i.e. our object)
            var bnd:Rectangle = dst ? dst : new Rectangle(),
                selfBnd:Rectangle = _hBmd.getColorBoundsRect(0xFF000000, 0x00000000, false);

            // transform the area to eliminate the effect of _hMtr transformation; now we've obtained
            // the bounds of the object in its own coord. system (self bounds)
            selfBnd.offset(-tx, -ty);

            if (space && space !== obj) { // the dst coord space is different from the object's one
                // so we need to obtain transformation matrix from the object's coord space to the dst one
                var mObjToSpace:Matrix;

                if (space === obj.parent) {
                    // optimization
                    mObjToSpace = obj.transform.matrix;
                } else if (space == obj.stage) {
                    // optimization
                    mObjToSpace = obj.transform.concatenatedMatrix;
                } else {
                    // general case
                    var mStageToSpace:Matrix = space.transform.concatenatedMatrix; // space -> stage
                        mStageToSpace.invert();                                    // stage -> space
                    mObjToSpace = obj.transform.concatenatedMatrix;                // obj -> stage
                    mObjToSpace.concat(mStageToSpace);                             // obj -> space
                }

                // now we transform all four vertices of the boundary rectangle to the target space
                // and determine the bounds of this transformed shape

                _xMin =  Number.MAX_VALUE;
                _xMax = -Number.MAX_VALUE;
                _yMin =  Number.MAX_VALUE;
                _yMax = -Number.MAX_VALUE;

                expandBounds(mObjToSpace.transformPoint(getPoint(selfBnd.x, selfBnd.y)));
                expandBounds(mObjToSpace.transformPoint(getPoint(selfBnd.right, selfBnd.y)));
                expandBounds(mObjToSpace.transformPoint(getPoint(selfBnd.x, selfBnd.bottom)));
                expandBounds(mObjToSpace.transformPoint(getPoint(selfBnd.right, selfBnd.bottom)));

                bnd.x = _xMin;
                bnd.y = _yMin;
                bnd.width = _xMax - _xMin;
                bnd.height = _yMax - _yMin;
            } else {
                // the dst coord space is the object's one, so we simply return the self bounds
                bnd.x = selfBnd.x;
                bnd.y = selfBnd.y;
                bnd.width = selfBnd.width;
                bnd.height = selfBnd.height;
            }

            return bnd;
        }

        private function getPoint(x:Number, y:Number):Point {
            _hPoint.x = x;
            _hPoint.y = y;
            return _hPoint;
        }

        private function expandBounds(p:Point):void {
            if (p.x < _xMin) {
                _xMin = p.x;
            }
            if (p.x > _xMax) {
                _xMax = p.x;
            }
            if (p.y < _yMin) {
                _yMin = p.y;
            }
            if (p.y > _yMax) {
                _yMax = p.y;
            }
        }
    }
}
于 2013-01-22T21:05:15.660 回答
0

我不确定使用 getBounds 或 getRect 的普通方法是否可行,因为这些方法只会返回正常的 100x100 正方形。

我有几个建议给你。

首先,您可以动态应用过滤器。这样,您将获得过滤器的数字,并且可以通过编程方式进行数学运算以计算出实际大小。

您可以向 fla 中的影片剪辑添加第二层,它具有原始正方形的尺寸以及所有过滤器。然后将此正方形的 alpha 设置为零

第三,你可以只在 fla 中有一个包含正方形的 png 以及其中的发光。

就我个人而言,我会选择中间选项,因为如果要更改过滤器或原始正方形,则需要最少的维护。

于 2009-01-21T19:34:17.860 回答
0

好吧,有好消息也有坏消息。坏消息是,确实没有一种有效的方法可以“正确”地做到这一点。好消息是有足够的方法来近似它。

一般规则是宽度的大小约为 (filter.blurX * 1.5) + sprite.width,其中“filter”是 sprite.filters 数组中的过滤器。模糊和高度也是如此。另一个一般规则是最小 x 变为 sprite.x - ( filter.blurX * 1.5 ) / 2;

这些数字都不是“Adobe 官方”,但工作在足够合理的误差范围内,允许您基于此创建 BitmapData。

于 2009-01-21T20:09:39.333 回答