【问题标题】:How can I ammend this resize script to fill/crop to container?如何修改此调整大小脚本以填充/裁剪到容器?
【发布时间】:2011-08-30 02:03:38
【问题描述】:

我有一个简单的脚本,可以调整加载图像的大小以适应特定的宽度和高度,但是我希望该选项能够填充,即居中并裁剪到特定的宽度/高度 - 关于我如何做的任何想法修改这个?

当前脚本:

function resizeImg(mc:MovieClip, maxW:Number, maxH:Number=0, constrainProportions:Boolean=true):void{
    maxH = maxH == 0 ? maxW : maxH;
    mc.width = maxW;
    mc.height = maxH;
    if (constrainProportions) {
        mc.scaleX < mc.scaleY ? mc.scaleY = mc.scaleX : mc.scaleX = mc.scaleY;
    }
}

我尝试从 Soulwire (http://blog.soulwire.co.uk/flash/actionscript-3/fit-a-displayobject-into-a-rectangle) 中挑选 DisplayUtils AS3 类的代码,但它很漂亮很好混淆并且没有cmets所以我很挣扎:(

【问题讨论】:

    标签: actionscript-3 flash-cs5


    【解决方案1】:

    为此,您首先需要存储原始宽度和高度,以便使用它来缩放图像。除此之外,您需要屏蔽图像并使用您的 maxW 和 maxH 值作为它的尺寸。之后你就可以使用这样的函数了:

    function resizeImg(mc:Sprite, maxW:Number, maxH:Number = 0, constrainProportions:Boolean = true) : void
    {
        maxH = maxH == 0 ? maxW : maxH;
    
        if(constrainProportions)
        {
    
            // First of we'll make the mc fit within the viewport
    
            // calulate the difference between the max and stored dimensions
            var dX:Number = maxW - originalWidth;
            var dY:Number = maxH - originalHeight;
    
            // evaluate values
            if (dY > dX)
            {
    
                // mc is wider then maxW
                // set width to max and scaleY to offset of width
                mc.width = maxW;
                mc.scaleY = mc.scaleX;
            }
            else
            {
                // mc is heigher then maxH
                // set height to max and scaleX to offset of height 
                mc.height = maxH;
                mc.scaleX = mc.scaleY;
            }
    
            // the mc now fits within the max viewport
            // now we'll use the same trick to fill the resized mc in the max viewport
    
            var dXcorrection:Number = maxW - mc.width;
            var dYcorrection:Number = maxH - mc.height;
    
            if (dYcorrection < dXcorrection)
            {
                mc.width = maxW;
                mc.scaleY = mc.scaleX;
            }
            else
            {
                mc.height = maxH;
                mc.scaleX = mc.scaleY;
            }
    
    
            // finally we'll center the resized mc within the max viewport 
            mc.x = (maxW - mc.width)/2;
            mc.y = (maxH - mc.height)/2;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      • 2011-08-20
      • 2011-10-14
      • 2019-05-10
      • 1970-01-01
      相关资源
      最近更新 更多