Flex 给容器设置背景渐变
时间:2011-06-03 来源:水手◎辛巴德
Flex和Severlight在一些UI方面,确实差很多,需要大大改进。目前,flex除了application支持背景渐变色外,其它控件都不支持,近日需要这个功能,在网上搜到些资料整理如下:
类文件GradientBorder.as:
View Code/*
=========== 背景渐变色
*/
package {
import flash.display.*;
import flash.geom.*;
import flash.utils.*;
import mx.core.EdgeMetrics;
import mx.skins.halo.HaloBorder;
import mx.utils.ColorUtil;
import mx.utils.GraphicsUtil;
public class GradientBorder extends HaloBorder
{
private var topCornerRadius:Number; // top corner radius
private var bottomCornerRadius:Number; // bottom corner radius
private var fillColors:Array; // fill colors (two)
private var setup:Boolean;
private function setupStyles():void
{
fillColors = getStyle("fillColors") as Array;
if (!fillColors) fillColors = [0xFFFFFF, 0xFFFFFF];
topCornerRadius = getStyle("cornerRadius") as Number;
if (!topCornerRadius) topCornerRadius = 0;
bottomCornerRadius = getStyle("bottomCornerRadius") as Number;
if (!bottomCornerRadius) bottomCornerRadius = topCornerRadius;
}
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
setupStyles();
var g:Graphics = graphics;
var b:EdgeMetrics = borderMetrics;
var w:Number = unscaledWidth - b.left - b.right;
var h:Number = unscaledHeight - b.top - b.bottom;
var m:Matrix = verticalGradientMatrix(0, 0, w, h);
g.beginGradientFill("linear", fillColors, [1, 1], [0, 255], m);
var tr:Number = Math.max(topCornerRadius-2, 0);
var br:Number = Math.max(bottomCornerRadius-2, 0);
GraphicsUtil.drawRoundRectComplex(g, b.left, b.top, w, h, tr, tr, br, br);
g.endFill();
}
}
}
调用示例:tester.mxml:
View Code1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
3 layout="vertical" backgroundImage="" backgroundColor="white">
4
5 <mx:Style>
6
7 .gradient
8 {
9 border-style: solid;
10 border-thickness: 0;
11 border-skin: ClassReference("GradientBorder");
12 fill-colors: #0099FF, #000099;
13 corner-radius: 10;
14 drop-shadow-enabled: true;
15 }
16
17 </mx:Style>
18
19 <mx:Script>
20 <![CDATA[
21 private function changeStyle():void
22 {
23 box.setStyle("fillColors", [col1.value, col2.value]);
24 box.setStyle("cornerRadius", corner.value);
25 }
26 ]]>
27 </mx:Script>
28
29 <mx:VBox id="box" styleName="gradient" width="400" height="300" verticalAlign="middle" horizontalAlign="center">
30 <mx:FormItem label="Color 1:">
31 <mx:ColorPicker id="col1" change="changeStyle()" selectedColor="0x0099FF"/>
32 </mx:FormItem>
33 <mx:FormItem label="Color 2:">
34 <mx:ColorPicker id="col2" change="changeStyle()" selectedColor="0x000099"/>
35 </mx:FormItem>
36 <mx:FormItem label="Corner radius:">
37 <mx:HSlider id="corner" value="10" minimum="0" maximum="100" change="changeStyle()"/>
38 </mx:FormItem>
39 </mx:VBox>
40
41 </mx:Application>
相关阅读 更多 +