Created
June 1, 2013 15:20
-
-
Save lynxerzhang/5690727 to your computer and use it in GitHub Desktop.
利用Graphics的readGraphicsData方法绘制图形
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
import flash.display.Bitmap; | |
import flash.display.BitmapData; | |
import flash.display.DisplayObject; | |
import flash.display.Graphics; | |
import flash.display.IGraphicsData; | |
import flash.display.MovieClip; | |
import flash.display.Shape; | |
import flash.display.Sprite; | |
public class GraphicsDrawClip extends Sprite | |
{ | |
public function GraphicsDrawClip() | |
{ | |
this.mouseChildren = false; | |
} | |
/** | |
* copy displayobject | |
* @param resource | |
*/ | |
public function copy(resource:DisplayObject):void { | |
var graphics:Graphics; | |
if (resource is Shape) { | |
graphics = Shape(resource).graphics; | |
} | |
else { | |
if (resource is MovieClip) { | |
drawClip(MovieClip(resource)); | |
} | |
else if (resource is Sprite) { | |
graphics = Sprite(resource).graphics; | |
} | |
else { | |
if (!internalBitmapContent) { | |
internalBitmapContent = new Bitmap(); | |
} | |
if (internalBitmapContent.bitmapData) { | |
internalBitmapContent.bitmapData.dispose(); | |
} | |
internalBitmapContent.bitmapData = getDrawContent(resource); | |
this.addChild(internalBitmapContent); | |
} | |
} | |
if (graphics) { | |
graphicsDataVect = graphics.readGraphicsData(true); | |
if (!graphicsShape) { | |
graphicsShape = new Shape(); | |
} | |
graphicsShape.graphics.clear(); | |
graphicsShape.graphics.drawGraphicsData(graphicsDataVect); | |
this.addChild(graphicsShape); | |
} | |
} | |
/** | |
* create clip | |
* @param mc | |
*/ | |
private function drawClip(mc:MovieClip):void { | |
if (!clipVect) { | |
clipVect = new Vector.<Shape>(); | |
} | |
frameIndex = 0; | |
clipVect.length = 0; | |
var s:Shape, graphicsDataVect:Vector.<IGraphicsData>; | |
for (var i:int = 1; i <= mc.totalFrames; i ++) { | |
mc.gotoAndStop(i); | |
s = new Shape(); | |
s.visible = false; | |
graphicsDataVect = mc.graphics.readGraphicsData(true); | |
s.graphics.drawGraphicsData(graphicsDataVect); | |
clipVect[i - 1] = s; | |
this.addChild(s); | |
} | |
clipVect[frameIndex].visible = true; | |
} | |
/** | |
* get totalFrames | |
*/ | |
public function get totalFrames():int { | |
return clipVect == null ? 1 : clipVect.length; | |
} | |
/** | |
* go to nextFrame | |
*/ | |
public function nextFrame():void { | |
if (clipVect) { | |
if (frameIndex + 1 < totalFrames) { | |
clipVect[frameIndex].visible = false; | |
frameIndex++; | |
} | |
var s:Shape = clipVect[frameIndex]; | |
s.visible = true; | |
} | |
} | |
/** | |
* get currentFrame | |
*/ | |
public function get currentFrame():int { | |
return clipVect == null ? 1 : frameIndex + 1; | |
} | |
/** | |
* go to prevFrame | |
*/ | |
public function prevFrame():void { | |
if (clipVect) { | |
if (frameIndex > 0) { | |
clipVect[frameIndex].visible = false; | |
frameIndex--; | |
} | |
var s:Shape = clipVect[frameIndex]; | |
s.visible = true; | |
} | |
} | |
private var frameIndex:int = 0; | |
/** | |
* simple draw content | |
* @param content | |
* @return | |
*/ | |
private function getDrawContent(content:DisplayObject):BitmapData { | |
var d:BitmapData = new BitmapData(content.width, content.height, true, 0); | |
d.draw(content); | |
return d; | |
} | |
private var internalBitmapContent:Bitmap; | |
private var graphicsDataVect:Vector.<IGraphicsData>; | |
private var graphicsShape:Shape; | |
private var clipVect:Vector.<Shape>; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment