如何使用StageScaleMode.SHOW_ALL获取舞台尺寸?

时间:2011-03-29 00:14:22

标签: flash actionscript-3 bitmap scale dimensions

如果将stage.scaleMode设置为StageScaleMode.SHOW_ALL,则可以按比例放大或缩小swf,并可以在顶部/底部或左/右填充。但是,stage.width + height总是返回swf定义的宽度和高度,stage.scaleX + Y总是返回1.据我所知,不会抛出resize事件。那么我如何得到实际的尺度和尺寸呢?

我希望他们有两个问题:

  1. 我想在用户可以看到的情况下,在顶部/底部或左/右填充填充物。

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

2 个答案:

答案 0 :(得分:3)

在您的情况下,使用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
}

答案 1 :(得分: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;
}