36

600x600のBitmapDataがあり、100x100にスケールダウンしたいとします。

4

6 に答える 6

69

これは機能します:

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);
于 2009-06-08T12:20:33.373 に答える
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/

于 2009-06-08T18:52:59.817 に答える
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;
}

drawメソッドは、DisplayObjectおよびBitmapDataのインターフェイスであるIBitmapDrawableを受け入れます。

于 2011-07-07T09:03:49.203 に答える
1

自分でコードを書かずに。これにアプローチする方法は、目的のサイズの新しいBitmapDataオブジェクトを作成し、bitmap.drawメソッドを使用して大きいオブジェクトを小さいオブジェクトにコピーすることです。ビットマップ.drawメソッドは、コピー時にスケーリングに使用できる行列引数も受け入れます。

于 2009-06-08T12:13:30.173 に答える
0

マトリックススケーリングを使用する場合の問題は、アンチエイリアスやスムージングが行われないことです。これは、ダウンスケーリングのみを行うことが確実な場合はおそらく問題ありませんが、より一般的な方法では、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」を使用して何でもできます。

于 2009-06-23T02:10:01.437 に答える
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;
}
于 2017-08-21T09:07:36.307 に答える