Flex4 使用itemRenderer 为Tree加线具体实现

代码如下:

package Modules
{
import flash.display.BitmapData;
import flash.display.Graphics;

import mx.collections.ICollectionView;
import mx.collections.IList;
import mx.controls.Tree;
import mx.controls.treeClasses.ITreeDataDescriptor;
import mx.controls.treeClasses.TreeItemRenderer;

/**
* Alpha value for the Tree lines.
* @default 1
*/
[Style(name="lineAlpha", type="Number", format="Length", inherit="no")]

/**
* Color of the Tree lines.
* @default 0x808080
*/
[Style(name="lineColor", type="uint", format="Color", inherit="no")]

/**
* Thickness value for the Tree lines.
* @default 1
*/
[Style(name="lineThickness", type="Number", format="Length", inherit="no")]

/**
* The Tree line style - none, dotted (default), or solid.
* @default "dotted"
*/
[Style(name="lineStyle", type="String", enumeration="solid", inherit="no")]

/**
* Extends TreeItemRenderer to draw the dotted lines of the tree.
* It supports 3 line styles - dotted (default), solid, or none.
* And the tree line color, alpha, and thickness values are configurable styles too.
*
* <pre>
* <ui:TreeItemLinesRenderer
* <b>Styles</b>
* lineAlpha="1"
* lineColor="#808080"
* lineThickness="1"
* lineStyle="dotted"
* >
* ...
* <i>child tags</i>
* ...
* </ui:TreeItemLinesRenderer>
* </pre>
*
* @author Chris Callendar
* @date April 20th, 2009
*/
public class TreeItemLinesRenderer extends TreeItemRenderer
{

public static const DOTTED:String = "dotted"; // default
public static const SOLID:String = "solid";
public static const NONE:String = "none";

public function TreeItemLinesRenderer() {
super();
}

override public function set data(value:Object):void {
super.data = value;
}

override protected function updateDisplayList(w:Number, h:Number):void {
super.updateDisplayList(w, h);

if ((w > 0) && (h > 0)) {
// go up the hierarchy, drawing the vertical dotted lines for each node
var tree:Tree = (owner as Tree);
var desc:ITreeDataDescriptor = tree.dataDescriptor;
var currentNode:Object = data;
var parentNode:Object = tree.getParentItem(currentNode);
// the level is zero at this node, then increases as we go up the tree
var levelsUp:int = 0;

var lineStyle:String = getStyle("solid");
var lineColor:uint = getColorStyle("lineColor", 0x808080);
var lineAlpha:Number = getNumberStyle("lineAlpha", 1);
var lineThickness:Number = getNumberStyle("lineThickness", 1);
var indentation:Number = tree.getStyle("indentation");

// move the icon and label over to make room for the lines (less for root nodes)
var shift:int = (parentNode == null ? 2 : 6) + lineThickness;
if (icon) {
icon.move(icon.x + shift, icon.y);
}
if (label) {
label.move(label.x + shift, label.y);
}

var g:Graphics = graphics;
g.clear();

if ((lineStyle != NONE) && (lineAlpha > 0) && (lineThickness > 0)) {
while (parentNode != null) {
var children:ICollectionView = desc.getChildren(parentNode);
if (children is IList) {
var itemIndex:int = (children as IList).getItemIndex(currentNode);
// if this node is the last child of the parent
var isLast:Boolean = (itemIndex == (children.length - 1));
drawLines(g, w, h, lineStyle, lineColor, lineAlpha, lineThickness, isLast, levelsUp, indentation);

// go up to the parent, increasing the level
levelsUp++;
currentNode = parentNode;
parentNode = tree.getParentItem(parentNode);
} else {
break;
}
}
}
}
}

protected function drawLines(g:Graphics, w:Number, h:Number, lineStyle:String, lineColor:uint,
lineAlpha:Number, lineThickness:Number, isLastItem:Boolean, levelsUp:int, indentation:Number):void {
var midY:Number = Math.round(h / 2);
var lineX:Number = 0;
if (disclosureIcon) {
lineX = disclosureIcon.x + (disclosureIcon.width / 2);
} else if (icon) {
lineX = icon.x - 8;
} else if (label) {
lineX = label.x - 8;
}
lineX = Math.floor(lineX) - int(lineThickness / 2);
// adjust the x position based on the indentation
if (levelsUp > 0) {
if (!isNaN(indentation) && (indentation > 0)) {
lineX = lineX - (levelsUp * indentation);
} else {
// Invalid indentation style value
return;
}
}
var lineY:Number = h;
// stop the dotted line halfway on the last item
if (isLastItem) {
lineY = midY;
// no lines need to be drawn for parents of the last item
if (levelsUp > 0) {
return;
}
}

g.lineStyle(0, 0, 0);
if (lineStyle == SOLID) {
g.beginFill(lineColor, lineAlpha);
} else {
var verticalDottedLine:BitmapData = createDottedLine(lineColor, lineAlpha, lineThickness, true);
g.beginBitmapFill(verticalDottedLine);
}

// draw the vertical line
g.drawRect(lineX, 0, lineThickness, lineY);
// end the fill and start it again otherwise the lines overlap and it create white squares
g.endFill();

// draw the horizontal line - only needed on this node (not on any parents)
if (levelsUp == 0) {
var startX:int = lineX + 1 + int(lineThickness / 2);
var endX:int = startX + 11; // 5 dots
if (isLastItem) {
startX = lineX;
}
var startY:Number = midY - int(lineThickness / 2);
if (lineStyle == SOLID) {
g.beginFill(lineColor, lineAlpha);
} else {
var horizontalDottedLine:BitmapData = createDottedLine(lineColor, lineAlpha, lineThickness, false);
g.beginBitmapFill(horizontalDottedLine);
}
g.drawRect(startX, startY, endX - startX, lineThickness);
g.endFill();
}

}

/**
* Creates a BitmapData that is used to renderer a dotted line.
* If the vertical parameter is true, then it creates a rectangle bitmap that is
* twice as long as it is wide (lineThickness). Otherwise it creates a rectangle
* that is twice as wide as it is long.
* The first half of the rectangle is filled with the line color (and alpha value),
* then second half is transparent.
*/
private function createDottedLine(lineColor:uint, lineAlpha:Number, lineThickness:Number,
vertical:Boolean = true):BitmapData {
var w:Number = (vertical ? lineThickness : 2 * lineThickness);
var h:Number = (vertical ? 2 * lineThickness : lineThickness);
var color32:uint = combineColorAndAlpha(lineColor, lineAlpha);
var dottedLine:BitmapData = new BitmapData(w, h, true, 0x00ffffff);
// create a dotted bitmap
for (var i:int = 0; i < lineThickness; i++) {
for (var j:int = 0; j < lineThickness; j++) {
dottedLine.setPixel32(i, j, color32);
}
}
return dottedLine;
}

/**
* Combines the color value and the alpha value into a 32 bit uint like #AARRGGBB.
*/
private function combineColorAndAlpha(color:uint, alpha:Number):uint {
// make sure the alpha is a valid number [0-1]
if (isNaN(alpha)) {
alpha = 1;
} else {
alpha = Math.max(0, Math.min(1, alpha));
}

// convert the [0-1] alpha value into [0-255]
var alphaColor:Number = alpha * 255;
// bitshift it to come before the color
alphaColor = alphaColor << 24;
// combine the two values: #AARRGGBB
var combined:uint = alphaColor | color;
return combined;
}

private function getColorStyle(propName:String, defaultValue:uint):uint {
var color:uint = defaultValue;
if (propName != null) {
var n:Number = getStyle(propName);
if (!isNaN(n)) {
color = uint(n);
}
}
return color;
}

private function getNumberStyle(propName:String, defaultValue:Number):Number {
var number:Number = defaultValue;
if (propName != null) {
var n:Number = getStyle(propName);
if (!isNaN(n)) {
number = n;
}
}
return number;
}

}
}

(0)

相关推荐

  • Flex4 使用itemRenderer 为Tree加线具体实现

    复制代码 代码如下: package Modules { import flash.display.BitmapData; import flash.display.Graphics; import mx.collections.ICollectionView; import mx.collections.IList; import mx.controls.Tree; import mx.controls.treeClasses.ITreeDataDescriptor; import mx.co

  • Flex tree加虚线显示效果并且替代原始图标

    一.Flex tree修改默认图标,并且加虚线显示效果图如下:  实在是看不下去那种巨丑无比的小箭头+文件夹的显示方式,就从网上down了一份加虚线的源码,就是重写Tree的ItemRender,网上很多,不在啰嗦,下面就是去掉 复制代码 代码如下: mx|Tree { folderOpenIcon:ClassReference(null); folderClosedIcon:ClassReference(null); } 至于为什吗去掉这个样式,狗日的Flex有两个默认图标,如果你发现你有类似

  • DEDE隔行变色以及分组加线修改方法

    打开:inc_channel_unit_functions.php 加入下面一段  复制代码 代码如下: //zouql //$f 列表总数 //$t 每组列表数 function MyList($f,$t,$meid,$con) { if($meid%$t ==0 && $meid < $f) return $con; } 使用举例:  复制代码 代码如下: <ul> {dede:arclist row=20 titlelen=80 orderby=pubdate} &

  • Extjs中通过Tree加载右侧TabPanel具体实现

    最近在做一个物流管理的项目,公司必须要求用Extjs4.1来做界面,因为以前一直也没有接触过所以开发的过程中遇到了很多的困难.同时Extjs4.1的资料在网上也相对来说较少.好了,不说废话上代码: 1.左侧的功能树 复制代码 代码如下: Ext.define("AM.view.SystemTree", { extend : 'Ext.tree.Panel', alias : 'widget.systemTree', rootVisible : false,// 不展示ROOT disp

  • 详解angular笔记路由之angular-router

    本文介绍了angular笔记路由之angular-router,分享给大家,具体如下: 创建项目 ng new router --routing \\ 加routing参数 \\ 会新增一个app-routing.module.ts 文件 路由的基本使用 名称 简介 Routes 路由的配置,保存着哪一个URL对应展示哪一个组件和在哪一个RouterOutler展示 RouterOutler 在HTML中标记路由内容呈现的占位符指令 Router 运行时执行的路由对象,可以通过navigate(

  • 浅析document.ready和window.onload的区别讲解

    Javascript 只有在DOM元素已经定义以后才可以对其执行某种操作,这个问题在"javascript执行顺序里"http://www.jb51.net/article/44577.htm一文里有详细介绍. jQuery使用document.ready来保证所要执行的代码是在DOM元素被加载完成的情况下执行.比如在"jQuery基础 - 如何开始"一文里,我用到如下jQuery代码: 复制代码 代码如下: <!--   $(document).ready(

  • jquery中的$(document).ready()使用小结

    window.onload = function(){ alert("welcome"); }这样的写法作用是希望在页面加载完,自动执行定义js代码(function). $(document).ready(function(){.... })这个函数是用来取代页面中的window.onload; document.ready()和传统的方法<body onload="load()"> 相似,不同的是onload()的方法是在页面加载完成后才发生,这包括

  • php 文章调用类代码

    调用方法如下: 复制代码 代码如下: $Template= '<li class="xxx">[<a href="{catedir}">{catetitle}</a>]<a href="{html}" title="{title}" >{title|6}{title2|20}</a>点击数:{hits} 日期:{posttime|H:i:s}</li>';

  • Android定制自己的EditText轻松改变底线颜色

    最近做 android 项目遇到这个问题,为了保持 app 风格一致,需要将原生的EditText底线颜色改成橙色.网上搜了一些解决方案,特此记录总结一下. 效果图 默认的EditText底线颜色是蓝色的, 我们想实现橙色的效果 实现方法 1.准备两个背景图 一个作为 edittext 的默认背景 , 另一个作为 输入时候的背景 Note 使用 9.png, 不要用png, 否则图片会模糊, 花掉 在文件夹 drawable 用selector 建立一个xml 文件 <!-- drawable/

  • Android自定义ImageView实现在图片上添加图层效果

    首先我们先看下效果图 实现思路 这是两张前后对比图,右边第二张图里面的已抢光标签图片当已经没有商品的时候就会显示了,在每个图片的中心位置,第一想法是在ImageView的外层再套一层RelativeLayout 实现方法 <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <SelectableRoundedImageVi

随机推荐