【问题标题】:ActionScript 3: Resizing image inside UIComponentActionScript 3:在 UIComponent 中调整图像大小
【发布时间】:2011-08-26 08:33:38
【问题描述】:

我有一个 UIComponent,我在里面放了一个加载器(它加载一个图像文件)。

问题: 即使我调整了 UIComponent(到正方形)的大小,它仍然会跟随加载器中图像的大小。但是当我在加载器中调整图像大小时,它会被拉伸。我想要做的是拥有一个具有固定正方形大小的容器(UIComponent),并且在加载图像后,它将恰好适合容器的尺寸而不会被拉伸。谁能帮我怎么做?

【问题讨论】:

    标签: actionscript-3 actionscript


    【解决方案1】:

    我一直在使用这个函数来调整图像大小,同时保持纵横比:

    public function constraintResize(iSourceWidth:*, iSourceHeight:*, iTargetWidth:*, iTargetHeight:*, iFitSmallerDimension:*):* {
      if (iFitSmallerDimension == undefined) iFitSmallerDimension = false;
      if (iTargetWidth == 0 || iTargetHeight == 0) return;
      if (iSourceWidth == 0 || iSourceHeight == 0) return;
    
      var newWidth:*;
      var newHeight:*;
      var targetRatio:*;
      var sourceRatio:*;
    
      var output:* = { width:iSourceWidth, height:iSourceHeight };
    
      if (iSourceWidth <= iTargetWidth && iSourceHeight <= iTargetHeight) {
        if (iFitSmallerDimension) {
          targetRatio = iTargetWidth / iTargetHeight;
          sourceRatio = iSourceWidth / iSourceHeight;
          if (sourceRatio < targetRatio) {
            newHeight = iTargetHeight;
            newWidth = newHeight * sourceRatio;
          } else {
            newWidth = iTargetWidth;
            newHeight = newWidth / sourceRatio;
          } 
          output.width = newWidth
          output.height = newHeight
        }
    
        } else {
    
        targetRatio = iTargetWidth / iTargetHeight
        sourceRatio = iSourceWidth / iSourceHeight
        if (sourceRatio < targetRatio) {
          newHeight = iTargetHeight
          newWidth = newHeight * sourceRatio
        } else {
          newWidth = iTargetWidth
          newHeight = newWidth / sourceRatio
        }
    
        output.width = newWidth
        output.height = newHeight
      }
    
      return output
    }       
    

    该函数将为您提供将source 放入target 的理想尺寸。所以,在你的情况下,你可以这样使用它:

    var s:* = constraintResize(loader.contentWidth, loader.contentHeight, yourSquare.width, yourSquare.height);
    loader.width = s.width;
    loader.height = s.height;
    

    【讨论】:

    • 哇!我以为我可以在 5 行代码内完成。这次真是万分感谢!我一定会试试的!
    【解决方案2】:

    使用 UILoader 组件并在组件检查器窗口中设置 scaleContent=true。

    【讨论】:

    • 我正在尝试导入 fl.containers.UILoader 但 flex 似乎不理解这个包......我需要一些特殊的库吗?
    • 对不起,我对 flex 的了解不够。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2016-12-31
    • 1970-01-01
    相关资源
    最近更新 更多