当前位置: 代码迷 >> Web前端 >> 一个九宫格肌肤类
  详细解决方案

一个九宫格肌肤类

热度:222   发布时间:2012-10-24 14:15:58.0
一个九宫格皮肤类
这个类是做某个游戏皮肤用到的,功能大致等同于用元数据标签Embed,而且比它还要繁琐一些。

SkinUtil.as
package com.thornbird.utils
{

import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.filters.GlowFilter;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;	

public class SkinUtil
{
	
	public static function drawScaleGrid(g:Graphics, x:Number, y:Number, 
			width:Number, height:Number, bitmapData:BitmapData, 
			scaleGrid:Rectangle = null, repeat:Boolean = false):void
	{
		var w:Number = bitmapData.width;
		var h:Number = bitmapData.height;
		var matrix:Matrix = null;
		if (scaleGrid == null)
		{
			matrix = new Matrix();
			if (!repeat)
				matrix.createBox(width / w, height / h, 0, x, y);
			g.beginBitmapFill(bitmapData, matrix, false, repeat);
			g.drawRect(x, y, width, height);
			g.endFill();
			return;
		}
		var l:Number = scaleGrid.left;
		var r:Number = scaleGrid.right;
		var t:Number = scaleGrid.top;
		var b:Number = scaleGrid.bottom;
		var origin:Point = new Point(0, 0);
		var topLeft:BitmapData = null;
		if (l > 0 && t > 0)
			topLeft = new BitmapData(l, t, true, 0);
		var topRight:BitmapData = null;
		if (w - r > 0 && t > 0)
			topRight = new BitmapData(w - r, t, true, 0);
		var bottomLeft:BitmapData = null;
		if (l > 0 && h - b > 0)
			bottomLeft = new BitmapData(l, h - b,  true, 0);
		var bottomRight:BitmapData = null;
		if (w - r > 0 && h - b > 0)
			bottomRight = new BitmapData(w - r, h - b,  true, 0);
		var left:BitmapData = null;
		if (l > 0 && b - t > 0)
			left = new BitmapData(l, b - t, true, 0);
		var right:BitmapData = null;
		if (w - r > 0 && b - t > 0)
			right = new BitmapData(w - r, b - t, true, 0);
		var top:BitmapData = null;
		if (r - l > 0 && t > 0)
			top = new BitmapData(r - l, t, true, 0);
		var bottom:BitmapData = null;
		if (r - l > 0 && h - b > 0)
			bottom = new BitmapData(r - l, h - b,  true, 0);
		var center:BitmapData = null;
		if (r - l > 0 && b - t > 0)
			center = new BitmapData(r - l, b - t, true, 0);
		var scaleX:Number = 1;
		var scaleY:Number = 1;
		if (topLeft != null)
			topLeft.copyPixels(bitmapData, new Rectangle(0, 0, l, t), origin);
		if (topRight != null)
			topRight.copyPixels(bitmapData, new Rectangle(r, 0, w - r, t), origin);
		if (bottomLeft != null)
			bottomLeft.copyPixels(bitmapData, new Rectangle(0, b, l, h - t), origin);
		if (bottomRight != null)
			bottomRight.copyPixels(bitmapData, new Rectangle(r, b, w - r, h - b), origin);
		if (left != null)
			left.copyPixels(bitmapData, new Rectangle(0, t, l, b - t), origin);
		if (right != null)
			right.copyPixels(bitmapData, new Rectangle(r, t, w - r, b - t), origin);
		if (top != null)
			top.copyPixels(bitmapData, new Rectangle(l, 0, r - l, t), origin);
		if (bottom != null)
			bottom.copyPixels(bitmapData, new Rectangle(l, b, r - l, h - b), origin);
		if (center != null)
			center.copyPixels(bitmapData, new Rectangle(l, t, r - l, b - t), origin);
		matrix = new Matrix();
		if (topLeft != null)
		{
			matrix.createBox(scaleX, scaleY, 0, x, y);
			g.beginBitmapFill(topLeft, matrix, false, true);
			g.drawRect(x, y, l, t);
			g.endFill();
		}
		if (topRight != null)
		{
			matrix.createBox(scaleX, scaleY, 0, x + width - (w - r), y);
			g.beginBitmapFill(topRight, matrix, false, true);
			g.drawRect(x + width - (w - r), y, w - r, t);
			g.endFill();
		}
		if (bottomLeft != null)
		{
			matrix.createBox(scaleX, scaleY, 0, x, y + height - (h - b));
			g.beginBitmapFill(bottomLeft, matrix, false, true);
			g.drawRect(x, y + height - (h - b), l, h - b);
			g.endFill();
		}
		if (bottomRight != null)
		{
			matrix.createBox(scaleX, scaleY, 0, x + width - (w - r), y + height - (h - b));
			g.beginBitmapFill(bottomRight, matrix, false, true);
			g.drawRect(x + width - (w - r), y + height - (h - b), w - r, h - b);
			g.endFill();
		}
		scaleX = 1;
		scaleY = repeat ? 1 : (height - t - (h - b)) / (b - t);
		if (left != null)
		{
			matrix.createBox(scaleX, scaleY, 0, x, y + t);
			g.beginBitmapFill(left, matrix, repeat, true);
			g.drawRect(x, y + t, l, height - t - (h - b));
			g.endFill();
		}
		if (right != null)
		{
			matrix.createBox(scaleX, scaleY, 0, x + width - (w - r), y + t);
			g.beginBitmapFill(right, matrix, repeat, true);
			g.drawRect(x + width - (w - r), y + t, w - r, height - t - (h - b));
			g.endFill();
		}
		scaleX = repeat ? 1 : (width - l - (w - r)) / (r - l);
		scaleY = 1;
		if (top != null)
		{
			matrix.createBox(scaleX, scaleY, 0, x + l, y);
			g.beginBitmapFill(top, matrix, repeat, true);
			g.drawRect(x + l, y, width - l - (w - r), t);
			g.endFill();
		}
		if (bottom != null)
		{
			matrix.createBox(scaleX, scaleY, 0, x + l, y + height - (h - b));
			g.beginBitmapFill(bottom, matrix, repeat, true);
			g.drawRect(x + l, y + height - (h - b), width - l - (w - r), h - b);
			g.endFill();
		}
		scaleX = repeat ? 1 : (width - l - (w - r)) / (r - l);
		scaleY = repeat ? 1 : (height - t - (h - b)) / (b - t);
		if (center != null)
		{
			matrix.createBox(scaleX, scaleY, 0, x + l, y + t);
			g.beginBitmapFill(center, matrix, repeat, true);
			g.drawRect(x + l, y + t, width - l - (w - r), height - t - (h - b));
			g.endFill();
		}
	}
	
	public static function drawOutline(object:Object, color:Number, alpha:Number):void
	{
		if (!(object is DisplayObject) || isNaN(color))
			return;
		if (color < 0x000000 || color > 0xFFFFFF)
			return;
		if (isNaN(alpha))
			alpha = 0.8;
		object.filters = [];
		object.filters = [new GlowFilter(color, alpha, 2, 2, 80)];
	}
	
}

}


这是一个调用示例:
ButtonSkin.as
package com.thornbird.skins.halo
{

import com.thornbird.utils.SkinUtil;

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.filters.GlowFilter;
import flash.geom.Rectangle;

import mx.core.IUITextField;
import mx.core.mx_internal;
import mx.skins.halo.ButtonSkin;

use namespace mx_internal;

public class ButtonSkin extends mx.skins.halo.ButtonSkin
{
	
	[Embed(source="../../../../../assets/images/button_up.png")]
	private static var ButtonUp:Class;
	private static var buttonUpBitmap:Bitmap = null;
	
	[Embed(source="../../../../../assets/images/button_over.png")]
	private static var ButtonOver:Class;
	private static var buttonOverBitmap:Bitmap = null;
	
	[Embed(source="../../../../../assets/images/button_down.png")]
	private static var ButtonDown:Class;
	private static var buttonDownBitmap:Bitmap = null;
	
	[Embed(source="../../../../../assets/images/button_disabled.png")]
	private static var ButtonDisabled:Class;
	private static var buttonDisabledBitmap:Bitmap = null;
	
	protected var drawFocusFlag:Boolean = true;
	
	protected var focusIn:Boolean = false;
	
	override protected function updateDisplayList(w:Number, h:Number):void
	{
		var bitmapData:BitmapData = null;
		switch (name)
		{
			case "selectedUpSkin":
			case "upSkin":
				if (buttonUpBitmap == null)
					buttonUpBitmap = new ButtonUp() as Bitmap;
				bitmapData = buttonUpBitmap.bitmapData;
				break;
			case "selectedOverSkin":
			case "overSkin":
				if (buttonOverBitmap == null)
					buttonOverBitmap = new ButtonOver() as Bitmap;
				bitmapData = buttonOverBitmap.bitmapData;
				break;
			case "selectedDownSkin":
			case "downSkin":
				if (buttonDownBitmap == null)
					buttonDownBitmap = new ButtonDown() as Bitmap;
				bitmapData = buttonDownBitmap.bitmapData;
				break;
			case "selectedDisabledSkin":
			case "disabledSkin":
				if (buttonDisabledBitmap == null)
					buttonDisabledBitmap = new ButtonDisabled() as Bitmap;
				bitmapData = buttonDisabledBitmap.bitmapData;
				break;
		}
		graphics.clear();
		if (bitmapData != null)
			SkinUtil.drawScaleGrid(graphics, 0, 0, w, h, bitmapData, 
					new Rectangle(9, 6, 68, 11));
		if (name.toLowerCase().indexOf("disabled") < 0)
			SkinUtil.drawOutline(getTextField(), getStyle("outlineColor"), 
					getStyle("outlineAlpha"));
	}
	
	protected function getTextField():IUITextField
	{
		var textField:IUITextField = null;
		if (parent)
		{
			for (var i:int = 0; i < parent.numChildren; i++)
			{
				var child:DisplayObject = parent.getChildAt(i);
				if (child is IUITextField)
				{
					textField = child as IUITextField;
					break;
				}
			}
		}
		return textField;
	}
	
}

}
  相关解决方案