1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Flash Builder 4 and Flex 4 Bible- P10 ppt

50 365 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 50
Dung lượng 792,41 KB

Nội dung

Chapter 14: Declaring Graphics with MXML and FXG 421 l Ellipse. Draws an elliptical shape. If its height and width are identical, it draws a circle. l Line. Draws a straight line from one set of coordinates to another. l Path. Draws a shape based on a set of drawing commands, creating multiple shape segments. l Rect. Draws a rectangular shape. If its height and width are identical, it draws a square. The spark.primitives package also includes the following classes that are used to group graphics together and embed graphical files in conventional formats such as PNG, GIF, and JPG: l BitmapImage. A class that embeds bitmap data defined in a graphical file. The file can be in any one of the conventional bitmap file formats: PNG, JPG, or GIF. l Graphic. A nonvisual class that can be used to group multiple FXG graphics together. When you place multiple FXG graphics within an instance of the Graphic class, they overlap each other using a layout architecture similar to a Group with a layout of BasicLayout. Drawing lines The Line class draws a line on the screen. As with all such primitives, it must be placed within a Spark application or component. Its width and height properties determine its horizontal and vertical length, while the stroke property determines its color and style. The stroke property must be set to an instance of a class that implements the IStroke inter- face. Examples of such classes in the Flex 4 SDK include GradientStroke and SolidColorStroke. Caution The Line class’s stroke property defaults to null. If you don’t set the stroke to an instance of a class that implements the IStroke interface, the line is invisible. n The following code draws a simple horizontal line. The stroke property is set to an instance of SolidColorStroke with a color of black (#000000) and a weight of 2 pixels: <s:Line width=”700”> <s:stroke> <s:SolidColorStroke color=”#000000” weight=”2”/> </s:stroke> </s:Line> Using gradient strokes As with the fill property used with shapes that are drawn with the Rect, Ellipse, and Path classes (described in the following sections), the stroke property can be set to a gradient of two or more colors with the GradientStroke class or with one of its subclasses, LinearGradientStroke and RadialGradientStroke. 20_488959-ch14.indd 42120_488959-ch14.indd 421 3/5/10 2:29 PM3/5/10 2:29 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Part II: Designing Flex Applications 422 The gradient stroke classes support a property named entries that’s set to an array of two or more instances of the GradientEntry class. The following code draws a horizontal line that’s 10 pixels wide and has five entries: <s:Line width=”700”> <s:stroke> <s:LinearGradientStroke weight=”10”> <s:entries> <s:GradientEntry color=”#000000”/> <s:GradientEntry color=”#ffffff”/> <s:GradientEntry color=”#000000”/> <s:GradientEntry color=”#ffffff”/> <s:GradientEntry color=”#000000”/> </s:entries> </s:LinearGradientStroke> </s:stroke> </s:Line> Figure 14.1 shows the resulting line, with alternating black and white colors. FIGURE 14.1 A horizontal line with a gradient stroke Drawing rectangular and elliptical shapes The two most commonly used primitive vector graphic classes are Rect and Ellipse. Respectively, they render rectangular and elliptical shapes on the screen. Both support fill and stroke prop- erties, which enable you to define the outer border and inner fill of the shape you’re drawing. Each shape’s fill property can be set to an instance of a class that implements the IFill inter- face. Examples of such classes in the Flex 4 SDK include: l BitmapFill l LinearGradient l RadialGradient l SolidColor And, as described in the previous section on the Line class, each shape’s stroke property is set to an instance of a class that implements the IStroke interface. 20_488959-ch14.indd 42220_488959-ch14.indd 422 3/5/10 2:29 PM3/5/10 2:29 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Chapter 14: Declaring Graphics with MXML and FXG 423 Note All shape classes support the x, y, top, bottom, left, right, horizontalCenter, and verticalCenter properties to control a shape’s position within its container, and the height and width properties to control its dimensions. As with all Flex visual components, the height and width can be set either to a numeric value representing a certain number of pixels or to a percentage value. n The following MXML code defines a rectangle with dimensions of 400 pixels width by 300 pixels height. The rectangle’s outer border is a solid black line with a weight of 2 pixels, and the fill is a solid light gray: <s:Rect width=”400” height=”300” horizontalCenter=”0” verticalCenter=”0” > <s:fill> <s:SolidColor color=”#EEEEEE”/> </s:fill> <s:stroke> <s:SolidColorStroke color=”#000000” weight=”2”/> </s:stroke> </s:Rect> Figure 14.2 shows the resulting shape, a simple rectangle with a light gray fill and a black stroke. FIGURE 14.2 This simple shape is defined as an instance of the Rect class. Drawing arbitrary shapes with the Path class The Path class enables you to declare any shape based on a set of commands that replicate the features of the Flash drawing API. Its data property is a string that executes cursor placement and drawing operations. The data string alternates commands and sets of numeric values. Each com- mand is notated with a single alphabetical character, as follows: 20_488959-ch14.indd 42320_488959-ch14.indd 423 3/5/10 2:29 PM3/5/10 2:29 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Part II: Designing Flex Applications 424 l C. Draw a cubic Bezier curve. The first two values are the first set of control coordinates, the second two values are the second set of control coordinates, and the last two values are the drawing destination. l H. Draw a horizontal line from the current cursor coordinate to a new X coordinate. l L. Draw a line from the current cursor position to a set of coordinates. For example, the command L 100 100 causes a line to be drawn from the current cursor position to X and Y coordinates of 100 and 100. l M. Move the cursor to a set of coordinates. For example, the command M 50 100 causes the cursor to be placed at an X coordinate of 50 and a Y coordinate of 100. l Q. Draw a quadratic Bezier curve. The first two values are the control coordinates, and the last two are the drawing destination. l V. Draw a vertical line from the current cursor coordinate to a new Y coordinate. l Z. Close the path. The following simple Path object draws a horizontal line starting at X and Y positions of 100, and then draws a horizontal line to an X position of 500. The color and weight of the path are deter- mined by its stroke property: <s:Path data=”M 100 100 H 500 Z”> <s:stroke> <s:SolidColorStroke color=”black” weight=”5”/> </s:stroke> </s:Path> Note If you change any command to a lowercase character, it’s evaluated as a coordinate relative to the current cur- sor position instead of an absolute coordinate within the vector graphic’s container. For example, a data property of M 100 100 h 500 Z creates a horizontal line that’s 500 pixels wide, starting at an X position of 100, whereas if you use the uppercase H, the line is 400 pixels wide (drawn from 100 to 500 pixels). n Caution Errors in the data property’s commands don’t cause compilation or runtime errors; instead, the shape proba- bly won’t be drawn. n More complex Path objects can be drawn with more commands, and multiple shapes and lines can be drawn in the same Path object by moving the cursor and initiating new draw commands. The following object draws a curved arrow using a series of lines and cubic Bezier curves: <s:Path data=”M 20 0 C 50 0 50 35 20 35 L 15 35 L 15 45 L 0 32 L 15 19 L 15 29 L 20 29 C 44 29 44 6 20 6”> 20_488959-ch14.indd 42420_488959-ch14.indd 424 3/5/10 2:29 PM3/5/10 2:29 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Chapter 14: Declaring Graphics with MXML and FXG 425 <s:stroke> <s:SolidColorStroke color=”0x888888”/> </s:stroke> <s:fill> <s:LinearGradient rotation=”90”> <s:GradientEntry color=”0x000000” alpha=”0.8”/> <s:GradientEntry color=”0xFFFFFF” alpha=”0.8”/> </s:LinearGradient> </s:fill> </s:Path> Figure 14.3 shows the resulting vector graphic: a curved arrow with a linear gradient. FIGURE 14.3 A curved arrow drawn with the Path class Note White space, such as extra space, tab, or line feed characters, is ignored in the data property’s value. In the preceding code, I’ve added space characters and line feeds to make the data property more readable. This doesn’t affect the application’s performance or functionality. n Adding visual effects The primitive vector graphic classes support many properties that enable you to modify their appearance. Examples include gradient fills and strokes, drop shadows and other filters, and scal- ing, or resizing, of vector graphics. In this section I describe some of the most commonly used effects from which you can choose. Using gradient fills The fill property that’s supported by the Rect, Ellipse, and Path classes can be set to a solid color with the SolidColor class or to a gradient with either RadialGradient or LinearGradient. Each does exactly what its name implies: 20_488959-ch14.indd 42520_488959-ch14.indd 425 3/5/10 2:29 PM3/5/10 2:29 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Part II: Designing Flex Applications 426 l LinearGradient. Defines a change in colors in a linear using calculation from one coor- dinate to another. By default the gradient is calculated from left to right, but it can be adjusted by changing the gradient class’s direction property; for example, to change the gra- dient to go from top to bottom, change the LinearGradient object’s direction to 90. l RadialGradient. Defines a change in colors starting from the certain point in an object (by default its center) and radiating outward to its borders. You can set the focalPoint Ratio and rotation properties to change the point from which the gradient radiates. The application in Listing 14.1 defines two Ellipse shapes. The first uses a radial gradient, and the seconds uses a linear gradient. Each is modified by the focalPointRatio and rotation properties to create a particular visual appearance. LISTING 14.1 Two shapes with gradient fills <?xml version=”1.0” encoding=”utf-8”?> <s:Application xmlns:fx=”http://ns.adobe.com/mxml/2009” xmlns:s=”library://ns.adobe.com/flex/spark” xmlns:mx=”library://ns.adobe.com/flex/mx”> <fx:Style> @namespace s “library://ns.adobe.com/flex/spark”; s|Label { font-size:14; font-weight:bold; } </fx:Style> <s:layout> <s:VerticalLayout gap=”20” horizontalAlign=”center” paddingTop=”20”/> </s:layout> <s:Label text=”Radial Gradient”/> <s:Ellipse width=”200” height=”100” horizontalCenter=”0” verticalCenter=”0” > <s:fill> <s:RadialGradient focalPointRatio=” 1” rotation=”45”> <s:entries> <s:GradientEntry color=”#FFFFFF”/> <s:GradientEntry color=”#000000”/> </s:entries> </s:RadialGradient> </s:fill> <s:stroke> <s:SolidColorStroke color=”#000000” weight=”2”/> </s:stroke> </s:Ellipse> <s:Label text=”Linear Gradient”/> <s:Ellipse width=”200” height=”100” horizontalCenter=”0” verticalCenter=”0”> 20_488959-ch14.indd 42620_488959-ch14.indd 426 3/5/10 2:29 PM3/5/10 2:29 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Chapter 14: Declaring Graphics with MXML and FXG 427 <s:fill> <s:LinearGradient rotation=”45”> <s:entries> <s:GradientEntry color=”#FFFFFF”/> <s:GradientEntry color=”#000000”/> </s:entries> </s:LinearGradient> </s:fill> <s:stroke> <s:SolidColorStroke color=”#000000” weight=”2”/> </s:stroke> </s:Ellipse> </s:Application> On the Web The code in Listing 14.1 is available in the Web site files in the chapter14 project as GradientDemos.mxml. n Figure 14.4 shows visual result: two Ellipse objects with different types of gradient fills. FIGURE 14.4 These ellipse objects use, respectively, a linear and a radial gradient fill. Radial Gradient Linear Gradient The fill property contains an array of GradientEntry objects. Each can be assigned a ratio value from 0 to 1 that determines where in the graphical element the color change occurs. The fol- lowing Path object uses a RadialGradient with many instances of the GradientEntry class. They alternate colors and determine where the colors change in the graphical element with their ratio values: 20_488959-ch14.indd 42720_488959-ch14.indd 427 3/5/10 2:29 PM3/5/10 2:29 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Part II: Designing Flex Applications 428 <s:Path data=”M 0 40 L 40 0 L 105 0 L 144 40 L 144 51 L 105 90 L 40 90 L 0 51 L 0 40 Z “> <s:fill> <s:RadialGradient> <s:GradientEntry color=”#333333” ratio=”0” alpha=”1”/> <s:GradientEntry color=”#CCCCCC” ratio=”0.09” alpha=”1”/> <s:GradientEntry color=”#333333” ratio=”0.37” alpha=”1”/> <s:GradientEntry color=”#CCCCCC” ratio=”0.89” alpha=”1”/> <s:GradientEntry color=”#333333” ratio=”0.99” alpha=”1”/> </s:RadialGradient> </s:fill> </s:Path> Figure 14.5 shows the result: a symmetrical polygon with a series of concentric radial gradients. FIGURE 14.5 A symmetrical polygon with a radial gradient fill defined with primitive vector graphic classes declared in MXML Reusing graphic elements with <fx:Library> and <fx:Definition> The Flex 4 SDK adds a new MXML element named <fx:Library> that you use to define graphic elements that can then be reused anywhere in the same MXML document. Within the <fx:Library> element, you define one or more <fx:Definition> element. Each <fx:Definition> describes a graphic element. Caution The <fx:Library> tag must be placed as the first child element within the MXML document’s root element. If you place any other tags before it, a compiler error is generated. n The application in Listing 14.2 defines a single vector graphic as a library definition. Its name is set as CurvedArrow. In the body of the application, there are three instances of the graphic. Each sets its scaleX and scaleY properties to a different value. The resulting application displays three instances of the arrow. Even though they’re different sizes, they all show clean curves and smooth gradient fills. 20_488959-ch14.indd 42820_488959-ch14.indd 428 3/5/10 2:29 PM3/5/10 2:29 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Chapter 14: Declaring Graphics with MXML and FXG 429 LISTING 14.2 A library definition shape used multiple times <?xml version=”1.0” encoding=”utf-8”?> <s:Application xmlns:fx=”http://ns.adobe.com/mxml/2009” xmlns:s=”library://ns.adobe.com/flex/spark”> <fx:Library> <fx:Definition name=”CurvedArrow”> <s:Path data=”M 20 0 C 50 0 50 35 20 35 L 15 35 L 15 45 L 0 32 L 15 19 L 15 29 L 20 29 C 44 29 44 6 20 6”> <s:stroke> <s:SolidColorStroke color=”0x888888”/> </s:stroke> <s:fill> <s:LinearGradient rotation=”90”> <s:GradientEntry color=”0x000000” alpha=”0.8”/> <s:GradientEntry color=”0xFFFFFF” alpha=”0.8”/> </s:LinearGradient> </s:fill> </s:Path> </fx:Definition> </fx:Library> <s:HGroup horizontalCenter=”0” top=”20”> <fx:CurvedArrow scaleX=”1” scaleY=”1”/> <fx:CurvedArrow scaleX=”2” scaleY=”2”/> <fx:CurvedArrow scaleX=”4” scaleY=”4”/> </s:HGroup> </s:Application> On the Web The code in Listing 14.2 is available in the Web site files in the chapter14 project as ScalingGraphics.mxml. n New Feature The <fx:Library> MXML tag is new in Flex 4.You use it to define reusable graphic elements. Each reusable graphical element is defined in an <fx:Definition> class with a name property. The definition becomes an ActionScript class at compilation time. You can then declare instances of the reusable graphic element by referring to its name as an MXML tag with the fx prefix. For example, in Listing 14.2, the <fx:Library> tag is a direct child of <s:Application>. It contains this definition: 20_488959-ch14.indd 42920_488959-ch14.indd 429 3/5/10 2:29 PM3/5/10 2:29 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Part II: Designing Flex Applications 430 <fx:Definition name=”CurvedArrow”> <s:Path /> </fx:Definition> The resulting CurvedArrow graphical element can then be used anywhere in the visual content of the docu- ment in which the <fx:Library> tag is declared. n Scaling graphic elements An MXML graphic is rendered as a vector graphic; this means that its rendering is calculated math- ematically rather than as a set of pixels (as is the case with bitmap graphics). As a result, you can increase or decrease the size of the graphic (a process known as scaling) without disturbing the graphic’s resolution. When you do the same thing with a bitmap graphic, it’s often pixilated, show- ing jagged edges. With vector graphics, the rendering stays clean and attractive. In Listing 14.2, the graphic that’s defined in the <fx:Library> section is rendered three times, each time with a different value for the object’s scaleY and scaleY properties: <fx:CurvedArrow scaleX=”1” scaleY=”1”/> <fx:CurvedArrow scaleX=”2” scaleY=”2”/> <fx:CurvedArrow scaleX=”4” scaleY =”4”/> Figure 14.6 shows the result, with the three versions of the arrow graphic displayed side by side. Notice that the graphic’s curves are smooth regardless of the scale. This is a benefit of vector graph- ics: because their presentation is calculated mathematically, they can adjust to whatever scale your application needs. FIGURE 14.6 A graphic element displayed in three scales 20_488959-ch14.indd 43020_488959-ch14.indd 430 3/5/10 2:29 PM3/5/10 2:29 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... winding=”nonZero” ai:knockout=”0” data=”M151.206 161.63C 246 .726 2 94. 045 180.796 40 1.39 99 .46 58 245 .871 18.1367 40 1.39 -47 .7939 2 94. 045 47 .7256 161.63 -47 .7939 29.2 144 18.1367 -78.12 94 99 .46 58 77.3901 180.796 -78.12 94 246 .726 29.2 144 151.206 161.63Z” > 43 8 Chapter 14: Declaring Graphics with MXML and FXG . data=”M151.206 161.63C 246 .726 2 94. 045 180.796 40 1.39 99 .46 58 245 .871 18.1367 40 1.39 -47 .7939 2 94. 045 47 .7256 161.63 -47 .7939 29.2 144 18.1367 -78.12 94 99 .46 58 77.3901. data=”M151.206 161.63C 246 .726 2 94. 045 180.796 40 1.39 99 .46 58 245 .871 18.1367 40 1.39 -47 .7939 2 94. 045 47 .7256 161.63 -47 .7939 29.2 144 18.1367 -78.12 94 99 .46 58 77.3901

Ngày đăng: 26/01/2014, 20:20

TỪ KHÓA LIÊN QUAN