【问题标题】:How to get the stage dimensions with StageScaleMode.SHOW_ALL?如何使用 StageScaleMode.SHOW_ALL 获取舞台尺寸?
【发布时间】:2011-07-24 21:40:24
【问题描述】:

如果您将 stage.scaleMode 设置为 StageScaleMode.SHOW_ALL,您的 swf 可能会放大或缩小,并且可能会在顶部/底部或左侧/右侧填充。但是,stage.width + height 始终返回您的 swf 定义的宽度和高度,而 stage.scaleX + Y 始终返回 1。据我了解,不会引发调整大小事件。那么如何获得实际的比例和尺寸呢?

我希望他们解决两个问题:

  1. 我想在顶部/底部或左侧/右侧填充一些内容,前提是用户可以看到它。

  2. 我正在将矢量绘制到位图中,并希望适当地对其进行缩放,使其看起来不锯齿(或由于 bitmap.smoothing 模糊)。当您 cacheAsBitmap=true 时,Flash 似乎可以正确进行缩放,那么我该如何重新创建呢?

【问题讨论】:

标签: flash actionscript-3 bitmap scale dimensions


【解决方案1】:

这是我用来在 swf 加载期间获取尺寸并稍后根据 bjornson 的回答使用它们来缩放位图的代码。

var actualScale :Number;
var actualStageWidth :Number;
var actualStageHeight :Number;

private function collectDimensions () :void
{
    stage.scaleMode = StageScaleMode.NO_SCALE;
    actualStageWidth = stage.stageWidth;
    actualStageHeight = stage.stageHeight;
    var contentWidth :Number = yourVisibleContent.width;
    var contentHeight :Number = yourVisibleContent.height;
    var canvasAspectRatio :Number = actualStageWidth / actualStageHeight;
    var contentAspectRatio :Number = contentWidth / contentHeight;
    if (canvasAspectRatio > contentAspectRatio) {
        actualScale = actualStageHeight / contentHeight;
    } else {
        actualScale = actualStageWidth / contentWidth;
    }
    stage.scaleMode = StageScaleMode.SHOW_ALL;
}

public function createBitmap (clip :MovieClip) :Bitmap
{
    var bitmapData :BitmapData = new BitmapData(clip.width, clip.height);
    var matrix :Matrix = new Matrix();
    matrix.scale(actualScale, actualScale);
    bitmapData.draw(clip, matrix);
    var bitmap :Bitmap = new Bitmap(bitmapData);
    bitmap.scaleX = bitmap.scaleY = 1/actualScale;
    bitmap.smoothing = true;
    return bitmap;
}

【讨论】:

    【解决方案2】:

    在您的情况下,使用 StageScaleMode.NO_SCALE 并自己编写调整大小的代码可能会更容易:

    stage.addEventListener(Event.RESIZE, onStageResize);
    private function onStageResize(event : Event) : void 
        {
        var stageW : int = stage.stageWidth;
        var stageH : int = stage.stageHeight;
    
        var contentW : int = yourVisibleContent.width;
        var contentH : int = yourVisibleContent.height;
    
        // resize it to fit
        var canvasAspectRatio : Number = stageW / stageH;
        var contentAspectRatio : Number = contentW / contentH;
        if(canvasAspectRatio > contentAspectRatio)
        {
            yourVisibleContent.height = stageH;
            yourVisibleContent.width = yourVisibleContent.height * contentAspectRatio;
        } else {
    
            yourVisibleContent.width = stageW;
            yourVisibleContent.height = yourVisibleContent.width / contentAspectRatio;
        }
    
        // center it:
        yourVisibleContent.x = (stageW - yourVisibleContent.width) / 2;
        yourVisibleContent.y = (stageH - yourVisibleContent.height) / 2;
    
        // fill remaining space with black:
        graphics.beginFill(0x000000);
        if(canvasAspectRatio > contentAspectRatio)
        {
            var horizontalEmptySpace : Number = stageW - yourVisibleContent.width;
            graphics.drawRect(0, 0, horizontalEmptySpace / 2, stageH);
            graphics.drawRect(stageW - horizontalEmptySpace / 2, 0, horizontalEmptySpace / 2, stageH);
        }else{
            var verticalEmptySpace : Number = stageH - yourVisibleContent.height;
            graphics.drawRect(0, 0, stageW, verticalEmptySpace / 2);
            graphics.drawRect(0, stageH - verticalEmptySpace / 2, stageW, verticalEmptySpace / 2);
        }
    
        // now you can also redraw your bitmaps with higher resolutions
        // it is easy to read the scale of your content with: yourVisibleContent.scaleX and yourVisibleContent.scaleY
    }
    

    【讨论】:

    • 感谢您发布代码!它适用于我的两个要求,但对我现有的鼠标和定位代码造成了破坏。我决定继续使用 SHOW_ALL,而不是缩放所有这些,但在加载 swf 时短暂切换到 NO_SCALE 并使用您的代码来收集实际的比例和尺寸。这意味着我不能听调整大小,但在我的情况下,唯一的副作用是看起来更糟糕的位图。
    • 您应该使用 NO_SCALE 将其固定,只需更新鼠标和定位代码以使用 localToGlobal 和 globalToLocal 将一个对象上的点转换为另一个对象上的坐标。如果它弄乱了您的代码,那么您的代码可能假设局部坐标与全局坐标相同,这是错误的开始。
    猜你喜欢
    • 1970-01-01
    • 2016-08-06
    • 2013-12-14
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多