AS3对外部加载的位图进行九宫格缩放

      Flash习作 2011-3-19 0:43:00

 package { 

import flash.geom.Point; 
import flash.geom.Rectangle; 
import flash.net.URLRequest; 
import flash.events.Event; 
import flash.display.Loader; 
import flash.display.BitmapData; 
import flash.display.Bitmap; 
import flash.display.Sprite; 
 
/** * @author jh7086 */ 
 
[SWF (width="1000", height="800", backgroundColor="0xffffff")] 
 
public class BitmapTest extends Sprite { 
private var bitmap : Bitmap; 
private var loader : Loader; 
private var source : BitmapData; 
 
private var tile : Sprite;
 
private var bound : Rectangle;
private var grid9 : Rectangle;
 
private var tbound : Rectangle;
private var tgrid9 : Rectangle;
 
public function BitmapTest() { 
JH.print("Start>>>>>>>>>>"); 
bitmap = new Bitmap(); 
addChild(bitmap); 
loader = new Loader(); 
loader.contentLoaderInfo.addEventListener(Event.INIT, loader_complete); 
loader.load(new URLRequest("dlgFinger.png")); 
addChild(loader); 
bound = new Rectangle(0, 0, 200, 100);
grid9 = new Rectangle();
grid9.left = bound.left+15;
grid9.top = bound.top+40;
grid9.right = bound.right-15;
grid9.bottom = bound.bottom-20;
tbound = new Rectangle(0, 0, 500, 500);
tgrid9 = new Rectangle();
tgrid9.left=tbound.left + (grid9.left - bound.left);
tgrid9.top=tbound.top + (grid9.top-bound.left);
tgrid9.right=tbound.right - (bound.right - grid9.right);
tgrid9.bottom=tbound.bottom - (bound.bottom - grid9.bottom);
tile = new Sprite;
tile.graphics.beginFill(0xff0000, 0.2);
tile.graphics.drawRect(grid9.x, grid9.y, grid9.width, grid9.height);
//tile.graphics.beginFill(0x00ff00, 0.2);
//tile.graphics.drawRect(tgrid9.x, tgrid9.y+100, tgrid9.width, tgrid9.height);
addChild(tile);
}
 
private function loader_complete(event : Event) : void { 
var x : int;
var y : int;
var w : int;
var h : int;
var k:int;
var d:int;
var i:int;
var bd : BitmapData;
source = Bitmap(loader.content).bitmapData;
bd = new BitmapData(tbound.width, tbound.height, true, 0x66ff0000); 
//左上,原图
x = bound.left;
y = bound.top;
w = grid9.left - bound.left;
h = grid9.top - bound.top;
bd.copyPixels(source, new Rectangle(x, y, w, h), new Point(tbound.left, tbound.top));
//左,垂直拉伸 
k = tgrid9.height / grid9.height;
d = tgrid9.height % grid9.height;
x = bound.left;
y = grid9.top;
w = grid9.left - bound.left;
h = grid9.height;
for(i=0;i<k;i++){
bd.copyPixels(source, new Rectangle(x, y, w, h), new Point(tbound.left,tgrid9.top + i * h));
}
bd.copyPixels(source, new Rectangle(x, y, w, d), new Point(tbound.left, tgrid9.top + i * h));
//左下,原图
x = bound.left;
y = grid9.bottom;
w = grid9.left - bound.left;
h = bound.bottom - grid9.bottom;
bd.copyPixels(source, new Rectangle(x, y, w, h), new Point(tbound.left, tgrid9.bottom));
//下,水平拉伸
k = tgrid9.width / grid9.width;
d = tgrid9.width % grid9.width;
x = grid9.left;
y = grid9.bottom;
w = grid9.width;
h = bound.bottom - grid9.bottom;
for(i=0;i<k;i++){
bd.copyPixels(source, new Rectangle(x, y, w, h), new Point(tgrid9.left + i * w, tgrid9.bottom));
}
bd.copyPixels(source, new Rectangle(x, y, d, h), new Point(tgrid9.left + i * w, tgrid9.bottom));
//右下,原图
x = grid9.right;
y = grid9.bottom;
w = bound.right - grid9.right;
h = bound.bottom - grid9.bottom;
bd.copyPixels(source, new Rectangle(x, y, w, h), new Point(tgrid9.right, tgrid9.bottom));
//右,垂直拉伸
k = tgrid9.height / grid9.height;
d = tgrid9.height % grid9.height;
x = grid9.right;
y = grid9.top;
w = bound.right-grid9.right;
h = grid9.height;
for(i=0;i<k;i++){
bd.copyPixels(source, new Rectangle(x, y, w, h), new Point(tgrid9.right, tgrid9.top + i * h));
}
bd.copyPixels(source, new Rectangle(x, y, w, d), new Point(tgrid9.right, tgrid9.top + i * h));
//右上,原图
x = grid9.right;
y = bound.top;
w = bound.right - grid9.right;
h = grid9.top - bound.top;
bd.copyPixels(source, new Rectangle(x, y, w, h), new Point(tgrid9.right, tbound.top));
//上,水平拉伸
k = tgrid9.width / grid9.width;
d = tgrid9.width % grid9.width;
x = grid9.left;
y = bound.top;
w = grid9.width;
h = grid9.top - bound.top;
for(i=0;i<k;i++){
bd.copyPixels(source, new Rectangle(x, y, w, h), new Point(tgrid9.left+i*w, tbound.top));
}
bd.copyPixels(source, new Rectangle(x, y, d, h), new Point(tgrid9.left+i*w, tbound.top));
//中间,垂直拉伸,水平拉伸
k = tgrid9.width / grid9.width;
d = tgrid9.width % grid9.width;
x = grid9.left;
y = grid9.top;
w = grid9.width;
h = Math.min(grid9.height,tgrid9.height);
for(i=0;i<k;i++){
bd.copyPixels(source, new Rectangle(x, y, w, h), new Point(tgrid9.left + i * w, tgrid9.top));
}
bd.copyPixels(source, new Rectangle(x, y, d, h), new Point(tgrid9.left + i * w, tgrid9.top));
k = tgrid9.height / grid9.height;
d = tgrid9.height % grid9.height;
x = tgrid9.left;
y = tgrid9.top;
w = tgrid9.width;
h = grid9.height;
for(i=1;i<k;i++){
bd.copyPixels(bd, new Rectangle(x, y, w, h), new Point(tgrid9.left, tgrid9.top + i * h));
}
bd.copyPixels(bd, new Rectangle(x, y, w, d), new Point(tgrid9.left, tgrid9.top + i * h));
//
bitmap.bitmapData=bd;
bitmap.y = source.height + 1; 
JH.print(source.width, source.height);
JH.print(bitmap.width, bitmap.height); 
标签集:TAGS:AS3 位图 九宫格缩放
回复Comments() 点击Count()
喜欢就顶一下

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}