AS3 - Scale BitmapData

半城伤御伤魂 提交于 2019-12-23 09:49:47

问题


I'd like to scale a BitmapData to different sizes such as 200, 400, 600 and 800.

What is a good way to do that?


回答1:


You can't directly scale a BitmapData but you can make a scaled clone of it. Here is a quick example for scaling a BitmapData :

package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.geom.Matrix;

import mx.core.BitmapAsset;

public class Test extends Sprite {


    [Embed(source="test.jpg")]
    private var Image:Class;

    public function Test() {

        var originalBitmapData:BitmapData = BitmapAsset(new Image()).bitmapData;

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

        var bitmapA:Bitmap = new Bitmap(originalBitmapData);
        addChild(bitmapA);

        var bitmapB:Bitmap = new Bitmap(scaleBitmapData(originalBitmapData, 0.5));
        addChild(bitmapB);

    }
}
}



回答2:


Setting the width and height of a bitmap object scales that image, so create a bitmap with your bitmapData then scale it using width/height, or use scaleX/Y if you want to use scaling values.

var bmp:Bitmap = new Bitmap(bmpData);
bmp.width = 400; //  or 600,800 etc.
bmp.height = 400;

If you don't want to use a bitmap object and directy scale the BitmapData see this What is the best way to resize a BitmapData object?



来源:https://stackoverflow.com/questions/10722267/as3-scale-bitmapdata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!