android 9-patch tool
/**
* Copyright 9re ( http://wonderfl.net/user/9re )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/dVL9
*/
package {
import flash.display.BitmapDataChannel;
import flash.geom.Point;
import flash.net.FileReference;
import flash.ui.Mouse;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.display.Bitmap;
import flash.geom.Matrix;
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.display.Shape;
import flash.display.Graphics;
import flash.display.Sprite;
import mx.graphics.codec.PNGEncoder;
public class FlashTest extends Sprite {
public function FlashTest() {
graphics.beginFill(0xff00000);
graphics.drawRect(0, 0, 465, 465);
var scale:Number = 1.5;
const R:int = 12;
var off:Number = 3;
var bm:Bitmap = new Bitmap(makeComponent());
bm.x = bm.y = 100;
addChild(bm);
var mdpi:Sprite = makeButton('mdpi');
addChild(mdpi);
var hdpi:Sprite = makeButton('hdpi');
hdpi.x = 100;
addChild(hdpi);
mdpi.addEventListener(MouseEvent.CLICK, onClick);
hdpi.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void {
scale = e.currentTarget === mdpi ? 1.0 : 1.5;
var encoder:PNGEncoder = new PNGEncoder;
bm.bitmapData = makeComponent();
if (e) {
new FileReference().save(encoder.encode(bm.bitmapData), "white_frame.9.png");
}
}
function makeComponent():BitmapData {
var bg:Sprite = new Sprite;
var w:int;
var h:int;
var sp:Sprite = new Sprite;
sp.cacheAsBitmap = true;
sp.graphics.beginFill(0, 0.3);
sp.graphics.drawRoundRect(off * scale, off * scale, 20 * scale, 20 * scale, R * scale, R * scale);
sp.graphics.endFill();
var bmMask:Bitmap;
var bmdMask:BitmapData;
var spMask:Shape = new Shape;
spMask.cacheAsBitmap = true;
spMask.graphics.beginFill(0);
spMask.graphics.drawRect(0, 0, (20 + off) * scale, (20 + off) * scale);
spMask.graphics.endFill();
spMask.graphics.beginFill(0xffffff);
spMask.graphics.drawRoundRect(off * scale, off * scale, 20 * scale, 20 * scale, R * scale, R * scale);
spMask.graphics.endFill();
spMask.graphics.beginFill(0);
spMask.graphics.drawRoundRect(0, 0, 20 * scale, 20 * scale, R * scale, R * scale);
spMask.graphics.endFill();
bmdMask = new BitmapData(spMask.width, spMask.width, true, 0);
bmdMask.draw(spMask);
bmMask = new Bitmap(bmdMask);
bmdMask.threshold(bmdMask, bmdMask.rect, new Point, "<=", 0xff666666);
var bmdShadow:BitmapData = bmdMask.clone();
bmdShadow.fillRect(bmdMask.rect, 0);
bmdShadow.draw(sp);
w = bmdShadow.width;
h = bmdShadow.height;
bmdShadow.lock();
for (var u:int = 0; u < w; ++u) {
for (var v:int = 0; v < h; ++v) {
var destA:uint = bmdShadow.getPixel32(u, v) >> 24;
var srcA:uint = bmdMask.getPixel32(u, v) >> 24;
if (srcA < 10) {
bmdShadow.setPixel32(u, v, 0);
}
}
}
bmdShadow.unlock();
bg.addChild(new Bitmap(bmdShadow));
//sp.mask = bmMask;
//bg.addChild(bmMask);
var sp2:Shape = new Shape;
drawWhiteRect(sp2.graphics);
function drawWhiteRect(g:Graphics):void {
g.beginFill(0xffffff, 0.75);
g.drawRoundRect(0, 0, 20 * scale, 20 * scale, R * scale, R * scale);
g.endFill();
}
sp.x = sp.y = 1;
bmMask.x = bmMask.y = 1;
sp2.x = sp2.y = 1;
//bg.addChild(sp);
bg.addChild(sp2);
var M:Number = (20 - R - off) * scale;
var L:Number = (off + R / 2) * scale;
bg.graphics.lineStyle(1);
bg.graphics.moveTo(L, 0);
bg.graphics.lineTo(L + M, 0);
bg.graphics.moveTo(L, bmdShadow.height);
bg.graphics.lineTo(L + M, bmdShadow.height);
bg.graphics.moveTo(0, L);
bg.graphics.lineTo(0, L + M);
bg.graphics.moveTo(bmdShadow.width, L);
bg.graphics.lineTo(bmdShadow.width, L + M);
var bd:BitmapData = new BitmapData(bmdShadow.width + 1, bmdShadow.height + 1, true, 0x00ffffff);
bd.draw(bg, null, null, null, null, true);
// clear boundary bits
w = bd.width;
h = bd.height;
var i:int;
for (i = 0; i < w; ++i) {
var a:uint = bd.getPixel32(i, 0) >> 24;
a = a > 240 ? 0xff000000 : 0;
bd.setPixel32(i, 0, a);
a = bd.getPixel32(i, h - 1) >> 24;
a = a > 240 ? 0xff000000 : 0;
bd.setPixel32(i, h - 1, a);
}
for (i = 0; i < h; ++i) {
a = bd.getPixel32(0, i) >> 24;
a = a > 240 ? 0xff000000 : 0;
bd.setPixel32(0, i, a);
a = bd.getPixel32(h - 1, i) >> 24;
a = a > 240 ? 0xff000000 : 0;
bd.setPixel32(h - 1, i, a);
}
return bd;
}
function makeButton(label:String):Sprite {
var sp:Sprite = new Sprite;
sp.mouseChildren = false;
var tf:TextField = new TextField;
tf.mouseEnabled = false;
tf.background = true;
tf.backgroundColor = 0xff0000;
tf.textColor = 0xffffff;
tf.text = label;
tf.width = tf.textWidth + 4;
tf.height = tf.textHeight + 4;
sp.addChild(tf);
sp.buttonMode = true;
sp.tabEnabled = false;
return sp;
}
}
}
}