关于AS3加载透明图片

可以被FLASH动态加载的背景透明图片大致就GIF,PNG两种

先说说GIF图片,用普通的加载方法加载GIF图片完后只能看到动态图片的第一帧,由动态图变成静态的了

所幸有达人完美解决了这个问题,并封装好类供我们使用,这就是GIFPlayer。下载地址网上搜一下很多

GIFPlayer使用示例

package{
 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{
 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{
 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{
 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值,比较一下。

本文转自:http://blog.163.com/wuyongxiaodi@126/blog/static/7273500520100634727588/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值