加载透明图片
可以被FLASH动态加载的背景透明图片大致就GIF,PNG两种
先说说GIF图片,用普通的加载方法加载GIF图片完后只能看到动态图片的第一帧,由动态图变成静态的了
所幸有达人完美解决了这个问题,并封装好类供我们使用,这就是GIFPlayer。下载地址网上搜一下很多,要找不到也可以找我要
GIFPlayer使用示例
package{
/*
QQ: 334973991
*/
import flash.net.URLRequest;
import flash.display.Sprite;
import org.gif.events.GIFPlayerEvent;
import org.gif.player.GIFPlayer;
public class GIFPlayerTest extends Sprite {
private var player:GIFPlayer;
private var request:URLRequest;
public function GIFPlayerTest() {
request=new URLRequest("girl.gif");
player = new GIFPlayer();
startLoadImg();
this.addChild(player);
player.addEventListener(GIFPlayerEvent.COMPLETE,imageLoadComplete);
}
public function startLoadImg():void {
player.load(request);
}
public function imageLoadComplete(e:GIFPlayerEvent):void {
trace("图片加载成功");
trace(e);
}
}
}
AS3传统的方法加载透明图片,看着是透明的,但鼠标事件还是会响应的。有两种解决方案
第一种:在需要的鼠标事件触发时 判断鼠标所点位置的像素是否透明,透明的话则不响应该事件
为了程序的扩展性,可以添加一些自定义事件代替原有鼠标事件
程序如下:
package king.loadAphaImage{
/*
QQ: 334973991
*/
import flash.display.*;
import flash.events.*;
public class Main extends Sprite{
private var loadImage:LoadImage;
private var sprite:Sprite;
public function Main(){
loadImage=new LoadImage(completeHandler);
loadImage.Loading("king/loadAphaImage/langfangshi.png");
}
private function completeHandler(events:Event):void {
sprite=new Sprite();
var bitmap:Bitmap=events.target.content;
sprite.addChild(bitmap);
DealImage.dealImage(sprite);
sprite.addEventListener(MouseEvent.CLICK,clickHandle);
sprite.addEventListener("IMAGE_MOUSE_CLICK",imageClickHandle);
this.addChild(sprite);
}
private function clickHandle(e:MouseEvent):void{
trace("点击到我啦CLICK事件");
}
private function imageClickHandle(e:MouseEvent):void{
trace("点击到我啦IMAGE_CLICK事件");
}
}
}
------------------------------------------------------------------------------------
package king.loadAphaImage{
/*
QQ: 334973991
*/
import flash.display.*;
import flash.events.*;
import flash.net.*;
public class LoadImage{
private var fun:Function;
public function LoadImage(completeFun:Function){
fun=completeFun;
}
public function Loading(url:String ){
try {
var loader:Loader=new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,cancel);
loader.load(new URLRequest(url));
} catch (error:Error) {
trace(error);
}
}
private function cancel(events:IOErrorEvent):void {
trace(events);
}
private function completeHandler(events:Event):void {
fun(events);
}
}
}
--------------------------------------------------------------------------------------------
package king.loadAphaImage{
/*
QQ: 334973991
*/
import flash.display.*;
import flash.events.*;
public class DealImage{
public static function dealImage(sprite:Sprite){
sprite.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandle);
sprite.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandle);
sprite.addEventListener(MouseEvent.MOUSE_OUT,mouseOutHandle);
sprite.addEventListener(MouseEvent.MOUSE_OVER,mouseOverHandle);
sprite.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandle);
sprite.addEventListener(MouseEvent.CLICK,mouseClickHandle);
}
private static function mouseDownHandle(events:MouseEvent):void {
var sprite:Sprite=events.currentTarget as Sprite;
if(checkIsApha(events))sprite.dispatchEvent(new MouseEvent("IMAGE_MOUSE_DOWN"));
}
private static function mouseMoveHandle(events:MouseEvent):void {
var sprite:Sprite=events.currentTarget as Sprite;
if(checkIsApha(events))sprite.dispatchEvent(new MouseEvent("IMAGE_MOUSE_MOVE"));
}
private static function mouseOutHandle(events:MouseEvent):void {
var sprite:Sprite=events.currentTarget as Sprite;
if(checkIsApha(events))sprite.dispatchEvent(new MouseEvent("IMAGE_MOUSE_OUT"));
}
private static function mouseOverHandle(events:MouseEvent):void {
var sprite:Sprite=events.currentTarget as Sprite;
if(checkIsApha(events))sprite.dispatchEvent(new MouseEvent("IMAGE_MOUSE_OVER"));
}
private static function mouseUpHandle(events:MouseEvent):void {
var sprite:Sprite=events.currentTarget as Sprite;
if(checkIsApha(events))sprite.dispatchEvent(new MouseEvent("IMAGE_MOUSE_UP"));
}
private static function mouseClickHandle(events:MouseEvent):void {
var sprite:Sprite=events.currentTarget as Sprite;
if(checkIsApha(events))sprite.dispatchEvent(new MouseEvent("IMAGE_MOUSE_CLICK"));
}
private static function checkIsApha(events:MouseEvent):Boolean{
var sprite:Sprite=events.currentTarget as Sprite;
if((sprite.getChildAt(0) as Bitmap).bitmapData.getPixel32(events.localX,events.localY)){
return true;
}
return false;
}
}
}
第二种方法是把这种图片里的像素重新处理,只有不透明的像素才留下来
原图san.png
因为图片上传网站的原因把图片透明背景处理成淡蓝色的了
private function copyImagExtendTransparent(bit:BitmapData):Sprite{
var _width:uint=bit.width;
var _height:uint=bit.height;
var ht:Sprite=new Sprite();
for(var x:uint=0;x<_width;x++){
for(var y:uint=0;y<_height;y++){
if(bit.getPixel32(x,y)){
var aphaValue:uint=bit.getPixel32(x,y) >> 24 & 0xFF;
ht.graphics.beginFill(bit.getPixel(x,y),aphaValue/255);
ht.graphics.drawRect(x,y,1,1);
ht.graphics.endFill();
}
}
}
return ht;
}
处理完的图片效果和原图一样 不过在透明位置不会效应鼠标事件,图片的宽高也不包括透明位置
------------------------------------------------------------------
private function getImageBorder(bit:BitmapData):Sprite{
var _width:uint=bit.width;
var _height:uint=bit.height;
var ht:Sprite=new Sprite();
ht.graphics.beginFill(0);
for(var x:uint=0;x<_width;x++){
for(var y:uint=0;y<_height;y++){
if(bit.getPixel32(x,y)){
if(!bit.getPixel32(x-1,y)||!bit.getPixel32(x,y-1)||!bit.getPixel32(x+1,y)||!bit.getPixel32(x,y+1)){
ht.graphics.drawRect(x,y,1,1);
}
}
}
}
ht.graphics.endFill();
return ht;
}
获取图片的边框
--------------------------------------------------------------------------------------------
private function copyNoAphaImag(bit:BitmapData):Sprite{
var minx:uint=100000;
var miny:uint=100000;
var pointXArray:Array=new Array();
var pointYArray:Array=new Array();
var _width:uint=bit.width;
var _height:uint=bit.height;
for(var x:uint=0;x<_width;x++){
for(var y:uint=0;y<_height;y++){
if(bit.getPixel32(x,y)){
minx=minx>x?x:minx;
miny=miny>y?y:miny;
pointXArray.push(x);
pointYArray.push(y);
}
}
}
var ht:Sprite=new Sprite();
ht.graphics.beginFill(0);
var _length:uint=pointXArray.length;
for(var i:uint=0;i<_length;i++){
ht.graphics.drawRect(pointXArray[i]-minx,pointYArray[i]-miny,1,1);
}
ht.graphics.endFill();
ht.x=minx;
ht.y=miny;;
return ht;
}
这个方法得到图片和第一个方法外表看没有差别,但第一个图片的X,Y值是0,0.而这个X,Y值则是剔除透明区域后,图片仍在原位置时所需的X,Y值 ,更符合现实逻辑。感兴趣的同学可以分别输出他俩的宽高和X,Y值,比较一下
1. package 2. {
3. import flash.display.BitmapData; 4. import flash.display.DisplayObject; 5. import flash.display.Sprite; 6. import flash.events.Event; 7. import flash.geom.Matrix;
8. import flash.utils.setTimeout; 9.
10. import mx.controls.Image; 11. /**
12. * 自定义Image类,使之忽略PNG透明区域 13. * @author L4cd.Net 14. */
15. public class MyImage extends Image 16. {
17. private varht:Sprite = new Sprite(); 18. public function MyImage() 19. {
20. addChild(ht); 21. hitArea = ht;
22. //指定hitArea为ht对象 23. ht.visible = false;
24. ht.mouseEnabled = false; 25. mouseChildren = false;
26. addEventListener(Event.COMPLETE,complete,false,
99,true);
27. setTimeout(update,50) 28. }
29. private function complete(e:Event):void 30. {
31. setTimeout(update,50) 32. }
33. private function update():void 34. {
35. if(!content)return;
36. varloader:DisplayObject = content.parent as
DisplayObject;
37. varbit:BitmapData = new
BitmapData(loader.width,loader.height,true,0x00000000); 38. varmat:Matrix = new Matrix();
39. mat.scale(loader.scaleX,loader.scaleY); 40. bit.draw(loader,mat); 41. //重绘图象到bit
42. ht.graphics.clear();
43. ht.graphics.beginFill(0);
44. for(var x:uint=0;x<bit.width;x++) 45. {
46. for(var y:uint=0;y<bit.height;y++) 47. {
48. if(bit.getPixel32(x,y))ht.graphics.draw
Rect(x,y,1,1); 49. } 50. }
51. //以graphics画出bit的无透明区域 52. ht.graphics.endFill(); 53. } 54. } 55.}