flash on 2011-7-19
package
{
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.system.LoaderContext;
public class Main extends Sprite
{
private var gauge:Gauge;
public function Main()
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
loader.load(new URLRequest("http://assets.wonderfl.net/images/related_images/a/a4/a4f3/a4f36e6e6e051dce0864f0d3b90921c9c55f5b1b"), new LoaderContext(true));
}
private function initHandler(event:Event):void
{
graphics.beginFill(0xF0F0F0);
graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
graphics.endFill();
var loader:Loader = event.currentTarget.loader as Loader;
var bd:BitmapData = new BitmapData(loader.width, loader.height, true, 0x0);
bd.draw(loader);
gauge = new Gauge(200, bd);
gauge.y = 50;
scaleX = scaleY = 2.0;
addChild(gauge);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
gauge.value += 0.5;
if (gauge.value >= gauge.maxValue) gauge.value = 0;
}
}
}
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.geom.Matrix;
import flash.geom.Rectangle;
class Gauge extends Sprite
{
private var _value:Number = 50;
private var gaugeBitmap:Bitmap;
public var maxValue:Number = 100;
public var gauge:BitmapData;
public function Gauge(size:Number, bd:BitmapData)
{
var bitmap:ScaleBitmap = new ScaleBitmap(bd.clone());
bitmap.scale9Grid = new Rectangle(2, 0, 1, 6);
bitmap.setSize(size, 6);
addChild(bitmap);
gauge = new BitmapData(size - 4, 2, false, 0xFF0000);
gauge.fillRect(new Rectangle(0, 1, size - 4, 1), 0x7F0000);
gaugeBitmap = new Bitmap(gauge);
gaugeBitmap.x = gaugeBitmap.y = 2;
addChild(gaugeBitmap);
value = 50;
}
public function get value():Number
{
return _value;
}
public function set value(value:Number):void
{
_value = value;
gaugeBitmap.scaleX = _value / maxValue;
}
}
class ScaleBitmap extends Bitmap {
// ------------------------------------------------
//
// ---o properties
//
// ------------------------------------------------
protected var _originalBitmap : BitmapData;
protected var _scale9Grid : Rectangle = null;
// ------------------------------------------------
//
// ---o constructor
//
// ------------------------------------------------
function ScaleBitmap(bmpData : BitmapData = null, pixelSnapping : String = "auto", smoothing : Boolean = false) {
// super constructor
super(bmpData, pixelSnapping, smoothing);
// original bitmap
_originalBitmap = bmpData.clone();
}
// ------------------------------------------------
//
// ---o public methods
//
// ------------------------------------------------
/**
* setter bitmapData
*/
override public function set bitmapData(bmpData : BitmapData) : void {
_originalBitmap = bmpData.clone();
if (_scale9Grid != null) {
if (!validGrid(_scale9Grid)) {
_scale9Grid = null;
}
setSize(bmpData.width, bmpData.height);
} else {
assignBitmapData(_originalBitmap.clone());
}
}
/**
* setter width
*/
override public function set width(w : Number) : void {
if (w != width) {
setSize(w, height);
}
}
/**
* setter height
*/
override public function set height(h : Number) : void {
if (h != height) {
setSize(width, h);
}
}
/**
* set scale9Grid
*/
override public function set scale9Grid(r : Rectangle) : void {
// Check if the given grid is different from the current one
if ((_scale9Grid == null && r != null) || (_scale9Grid != null && !_scale9Grid.equals(r))) {
if (r == null) {
// If deleting scalee9Grid, restore the original bitmap
// then resize it (streched) to the previously set dimensions
var currentWidth : Number = width;
var currentHeight : Number = height;
_scale9Grid = null;
assignBitmapData(_originalBitmap.clone());
setSize(currentWidth, currentHeight);
} else {
if (!validGrid(r)) {
throw (new Error("#001 - The _scale9Grid does not match the original BitmapData"));
return;
}
_scale9Grid = r.clone();
resizeBitmap(width, height);
scaleX = 1;
scaleY = 1;
}
}
}
/**
* assignBitmapData
* Update the effective bitmapData
*/
private function assignBitmapData(bmp : BitmapData) : void {
super.bitmapData.dispose();
super.bitmapData = bmp;
}
private function validGrid(r : Rectangle) : Boolean {
return r.right <= _originalBitmap.width && r.bottom <= _originalBitmap.height;
}
/**
* get scale9Grid
*/
override public function get scale9Grid() : Rectangle {
return _scale9Grid;
}
/**
* setSize
*/
public function setSize(w : Number, h : Number) : void {
if (_scale9Grid == null) {
super.width = w;
super.height = h;
} else {
w = Math.max(w, _originalBitmap.width - _scale9Grid.width);
h = Math.max(h, _originalBitmap.height - _scale9Grid.height);
resizeBitmap(w, h);
}
}
/**
* get original bitmap
*/
public function getOriginalBitmapData() : BitmapData {
return _originalBitmap;
}
// ------------------------------------------------
//
// ---o protected methods
//
// ------------------------------------------------
/**
* resize bitmap
*/
protected function resizeBitmap(w : Number, h : Number) : void {
var bmpData : BitmapData = new BitmapData(w, h, true, 0x00000000);
var rows : Array = [0, _scale9Grid.top, _scale9Grid.bottom, _originalBitmap.height];
var cols : Array = [0, _scale9Grid.left, _scale9Grid.right, _originalBitmap.width];
var dRows : Array = [0, _scale9Grid.top, h - (_originalBitmap.height - _scale9Grid.bottom), h];
var dCols : Array = [0, _scale9Grid.left, w - (_originalBitmap.width - _scale9Grid.right), w];
var origin : Rectangle;
var draw : Rectangle;
var mat : Matrix = new Matrix();
for (var cx : int = 0;cx < 3; cx++) {
for (var cy : int = 0 ;cy < 3; cy++) {
origin = new Rectangle(cols[cx], rows[cy], cols[cx + 1] - cols[cx], rows[cy + 1] - rows[cy]);
draw = new Rectangle(dCols[cx], dRows[cy], dCols[cx + 1] - dCols[cx], dRows[cy + 1] - dRows[cy]);
mat.identity();
mat.a = draw.width / origin.width;
mat.d = draw.height / origin.height;
mat.tx = draw.x - origin.x * mat.a;
mat.ty = draw.y - origin.y * mat.d;
bmpData.draw(_originalBitmap, mat, null, null, draw, smoothing);
}
}
assignBitmapData(bmpData);
}
}