Actionscript 3 Simplebutton图像比实际图像大

Actionscript 3 Simplebutton图像比实际图像大,actionscript-3,flash,flash-builder,Actionscript 3,Flash,Flash Builder,我正在创建一个按钮,我使用beginBitmapFill方法将图像添加到它。一切正常,问题是加载程序加载的图像比实际图像大 类创建按钮 包模式{ 导入flash.display.Sprite; 导入org.osmf.net.StreamingURLResource; 公共类LoadButton扩展了Sprite { 公共储蓄; 公共函数LoadButtonx:uint,save:String,url:String { var按钮:CustomSimpleButton=新建CustomSimpl

我正在创建一个按钮,我使用beginBitmapFill方法将图像添加到它。一切正常,问题是加载程序加载的图像比实际图像大

类创建按钮

包模式{ 导入flash.display.Sprite; 导入org.osmf.net.StreamingURLResource; 公共类LoadButton扩展了Sprite { 公共储蓄; 公共函数LoadButtonx:uint,save:String,url:String { var按钮:CustomSimpleButton=新建CustomSimpleButtonurl; 按钮x=x; 保存=保存; addChildbutton; } } } 导入flash.display.*; 导入flash.display.Bitmap; 导入flash.display.DisplayObject; 导入flash.display.Shape; 导入flash.display.SimpleButton; 导入flash.events.*; 导入flash.events.EventDispatcher; 导入flash.events.MouseEvent; 导入flash.net.URLRequest; 导入flash.geom.Matrix; 类CustomSimpleButton扩展了SimpleButton { 专用变量upColor:uint=0xFFCC00; 专用var过色:uint=0xCCFF00; 私有var下行颜色:uint=0x00CCFF; 私有变量sizew:uint=100; 私有变量sizeh:uint=88; 公共函数CustomSimpleButtonurl:String { downState=新按钮显示状态DownColor、sizew、sizeh、url; overState=新按钮显示状态Overcolor、sizew、sizeh、url; upState=新按钮显示状态UpColor、sizew、sizeh、url; hitTestState=new ButtonDisplayStateupColor,sizew*2,sizeh,url; hitTestState.x=-sizew/4; hitTestState.y=hitTestState.x; useHandCursor=true; } } 类按钮显示状态扩展形状 { 专用颜色:uint; 私有变量大小:uint; 私有var sizeh:uint; 公共函数按钮显示状态bgColor:uint,sizew:uint,sizeh:uint,url:String { this.bgColor=bgColor; this.size=sizew; this.sizeh=sizeh; 拉乌尔; } 私有函数drawurl:String:void { var myLoader:Loader=新加载器; var图像:位图; var uri=新的URLRequesturl; myLoader.contentLoaderInfo.addEventListenerEvent.COMPLETE,函数E:Event { image=新的Bitmape.target.content.bitmapData; graphics.beginBitmapFillimage.bitmapData; graphics.drawRect0、0、100、88; 图形.endFill; }; loaduri; }
} 它显示为那样裁剪的原因是BitmapData比您要填充的区域大

在使用加载的位图数据填充形状之前,需要将其缩小到适当的大小

基于,您可以执行以下操作:

    var scaleAmount:Number;
    if(e.target.content.width >= e.target.content.height){
        scaleAmount = size / e.target.content.width;
    }else{
        scaleAmount = sizeh / e.target.content.height;
    }
    var scaledBitmapData:BitmapData = scaleBitmapData(e.target.content.bitmapData, scaleAmount);

    graphics.beginBitmapFill(scaledBitmapData);
    graphics.drawRect(0, 0, size, sizeh);
    graphics.endFill();

    function scaleBitmapData(bitmapData:BitmapData, scale:Number):BitmapData {
        scale = Math.abs(scale);
        var width:int = (bitmapData.width * scale) || 1;
        var height:int = (bitmapData.height * scale) || 1;
        var transparent:Boolean = bitmapData.transparent;
        var result:BitmapData = new BitmapData(width, height, transparent);
        var matrix:Matrix = new Matrix();
        matrix.scale(scale, scale);
        result.draw(bitmapData, matrix);
        return result;
    }
不过,只需将按钮状态设置为精灵并添加加载的位图对象并直接缩放它可能会更容易:

class ButtonDisplayState extends Sprite
{
    private var bgColor:uint;
    private var image:Bitmap;
    private var size:uint;
    private var sizeh:uint;

    public function ButtonDisplayState(bgColor:uint, sizew:uint,sizeh:uint,url:String) 
    {
        this.bgColor = bgColor;
        this.size    = sizew;
        this.sizeh = sizeh;
        loadImage(url);
    }

    private function loadImage(url:String):void 
    {
        var myLoader:Loader = new Loader();
        var uri = new URLRequest(url);
        myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event)
        {
            image = new Bitmap(e.target.content.bitmapData);

            //scale the bitmap while retaining aspect ratio
            //scale based off which dimension (width or height) is bigger
            if(image.width >= image.height){
                image.scaleX= size / image.width; //scale it to the width specified
                image.scaleY = image.scaleX; //make the height aspect ratio match the widths
            }else{
                image.scaleY = sizeh /image.height;
                image.scaleX = image.scaleY;
            }
            addChild(image);  //add it to the display list of this object
        });
        myLoader.load(uri); 
    }
}

如果我添加addChild,它工作,图片大小正确,但我需要使用beginBitmapFill,然后nai工作什么是“nai”?我更新了答案,加入了另一个例子。你找到解决方案了吗?如果你觉得我的答案有帮助,请投票。如果它导致您的解决方案,请接受它。如果你发现了不同的解决方案,请自己回答这个问题。如果您仍然需要帮助,请更新您的问题或留下评论。