自定义动画效果(Iris动画修改)

来源:互联网 发布:淘宝众筹服务商 编辑:程序博客网 时间:2024/05/17 23:57

NewIrisEffert.as

 

package tree{
    
//导入代码中使用的对象
    import mx.effects.MaskEffect;
    
import mx.effects.EffectInstance;
    
import mx.controls.SWFLoader;
    
import flash.display.Shape;
    
import flash.geom.Rectangle;
    
import mx.core.FlexShape;
    
import flash.display.Graphics;

    
public class NewIrisEffect extends MaskEffect{
        
//表明动画采用的起始点类型
        public var position:String;     //这个参数非常有用!
        
//构造函数
        public function NewIrisEffect(target:Object=null){
            
super(target);
            
//定义实例类型为NewIrisInstance,代替原来的IrisInstance.NewIrisInstance是我们新建的对像。
            instanceClass=NewIrisInstance;
            
//定义新的createMaskFunction,此函数用来绘制遮罩层
            this.createMaskFunction=createLargeMask;
        }

        
//此函数用来绘制遮罩层
        public function createLargeMask(targ:Object,bounds:Rectangle):Shape{
            
//绘制前,首先得到目标的实际尺寸,来确定遮罩层的尺寸。targetVisualBounds包含了目标可视区域的长和宽
            var targetWidth:Number = bounds.width / Math.abs(targ.scaleX);
            var targetHeight:Number 
= bounds.height / Math.abs(targ.scaleY);
            
if (targ is SWFLoader){
                
//
                targ.validateDisplayList();
                
if(targ.content){
                    targetWidth 
= targ.contentWidth;
                    targetHeight 
= targ.contentHeight;        
                }

            }

            
//创建遮罩层的表现对象
            var newMask:Shape=new FlexShape();
            var g:Graphics
=newMask.graphics;
            
//设定填充色
            g.beginFill(0xFFFF00);
            
//绘制椭圆,drawEllipse是Griphics对象的绘制方法之一,默认情况下绘制矩形
            g.drawEllipse(0,0,targetWidth,targetHeight);
            
            g.endFill();
            
//判断是否跳转
            if(target.rotation==0){
                
//如果没有跳转
                newMask.width=targetWidth;
                newMask.height
=targetHeight;
            }
else{
                
//如果跳转,重新计算遮罩层的大小
                
//得到角度的弧度数
                var angle:Number=targ.rotation * Math.PI/180;
                var sin:Number
=Math.sin(angle);
                var cos:Number
=Math.cos(angle);
                
//计算出新尺寸
                newMask.width=Math.abs(targetWidth * cos-targetHeight * sin);
                newMask.height
=Math.abs(targetWidth * sin + targetHeight * cos);
            }

            
            
return newMask;
        }

        
//覆盖父类的initInstance方法
        
//将生成类的属性传到实例对象中,这个方法在对象内部被调用
        override protected function initInstance(instance:EffectInstance):void{
            
//调用父类的同名方法
            super.initInstance(instance);
            
//创建新的实例
            var maskEffectInstance:NewIrisInstance=NewIrisInstance(instance);
            
//传递参数给实例
            maskEffectInstance.showTarget=showTarget;
            maskEffectInstance.xFrom
=xFrom;
            maskEffectInstance.yFrom
=yFrom;
            maskEffectInstance.xTo
=xTo;
            maskEffectInstance.yTo
=yTo;
            maskEffectInstance.scaleXFrom
=scaleXFrom;
            maskEffectInstance.scaleXTo
=scaleXTo;
            maskEffectInstance.scaleYFrom
=scaleYFrom;
            maskEffectInstance.scaleYTo
=scaleYTo;
            
//确定动画执行的初使位置
            maskEffectInstance.position=position;
        }

        
    }

}

 NewIrisInstance.as

 

package tree{
    
import mx.effects.effectClasses.MaskEffectInstance;
    
import mx.controls.SWFLoader;
    
    
public class NewIrisInstance extends MaskEffectInstance{
        
//新添加的参数,用来确定动画播放时的初使位置
        public var position:String;          //这个参数非常有用!
        
//构造函数
        public function NewIrisInstance(target:Object){    
            
super(target);
        }

        
//覆盖父类的方法,initMaskEffect主要初使化遮罩动画的信息
        override protected function initMaskEffect():void{
            
//调用父类的同名方法
            super.initMaskEffect();
            
//得到目标的实际尺寸,targetVisualBounds包含了目标可视区域的长和宽
            var targetWidth:Number = targetVisualBounds.width / Math.abs(target.scaleX);
            var targetHeight:Number 
= targetVisualBounds.height / Math.abs(target.scaleY);
            
//如果目标是SWFLoader对象,则计算加载内容的尺寸,而不是目标本身的尺寸
            if (target is SWFLoader){
                targetWidth 
= target.contentWidth;
                targetHeight 
= target.contentHeight;        
            }

            
//如果showTarget为true,表示显示目标对象
            if (showTarget){
                
//定义x,y方向的起始放缩值
                scaleXFrom = 0;
                scaleYFrom 
= 0;
                scaleXTo 
= 1;
                scaleYTo 
= 1;
                
//依据position属性决定起点的位置
                
//如果中心是左上角
                if(position=="leftTop"){
                    
//从左上角开始
                    
//targetVisualBounds还包含了目标可视区域的可视域的坐标位置
                    xFrom = targetVisualBounds.x;
                    yFrom 
= targetVisualBounds.y;
                }
else if(position=="rightBottom"){
                    
//如果中心是右下角,从右下角开始
                    xFrom = targetWidth + targetVisualBounds.x;
                    yFrom 
= targetHeight + targetVisualBounds.y;    
                }
else{
                    
//从默认中心开始,保持原来的计算方式不变
                    xFrom = targetWidth / 2 + targetVisualBounds.x;
                    yFrom 
= targetHeight / 2 + targetVisualBounds.y;
                }

                
//确定最后的坐标
                xTo = targetVisualBounds.x;
                yTo 
= targetVisualBounds.y;
            }
else{
                
//如果是隐藏目标
                scaleXFrom = 1;
                scaleYFrom 
= 1;
                scaleXTo 
= 0;
                scaleYTo 
= 0;
                
//目标的起始位置
                xFrom = targetVisualBounds.x;
                yFrom 
= targetVisualBounds.y;
                
//确定最后的坐标
                if(position=="leftTop"){
                    xFrom 
= targetVisualBounds.x;
                    yFrom 
= targetVisualBounds.y;
                }
else if(position=="rightBottom"){
                    xFrom 
= targetWidth + targetVisualBounds.x;
                    yFrom 
= targetHeight + targetVisualBounds.y;    
                }
else{
                    xTo 
= targetWidth / 2 + targetVisualBounds.x;
                    yTo 
= targetHeight / 2 + targetVisualBounds.y;
                }

                
            }

        }

    }



}

 

NewIris.mxml

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:tree="tree.*">
    
<!-- 定义动画效果   -->
    
<tree:NewIrisEffect position="leftTop" id="myIris" duration="1000"></tree:NewIrisEffect>
    
<!-- 动画效果绑定在图片上  -->
    
<mx:Image x="10" y="10" source="img/5.jpg" width="125" height="190" id="image" showEffect="{myIris}" hideEffect="{myIris}"/>
    
<mx:Button x="36" y="227" label="播放" click="image.visible=!image.visible"/>
    
</mx:Application>
原创粉丝点击