flex 绘线和快照图片

来源:互联网 发布:centos u盘启动盘制作 编辑:程序博客网 时间:2024/06/07 21:44

1>画线条cnv为Cnavas的控件名

//风格样式

cnv.graphics.lineStyle(0.5, 0x0000FF, 0.5, false, LineScaleMode.VERTICAL,
CapsStyle.NONE, JointStyle.MITER, 10);

//线条起始点
cnv.graphics.moveTo(0,0);

//线条终点
 cnv.graphics.lineTo(0,100);

2>创建快照数据:
    var _bitmapdata:BitmapData=ImageSnapshot.captureBitmapData(UIComponent(_cupShowCanvas));
     //存储新图片信息的数组,参数和原图数组要一致。因为没裁剪区域也占用存储单元
     var cutbitmapdata:BitmapData=new BitmapData(_bitmapdata.width / 4 + 60, _bitmapdata.height);
     //将bitmapData的注册点放在截图的起点39,99
      cutbitmapdata.draw(_bitmapdata, new Matrix(1, 0, 0, 1, -2 * (_cupShowCanvas.width *0.33) + 30, 0));
     var bitmap:Bitmap=new Bitmap(cutbitmapdata);
     byteArray=new PNGEncoder().encode(bitmap.bitmapData);

 

ImageSnapshot.captureBitmapData方法源码:

/**
     *  A utility method to grab a raw snapshot of a UI component as BitmapData.
     *
     *  @param source An object that implements the
     *    <code>flash.display.IBitmapDrawable</code> interface.
     *
     *  @param matrix A Matrix object used to scale, rotate, or translate
     *  the coordinates of the captured bitmap.
     *  If you do not want to apply a matrix transformation to the image,
     *  set this parameter to an identity matrix,
     *  created with the default new Matrix() constructor, or pass a null value.
     *
     *  @param colorTransform A ColorTransform
     *  object that you use to adjust the color values of the bitmap. If no object
     *  is supplied, the bitmap image's colors are not transformed. If you must pass
     *  this parameter but you do not want to transform the image, set this parameter
     *  to a ColorTransform object created with the default new ColorTransform() constructor.
     *
     *  @param blendMode A string value, from the flash.display.BlendMode
     *  class, specifying the blend mode to be applied to the resulting bitmap.
     *
     *  @param clipRect A Rectangle object that defines the
     *  area of the source object to draw. If you do not supply this value, no clipping
     *  occurs and the entire source object is drawn.
     *
     *  @param smoothing A Boolean value that determines whether a
     *  BitmapData object is smoothed when scaled.
     *
     *  @return A BitmapData object representing the captured snapshot.
     * 
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    public static function captureBitmapData(
                                source:IBitmapDrawable, matrix:Matrix = null,
                                colorTransform:ColorTransform = null,
                                blendMode:String = null,
                                clipRect:Rectangle = null,
                                smoothing:Boolean = false):BitmapData
    {
        var data:BitmapData;
        var width:int;
        var height:int;

        var normalState:Array;
        if (source is IUIComponent)
            normalState = prepareToPrintObject(IUIComponent(source));

        try
        {
            if (source != null)
            {
                if (source is DisplayObject)
                {
                    width = DisplayObject(source).width;
                    height = DisplayObject(source).height;
                }
                else if (source is BitmapData)
                {
                    width = BitmapData(source).width;
                    height = BitmapData(source).height;
                }
                else if (source is IFlexDisplayObject)
                {
                    width = IFlexDisplayObject(source).width;
                    height = IFlexDisplayObject(source).height;
                }
            }

            // We default to an identity matrix
            // which will match screen resolution
            if (!matrix)
                matrix = new Matrix(1, 0, 0, 1);

            var scaledWidth:Number = width * matrix.a;
            var scaledHeight:Number = height * matrix.d;
            var reductionScale:Number = 1;

            // Cap width to BitmapData max of 2880 pixels
            if (scaledWidth > MAX_BITMAP_DIMENSION)
            {
                reductionScale = scaledWidth / MAX_BITMAP_DIMENSION;
                scaledWidth = MAX_BITMAP_DIMENSION;
                scaledHeight = scaledHeight / reductionScale;
   
                matrix.a = scaledWidth / width;
                matrix.d = scaledHeight / height;
            }

            // Cap height to BitmapData max of 2880 pixels
            if (scaledHeight > MAX_BITMAP_DIMENSION)
            {
                reductionScale = scaledHeight / MAX_BITMAP_DIMENSION;
                scaledHeight = MAX_BITMAP_DIMENSION;
                scaledWidth = scaledWidth / reductionScale;
   
                matrix.a = scaledWidth / width;
                matrix.d = scaledHeight / height;
            }

            // the fill should be transparent: 0xARGB -> 0x00000000
            // only explicitly drawn pixels will show up
            data = new BitmapData(scaledWidth, scaledHeight, true, 0x00000000);
            data.draw(source, matrix, colorTransform,
                      blendMode, clipRect, smoothing);
        }
        finally
        {
            if (source is IUIComponent)
                finishPrintObject(IUIComponent(source), normalState);
        }

        return data;
    }

原创粉丝点击