翻转中心周围的图像

时间:2012-01-05 03:43:15

标签: flash actionscript-3 flex

要在中心周围翻转图像,我正在使用这段代码:

        // Calculate offset
        var offsetWidth:Number = image.contentWidth/2.0;
        var offsetHeight:Number =  image.contentHeight/2.0;
        // Perform flip
        var matrix:Matrix = new Matrix();
        matrix.translate(-offsetWidth, -offsetHeight);
        if(direction=="HORIZONTAL"){
            matrix.scale(-1, 1);
        }else if(direction=="VERTICAL"){
            matrix.scale(1,-1)
        }
        matrix.translate(+offsetWidth, +offsetHeight);
        matrix.concat(image.transform.matrix);
        image.transform.matrix = matrix.clone();

哪个工作正常。但我的问题是当我尝试从图像中获取BitmapData时这样:

        var bitmapData:BitmapData = new BitmapData(image.width,image.height); 
        bitmapData.draw(image);

并使用bitmapData作为其他图像的来源,不显示图像。出现“图像图标”。 此外,我正在围绕中心旋转图像,并使用下面提到的类似代码,它的工作正常,我能够将bitmapdata复制到另一个图像..这是代码供参考:

   var matrix:Matrix = new Matrix(); 
   matrix.rotate(Math.PI/2); 
   matrix.tx = img.content.height; 
   var bd:BitmapData = new BitmapData(img.content.height, img.content.width);   
   bd.draw(img.content, matrix);

请提供帮助。

1 个答案:

答案 0 :(得分:2)

答案就在问题中。

在第二种情况下,您正在使用矩阵

bd.draw(img.content, matrix);

同样适用于翻转

bitmapData.draw(image, matrix);

编辑:

计算翻转中矩阵的平移是不正确的。只有在水平和垂直翻转时才需要在X和Y上进行平移。 请在下面找到它正在运行的代码。

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

    [SWF(width="640", height="480")]

    public class FlipRotate extends Sprite
    {
        [Embed(source="away3d.jpg")]
        private var Pic:Class;


        public function FlipRotate()
        {
            super();

            stage.scaleMode = StageScaleMode.NO_SCALE;


            var sourceImage:Bitmap;
            var targetImage:Bitmap;

            sourceImage = new Pic();

            //flip(sourceImage, "BOTH");
            flip(sourceImage, "VERTICAL");
            //flip(sourceImage, "BOTH");

            rotate(sourceImage, Math.PI/3);



            targetImage = new Bitmap();

            /* Get the current bounds of source image to calculate the dimenstion of new image
             Apply translation to bring the content outside into view in new image */
            var bounds:Rectangle = sourceImage.getBounds(this);
            var matrix:Matrix = sourceImage.transform.matrix;
            matrix.translate(-bounds.x, -bounds.y);

            /* Draw in new image applying translated matrix of source imgae */
            targetImage.bitmapData = new BitmapData(bounds.width, bounds.height);
            targetImage.bitmapData.draw(sourceImage, matrix);

            /* Add new image and postion at the center of stage */
            addChild(targetImage);
            targetImage.x = (stage.stageWidth-targetImage.width)/2;
            targetImage.y = (stage.stageHeight-targetImage.height)/2;

        }

        public function flip(image:Bitmap, direction:String):void{

            var matrix:Matrix = image.transform.matrix;

            if(direction=="HORIZONTAL"){
                matrix.scale(-1, 1);
                matrix.translate(image.width, 0);
            }else if(direction=="VERTICAL"){
                matrix.scale(1,-1);
                matrix.translate(0, image.height);
            }else if(direction == "BOTH"){
                matrix.scale(-1,-1);
                matrix.translate(image.width, image.height);
            }

            image.transform.matrix = matrix;

        }   

        public function rotate(image:Bitmap, angle:Number):void{

            var matrix:Matrix = image.transform.matrix;

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

            image.transform.matrix = matrix;

        }   


    }
}