如果您设置在Stage.scaleMode为StageScaleMode.SHOW_ALL,您的SWF可以放大或缩小,并且可以填充在顶部/底部或左/右。但是,您的SWF定义stage.width +高度总是返回的宽度和高度,以及stage.scaleX + Y始终返回1。据我了解,调整大小事件不会引发。那么,如何得到实际的规模和尺寸?
我想他们两个问题:
我要填补顶部/底部的填充或左/右的东西,只有当用户能看到它。
我绘制矢量成位图,并希望适当扩大它,所以它看起来并不锯齿(或模糊时将Bitmap.smoothing)。闪光似乎做正确的比例,当您将cacheAsBitmap = true时,让我怎么重建呢?
解决方案在你的情况下,它可能会更容易使用StageScaleMode.NO_SCALE且为code中的调整自己:
stage.addEventListener(Event.RESIZE,onStageResize);
私有函数onStageResize(事件:事件):无效
{
VAR stageW:INT = stage.stageWidth;
VAR stageH:INT = stage.stageHeight;
VAR contentW:INT = yourVisibleContent.width;
VAR contentH:INT = yourVisibleContent.height;
//调整其大小,以适应
VAR canvasAspectRatio:数= stageW / stageH;
VAR contentAspectRatio:数= contentW / contentH;
如果(canvasAspectRatio> contentAspectRatio)
{
yourVisibleContent.height = stageH;
yourVisibleContent.width = yourVisibleContent.height * contentAspectRatio;
} 其他 {
yourVisibleContent.width = stageW;
yourVisibleContent.height = yourVisibleContent.width / contentAspectRatio;
}
//中心吧:
yourVisibleContent.x =(stageW - yourVisibleContent.width)/ 2;
yourVisibleContent.y =(stageH - yourVisibleContent.height)/ 2;
//填充剩余空间以黑色:
graphics.beginFill(0x000000处);
如果(canvasAspectRatio> contentAspectRatio)
{
VAR horizontalEmptySpace:数= stageW - yourVisibleContent.width;
graphics.drawRect(0,0,horizontalEmptySpace / 2,stageH);
graphics.drawRect(stageW - horizontalEmptySpace / 2,0,horizontalEmptySpace / 2,stageH);
}其他{
VAR verticalEmptySpace:数= stageH - yourVisibleContent.height;
graphics.drawRect(0,0,stageW,verticalEmptySpace / 2);
graphics.drawRect(0,stageH - verticalEmptySpace / 2,stageW,verticalEmptySpace / 2);
}
//现在你也可以重新绘制的位图分辨率更高
//很容易阅读你的内容与规模:yourVisibleContent.scaleX和yourVisibleContent.scaleY
}
If you set the stage.scaleMode to StageScaleMode.SHOW_ALL, your swf may be scaled up or down, and may be padded on the top/bottom or left/right. However, stage.width + height always return the width and height as defined by your swf, and stage.scaleX + Y always return 1. As I understand it, resize events are not thrown. So how do I get the actual scale and dimensions?
I want them for two problems:
I want to fill that padding on the top/bottom or left/right with something only if the user can see it.
I am drawing vectors into bitmaps and want to properly scale it so it doesn't look jagged (or fuzzy with bitmap.smoothing). Flash seems to do the scaling correctly when you cacheAsBitmap=true, so how do I recreate this?
解决方案
In your case it would probably be easier to use StageScaleMode.NO_SCALE and to code the resizing yourself:
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
}