2009-06-08 10 views

답변

67

이 작동 :

var scale:Number = 1.0/6.0; 
var matrix:Matrix = new Matrix(); 
matrix.scale(scale, scale); 

var smallBMD:BitmapData = new BitmapData(bigBMD.width * scale, bigBMD.height * scale, true, 0x000000); 
smallBMD.draw(bigBMD, matrix, null, null, null, true); 

var bitmap:Bitmap = new Bitmap(smallBMD, PixelSnapping.NEVER, true); 
+0

완벽하게 작동합니다. 감사합니다. –

+2

이 '0x000000'은 매우 중요합니다. 투명도가 없으면 – Cherniv

1

직접 코드를 작성하지 않아도됩니다. 방법은 내가 원하는 접근 방식은 원하는 크기의 새 BitmapData 개체를 만들고 작은 하나에 큰 하나를 복사하려면 bitmap.draw 메서드를 사용합니다. bitmap.draw 메서드는 복사 할 때 사용할 수있는 행렬 인수도 허용합니다.

19
public function drawScaled(obj:IBitmapDrawable, thumbWidth:Number, thumbHeight:Number):Bitmap { 
    var m:Matrix = new Matrix(); 
    m.scale(WIDTH/obj.width, HEIGHT/obj.height); 
    var bmp:BitmapData = new BitmapData(thumbWidth, thumbHeight, false); 
    bmp.draw(obj, m); 
    return new Bitmap(bmp); 
} 

IBitmapDrawable가의 DisplayObject와의 BitmapData에 대한 인터페이스입니다.

에서 : http://www.nightdrops.com/2009/02/quick-reference-drawing-a-scaled-object-in-actionscript/

+0

실제로 표시 객체가 아닌 비트 맵 데이터로 시작했기 때문에 실제로 원하는 것이 아닙니다. 그래도 고마워! – Iain

+1

BitmapData를 사용하여 DisplayObject를 쉽게 지정할 수 있습니다 :-) – Carlo

+0

이 하나가 저에게 효과적이었습니다 :-) – dharm0us

0

매트릭스 스케일링을 사용하는 데 문제가있는 안티 앨리어싱이나 스무딩을하지 않는다는 것입니다 - 이것은 당신이 당신이 오직 규모 축소 된 것입니다 확신한다면 아마 OK이지만,보다 일반적인 방법 Image 클래스를 사용하여 크기 조정을 수행합니다. AS3에서는 이것이 디스플레이 목록에 추가되지 않으므로 단지 "오프 스크린"으로 사용됩니다. ("sourceBitmapData"와 같은 비트 맵 데이터) 이런 식으로 뭔가 :

var image:Image = new Image(); 
image.load(new Bitmap(sourceBitmapData, PixelSnapping.NEVER, true)); 

var scale:uint = 100/600; // this is from your example of 600x600 => 100x100 
var scaledWidth:uint = sourceBitmapData.width * scale; 
var scaledHeight:uint = sourceBitmapData.height * scale; 

image.content.width = scaledWidth; 
image.content.height = scaledHeight; 

var scaledBitmapData:BitmapData = new BitmapData(scaledWidth, scaledHeight); 
scaledBitmapData.draw(image.content); 

image = null; 

그런 다음 무엇이든 함께 할 "sourceBitmapData"대신 "scaledBitmapData"를 사용할 수 있습니다. 스무딩

+0

이 Image 클래스의 출처는 무엇입니까? http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/index.html?filter_flashplayer=10.1에는 Image 클래스가 AS3 라이브러리의 일부로 나열되어 있지 않습니다. – Dwayne

+0

이미지 클래스 – Jotham

+0

에 대한 참조가 없으므로 http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/Image.html 또는 http://help.adobe.com입니다. /en_US/FlashPlatform/reference/actionscript/3/spark/components/Image.html –

10

:

function BitmapScaled(source:IBitmapDrawable, thumbWidth:Number, thumbHeight:Number):BitmapData { 
    var mat:Matrix = new Matrix(); 
    mat.scale(thumbWidth/source.width, thumbHeight/source.height); 
    var bmpd_draw:BitmapData = new BitmapData(thumbWidth, thumbHeight, false); 
    bmpd_draw.draw(source, mat, null, null, null, true); 
    return bmpd_draw; 
} 

연신 방법으로 DisplayObject과의 BitmapData위한 인터페이스이다 IBitmapDrawable을 받아 들인다.

0

다음은 위의 변형으로 줌, 스트레치 및 레터 박스에 대한 지원이 추가되었습니다. 클리핑을 지원하지 않을 수 있습니다.

var newSizeBitmapData:BitmapData = resizeBitmapData(myBitmapData, newWidth, newHeight); 


/** 
* Resize display object or bitmap data to a new size 
**/ 
public static function resizeBitmapData(bitmapDrawable:IBitmapDrawable, width:Number, height:Number, scaleMode:String="none", 
             smooth:Boolean = true, transparent:Boolean = true, fillColor:Number = 0x00000000):BitmapData { 
    var sizedBitmapData:BitmapData; 
    var matrix:Matrix; 
    matrix = getSizeByScaleMode(width, height, Object(bitmapDrawable).width, Object(bitmapDrawable).height, scaleMode); 
    sizedBitmapData = new BitmapData(width, height, transparent, fillColor); 
    sizedBitmapData.draw(bitmapDrawable, matrix, null, null, null, smooth); 

    return sizedBitmapData; 
} 

// Get correct scale. Inspired from code in Apache Flex (license Apache 2.0) 
public static function getSizeByScaleMode(maxWidth:int, maxHeight:int, 
              width:int, height:int, 
              scaleMode:String="letterbox", 
              dpi:Number=NaN):Matrix { 

    var aspectRatio:String = (maxWidth < maxHeight) ? "portrait" : "landscape"; 
    var orientation:String = aspectRatio; 

    var matrix:Matrix = new Matrix(); 

    var scaleX:Number = 1; 
    var scaleY:Number = 1; 

    switch(scaleMode) { 
     case "zoom": 
      scaleX = Math.max(maxWidth/width, maxHeight/height); 
      scaleY = scaleX; 
      break; 

     case "letterbox": 
      scaleX = Math.min(maxWidth/width, maxHeight/height); 
      scaleY = scaleX; 
      break; 

     case "stretch": 
      scaleX = maxWidth/width; 
      scaleY = maxHeight/height; 
      break; 
    } 

    if (scaleX != 1 || scaleY != 0) { 
     width *= scaleX; 
     height *= scaleY; 
     matrix.scale(scaleX, scaleY); 
    } 

    matrix.translate(-width/2, -height/2); 

    matrix.translate(maxWidth/2, maxHeight/2); 

    return matrix; 
}