2015-04-30 37 views
1

我正在创建一个按钮,我使用beginBitmapFill方法向它添加图像。一切工作正常,问题是由一个Loader(加载)的图像大于它实际上是Simplebutton图像大于它实际上是

类创建按钮

package mode { 
 
\t import flash.display.Sprite; 
 
\t import org.osmf.net.StreamingURLResource; 
 
\t 
 
\t public class LoadButton extends Sprite 
 
\t { 
 
\t \t public var Save; 
 
\t \t public function LoadButton(x:uint,save:String,url:String) 
 
\t \t { 
 
\t \t \t var button:CustomSimpleButton = new CustomSimpleButton(url); 
 
\t \t \t button.x = x; 
 
\t \t \t Save = save; 
 
\t \t \t addChild(button); 
 
\t \t } 
 
\t } 
 
} 
 

 
import flash.display.*; 
 
import flash.display.Bitmap; 
 
import flash.display.DisplayObject; 
 
import flash.display.Shape; 
 
import flash.display.SimpleButton; 
 
import flash.events.*; 
 
import flash.events.EventDispatcher; 
 
import flash.events.MouseEvent; 
 
import flash.net.URLRequest; 
 
import flash.geom.Matrix; 
 

 
class CustomSimpleButton extends SimpleButton 
 
{ 
 
\t private var upColor:uint = 0xFFCC00; 
 
\t private var overColor:uint = 0xCCFF00; 
 
\t private var downColor:uint = 0x00CCFF; 
 
\t private var sizew:uint  = 100; 
 
\t private var sizeh:uint  = 88; 
 
\t 
 
\t public function CustomSimpleButton(url:String) 
 
\t { 
 
\t \t downState  = new ButtonDisplayState(downColor, sizew,sizeh,url); 
 
\t \t overState  = new ButtonDisplayState(overColor, sizew,sizeh,url); 
 
\t \t upState  = new ButtonDisplayState(upColor, sizew,sizeh,url); 
 
\t \t hitTestState = new ButtonDisplayState(upColor, sizew * 2,sizeh,url); 
 
\t \t hitTestState.x = -(sizew/4); 
 
\t \t hitTestState.y = hitTestState.x; 
 
\t \t useHandCursor = true; 
 
\t } 
 
} 
 

 
class ButtonDisplayState extends Shape 
 
{ 
 
\t private var bgColor:uint; 
 
\t private var size:uint; 
 
\t private var sizeh:uint; 
 
\t 
 
\t public function ButtonDisplayState(bgColor:uint, sizew:uint,sizeh:uint,url:String) 
 
\t { 
 
\t \t this.bgColor = bgColor; 
 
\t \t this.size = sizew; 
 
\t \t this.sizeh = sizeh; 
 
\t \t draw(url); 
 
\t } 
 
\t 
 
\t private function draw(url:String):void 
 
\t { 
 
\t \t var myLoader:Loader = new Loader(); 
 
\t \t var image:Bitmap; 
 
\t \t var uri = new URLRequest(url); 
 
\t \t myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event) 
 
\t \t { 
 
\t \t \t image = new Bitmap(e.target.content.bitmapData); 
 

 
\t \t \t graphics.beginBitmapFill(image.bitmapData); 
 
\t \t \t graphics.drawRect(0, 0, 100, 88); 
 
\t \t \t graphics.endFill(); 
 
\t \t }); 
 
\t \t myLoader.load(uri); \t 
 
\t } 
 
}

图像如何是100x88

How the image is 100x88

,因为它是

enter image description here

回答

0

它显示出来的原因就是因为BitmapData比你填充的区域大。

在用加载的BitmapData填充形状之前,您需要将其销售到适当的尺寸。

基础上answer to this question,你可以做到以下几点:

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; 
    } 

可能更容易,虽然,只是让你的按钮的状态Sprite并添加加载Bitmap对象,并直接缩放:

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); 
    } 
} 
+0

如果我添加addChild它的作品,图片是正确的大小,但我需要使用beginBitmapFill然后奈作品 – Rene

+0

什么是'奈'?我更新的答案包括另一个例子 – BadFeelingAboutThis

+0

迪你找到一个解决方案?如果您发现我的答案有帮助,请立即注册。如果它导致您的解决方案,请接受它。如果您找到了不同的解决方案,请自己回答问题。如果您仍然需要帮助,请更新您的问题或留下评论。 – BadFeelingAboutThis

相关问题