package {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.Graphics;
/*示例内容自适应盒子的大小*/
[SWF(width=800, height=600)]
public class SampleAdjustSize extends Sprite {
private var borderWid:uint=600;//边框宽
private var borderHei:uint=400;//边框高
private var box:Sprite;
private var btm:Bitmap;
[Embed(source="img.jpg")] //嵌入864x684宽高图一张
public var IMG:Class;
public function SampleAdjustSize():void
{
init();
}
private function init():void
{
initBorder();
btm = new IMG() as Bitmap;
adjustSize();
}
private function initBorder():void
{
box = new Sprite();
var g:Graphics = box.graphics;
g.lineStyle(1, 0x000000);
g.drawRect(0, 0, borderWid, borderHei);
addChild(box);
box.x = this.stage.stageWidth / 2 - box.width /2;
box.y = this.stage.stageHeight / 2 - box.height / 2;
}
private function adjustSize():void
{
if(btm.width < box.width && btm.height < box.height) return;
var px:Number = box.width / btm.width;
var py:Number = box.height / btm.height;
var scale:Number = px > py ? py :px;
btm.width = btm.width * scale;
btm.height = btm.height * scale;
box.addChild(btm);
}
}
}