- Flex繪圖可以使用flash.display.Graphics繪圖。
Graphics 類擁有一系列的函數建立向量圖形。Display 對象可以支援Sprite和Shape對象的繪圖。這些類含有Graphics類型的graphics屬性。例如函數drawRect()(直角矩形),drawRoundRect()(圓角矩形), drawCircle()(圓形), drawEllipse()(橢圓)。
但是Graphics對象不能夠直接從ActionScript語言中直接執行個體化,直接使用new嘗試執行個體化,會拋出異常。並且Graphics類是final的,不能夠被繼承。
在Sprite和Shape中都含有此屬性。
例如代碼:
var child:Shape = new Shape(); child.graphics.beginFill(bgColor); child.graphics.lineStyle(borderSize, borderColor); child.graphics.drawRoundRect(0, 0, size, size, cornerRadius); child.graphics.endFill(); addChild(child); |
這段代碼需要寫在從DisplayObjectContainer繼承的類中。
- 上面是直接使用包含Graphics對象的類進行繪製的,而包含graphics對象是從Shape或者Sprite繼承而來。還有一種方法,可以將圖繪製到Bitmap上。
這種方法步驟如下:
(a)
建立BitmapData對象
(b)
調用BitmapDate.
draw(source:IBitmapDrawable, matrix:Matrix = null,
colorTransform:flash.geom:ColorTransform = null,
blendMode:String = null,
clipRect:Rectangle = null,
smoothing:Boolean = false):void
在此函數中,實現IBitmapDrawable介面的類比較多,包括TextField,Graphics等
(c)
建立Bitmap對象,用BitmapData進行初始化
Bitmap(bitmapData:BitmapData = null, pixelSnapping:String = "auto", smoothing:Boolean = false)
(d)
通過DisplayObjectContainer及其子類調用addChild(child:DisplayObject):DisplayObject將Bitmap添加該Bitmap對象。
優缺點:
優點:使用Bitmap比較靈活,佔用資源小,速度快。
缺點:使用Bitmap有大小限制。因此千萬注意,否則會出現運行時異常。
In AIR 1.5 and Flash Player 10, the maximum size for a BitmapData object is 8,191 pixels in width or height, and the total number of pixels cannot exceed 16,777,215 pixels. (So, if a BitmapData object is 8,191 pixels wide, it can only be 2,048 pixels high.) In Flash Player 9 and earlier and AIR 1.1 and earlier, the limitation is 2,880 pixels in height and 2,880 pixels in width. If you specify a width or height value that is greater than 2880, a new instance is not created. |