Giả sử tôi có BitmapData 600x600 và tôi muốn giảm kích thước xuống 100x100.Cách tốt nhất để thay đổi kích thước đối tượng BitmapData là gì?
Trả lời
này hoạt động:
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);
Nếu không tự viết mã. Cách tôi sẽ tiếp cận này sẽ là tạo một đối tượng BitmapData mới với kích thước mong muốn và sau đó sử dụng phương thức bitmap.draw để sao chép một đối tượng lớn vào một tệp nhỏ. Phương thức bitmap.draw cũng chấp nhận một đối số ma trận mà bạn có thể sử dụng để mở rộng khi bạn sao chép.
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 là một giao diện cho DisplayObject và BitmapData.
từ: http://www.nightdrops.com/2009/02/quick-reference-drawing-a-scaled-object-in-actionscript/
Vấn đề với việc sử dụng ma trận mở rộng quy mô là nó không làm bất cứ khử răng cưa hoặc mịn - điều này có lẽ là OK nếu bạn chắc chắn sẽ chỉ từng được downscaling, nhưng một phương pháp tổng quát hơn sẽ sử dụng lớp Image để thực hiện thay đổi kích thước. Trong AS3 này sẽ không bao giờ được thêm vào danh sách hiển thị, vì vậy sẽ chỉ được sử dụng "offscreen". Một cái gì đó như thế này (với dữ liệu bitmap của bạn là "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;
Sau đó bạn có thể sử dụng "scaledBitmapData" thay cho "sourceBitmapData" để làm bất cứ điều gì với.
Lớp hình ảnh này đến từ đâu? http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/index.html?filter_flashplayer=10.1 không liệt kê một lớp Hình ảnh như là một phần của thư viện AS3. – Dwayne
Bình chọn không tham chiếu đến lớp Hình ảnh – Jotham
, hoặc là http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/Image.html hoặc http://help.adobe.com /en_US/FlashPlatform/reference/actionscript/3/spark/components/Image.html –
Với mịn:
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;
}
Phương pháp bốc thăm chấp nhận IBitmapDrawable đó là một giao diện cho DisplayObject và BitmapData.
Dưới đây là một biến thể của các bên trên có thêm hỗ trợ cho thu phóng, giãn và hộp thư. Nó có thể không cung cấp hỗ trợ cắt.
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;
}
hoạt động hoàn hảo, cảm ơn! –
'0x000000' này rất quan trọng, không có nó không có độ trong suốt – Cherniv