2015-04-30 77 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

相關問題