位图的九宫格(flash用)
时间:2009-03-09
来源:互联网
最近做游戏,发现flash里位图是无法使用scale9Grid这个属性,,,没办法,只好自己写了一个,,,原理很简单,,用程序把位图切成九块,,装里一个Sprite里,然后重写Sprite的width和height这两个方法,根据改变大小来重新设置位图的各个位置…这样就实现了位图的不变形缩放….
ps:近期我会把以前做的一些小东西的源码放出来,供大家学习使用…
严重声明,,,
发现好多人都不知道这个问题,,用FLASH编译,位图使用scale9Grid也没用,
代码:
/*** @project dynasty
* @author 郭晟 or 猪哥靓
* @copyright
* @document
* @history
* create 2009-2-20
* 位图Scale9Grid功能
**/
package
{
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;
public class BitmapScale9Grid extends Sprite
{
private var source : Bitmap;
private var scaleGridTop : Number;
private var scaleGridBottom : Number;
private var scaleGridLeft : Number;
private var scaleGridRight : Number;
private var leftUp : Bitmap;
private var leftCenter : Bitmap;
private var leftBottom : Bitmap;
private var centerUp : Bitmap;
private var center : Bitmap;
private var centerBottom : Bitmap;
private var rightUp : Bitmap;
private var rightCenter : Bitmap;
private var rightBottom : Bitmap;
private var _width : Number;
private var _height : Number;
private var minWidth : Number;
private var minHeight : Number;
public function BitmapScale9Grid(source:Bitmap, scaleGridTop:Number, scaleGridBottom:Number, scaleGridLeft:Number, scaleGridRight:Number )
{
this.source = source;
this.scaleGridTop = scaleGridTop;
this.scaleGridBottom = scaleGridBottom;
this.scaleGridLeft = scaleGridLeft;
this.scaleGridRight = scaleGridRight;
init();
}
private function init() : void {
_width = source.width;
_height = source.height;
leftUp = getBitmap(0, 0, scaleGridLeft, scaleGridTop);
this.addChild(leftUp);
leftCenter = getBitmap(0, scaleGridTop, scaleGridLeft, scaleGridBottom - scaleGridTop);
this.addChild(leftCenter);
leftBottom = getBitmap(0, scaleGridBottom, scaleGridLeft, source.height - scaleGridBottom);
this.addChild(leftBottom);
centerUp = getBitmap(scaleGridLeft, 0, scaleGridRight - scaleGridLeft, scaleGridTop);
this.addChild(centerUp);
center = getBitmap(scaleGridLeft, scaleGridTop, scaleGridRight - scaleGridLeft, scaleGridBottom - scaleGridTop);
this.addChild(center);
centerBottom = getBitmap(scaleGridLeft, scaleGridBottom, scaleGridRight - scaleGridLeft, source.height - scaleGridBottom);
this.addChild(centerBottom);
rightUp = getBitmap(scaleGridRight, 0, source.width - scaleGridRight, scaleGridTop);
this.addChild(rightUp);
rightCenter = getBitmap(scaleGridRight, scaleGridTop, source.width - scaleGridRight, scaleGridBottom - scaleGridTop);
this.addChild(rightCenter);
rightBottom = getBitmap(scaleGridRight, scaleGridBottom, source.width - scaleGridRight, source.height - scaleGridBottom);
this.addChild(rightBottom);
minWidth = leftUp.width + rightBottom.width;
minHeight = leftBottom.height + rightBottom.height;
}
private function getBitmap(x:Number, y:Number, w:Number, h:Number) : Bitmap {
var bit:BitmapData = new BitmapData(w, h);
bit.copyPixels(source.bitmapData, new Rectangle(x, y, w, h), new Point(0, 0));
var bitMap:Bitmap = new Bitmap(bit);
bitMap.x = x;
bitMap.y = y;
return bitMap;
}
override public function set width(w : Number) : void {
if(w < minWidth) {
w = minWidth;
}
_width = w;
refurbishSize();
}
override public function set height(h : Number) : void {
if(h < minHeight) {
h = minHeight;
}
_height = h;
refurbishSize();
}
private function refurbishSize() : void {
leftCenter.height = _height - leftUp.height - leftBottom.height;
leftBottom.y = _height - leftBottom.height;
centerUp.width = _width - leftUp.width - rightUp.width;
center.width = centerUp.width;
center.height = leftCenter.height;
centerBottom.width = center.width;
centerBottom.y = leftBottom.y;
rightUp.x = _width - rightUp.width;
rightCenter.x = rightUp.x;
rightCenter.height = center.height;
rightBottom.x = rightUp.x;
rightBottom.y = leftBottom.y;
}
}
}
附件

2009-3-9 13:15, 下载次数: 479
演示

2009-3-9 13:15, 下载次数: 183
源码
作者: wnosidw 发布时间: 2009-03-09

作者: cc1007 发布时间: 2009-03-09

作者: byd168 发布时间: 2009-03-10
作者: zhang3968 发布时间: 2009-03-12
作者: loversnie 发布时间: 2009-03-12
作者: jinyuz 发布时间: 2009-03-12

作者: k5angle 发布时间: 2009-03-12
刚想夸奖楼主,可是我下一个网页却搜到了另外的结果:
在Flash里不是不可以,只是跟在Flex里方式不一样而已。
AS3里,显示对象拥有 scale9Grid 这个属性,就是他了。
作者: Jubu 发布时间: 2009-05-05
作者: HBrO 发布时间: 2009-05-05
这样也行吗?
作者: enc0717 发布时间: 2009-05-06
这只是一个很简单的类,只要最基础的功能,,很多功能都需要去扩展...
[ 本帖最后由 wnosidw 于 2009-5-11 11:15 编辑 ]
作者: wnosidw 发布时间: 2009-05-11
作者: shutaocg 发布时间: 2009-05-15
作者: South123 发布时间: 2009-05-15
作者: bfsjj 发布时间: 2009-05-15
作者: menuhinlove 发布时间: 2009-05-15
作者: yaydaniel 发布时间: 2010-02-02
作者: guanlan10 发布时间: 2010-06-11
附件

2010-6-12 10:17, 下载次数: 24
作者: yaonai2003 发布时间: 2010-06-12
用了楼主的扩展再加上flash ide本身的九宫格操作:让不支持9宫格的模式支持了9宫格
任何元件基于此类就不必写什么代码, 直接支持9宫格(前提,仅内置一个显示对象,且不需要命名)
package {
import engine.BitmapScale9Grid;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.geom.Rectangle;
/**
* ...
* @author Zszen John
*/
public class Sprite9GridScaleWithBmp extends Sprite {
private var display

private var grid9scale:BitmapScale9Grid
private var bmp:Bitmap
public function Sprite9GridScaleWithBmp() {
display = removeChildAt(0) as DisplayObject
var bd:BitmapData = new BitmapData(display.width,display.height,true,0)
bmp = new Bitmap(bd)
bd.draw(display)
display = null
var grid:Rectangle = scale9Grid
grid9scale = new BitmapScale9Grid(bmp, grid.top, grid.bottom, grid.left, grid.right)
addChild(grid9scale)
}
public function destroy():void {
bmp.bitmapData.dispose()
bmp.bitmapData = null
bmp = null
removeChild(grid9scale)
scale9Grid = null
}
override public function get width():Number { return grid9scale.width; }
override public function set width(value:Number):void {
grid9scale.width = value;
}
override public function get height():Number { return grid9scale.height; }
override public function set height(value:Number):void {
grid9scale.height = value;
}
}
}
作者: zszen 发布时间: 2010-10-28
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28