图片突出展示(二)as3版

GalleryBrowser.as

package {
	import flash.display.Sprite;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Timer;
	import flash.events.TimerEvent;

	import com.greensock.TweenLite;
	import fl.motion.easing.*;

	public class GalleryBrowser extends Sprite {
		private var xmlURL:String;
		private var xml:XML;
		public var len:int;
		private var arr_photoName:Array;
		private var arr_photoURL:Array;
		private var arr_photoLink:Array;

		private var photoWidth:Number=226;//图片宽度(不是photoFrame宽度)
		private var photoHeight:Number=220;//图片高度(不是photoFrame高度)

		private var arr_Xcoordinate:Array=[-290,-257,-210,-125,0,158,292,388,467];
		private var arr_Ycoordinate:Array=[photoHeight*0.4,photoHeight*0.3,photoHeight*0.2,photoHeight*0.1,0,photoHeight*0.1,photoHeight*0.2,photoHeight*0.3,photoHeight*0.4];
		//private var arr_Ycoordinate:Array=[150,130,110,90,0,90,110,130,150]

		private var arr_scale:Array=[0.2,0.4,0.6,0.8,1,0.8,0.6,0.4,0.2];
		//private var arr_scale:Array=[0.7,0.7,0.7,0.7,1,0.7,0.7,0.7,0.7];
		//private var arr_alpha:Array=[0.2,0.4,0.6,0.8,1,0.8,0.6,0.4,0.2];
		private var arr_alpha:Array=[1,1,1,1,1,1,1,1,1];

		private var arr_depth:Array=[0,2,4,6,8,7,5,3,1];
		private var arr_labelVisible:Array=[false,false,false,false,true,false,false,false,false];

		private var photoFrameContainer:Sprite;//装所有photoFrame的容器

		private var timer:Timer;

		private var currentID:int=0;
		private var anotherID:int=0;

		public function GalleryBrowser(xmlURL_:String):void {
			xmlURL=xmlURL_;

			init();
		}
		private function init():void {
			prevBtn.buttonMode=true;
			nextBtn.buttonMode=true;

			photoFrameContainer=new Sprite  ;
			addChild(photoFrameContainer);
			photoFrameContainer.x=400;
			photoFrameContainer.y=125;

			timer=new Timer(3000);
			timer.addEventListener(TimerEvent.TIMER,rotating,false,0,true);

			loadXML(xmlURL);
		}

		private function loadXML(url_:String):void {
			var xmlLoader:URLLoader=new URLLoader  ;
			xmlLoader.load(new URLRequest(url_));
			xmlLoader.addEventListener(Event.COMPLETE,xmlLoaded,false,0,true);
		}
		private function xmlLoaded(evt:Event):void {
			xml=XML(evt.currentTarget.data);
			len=xml.image_information.length();
			arr_photoName=[];
			arr_photoURL=[];
			arr_photoLink=[];

			for (var i:int=0; i<len; i++) {
				var photoName:String=xml.image_information[i].img_name;
				var photoURL:String=xml.image_information[i].small_url;
				var photoLink:String=xml.image_information[i].img_link;

				arr_photoName[i]=photoName;
				arr_photoURL[i]=photoURL;
				arr_photoLink[i]=photoLink;
			}

			loadPhotoFrame();
		}

		//-------------------------------------------------------------------------------------------
		//载入所有图片
		private function loadPhotoFrame():void {
			for (var i:int=0; i<len; i++) {
				var _name:String=arr_photoName[i];
				var url:String=arr_photoURL[i];
				var link:String=arr_photoLink[i];

				var photoFrame:PhotoFrame=new PhotoFrame(url,_name,link);
				photoFrame.name="photoFrame"+i;
				photoFrameContainer.addChild(photoFrame);
				photoFrame.buttonMode=true;

				photoFrame.addEventListener(MouseEvent.MOUSE_OVER,photoFrameOver,false,0,true);
				photoFrame.addEventListener(MouseEvent.MOUSE_OUT,photoFrameOut,false,0,true);
				photoFrame.addEventListener(MouseEvent.CLICK,photoFrameClick,false,0,true);
			}

			initializePhotoState();

			//timer.start();

			prevBtn.addEventListener(MouseEvent.MOUSE_OVER,prevBtnOver,false,0,true);
			nextBtn.addEventListener(MouseEvent.MOUSE_OVER,nextBtnOver,false,0,true);

			prevBtn.addEventListener(MouseEvent.MOUSE_OUT,prevBtnOut,false,0,true);
			nextBtn.addEventListener(MouseEvent.MOUSE_OUT,nextBtnOut,false,0,true);

			prevBtn.addEventListener(MouseEvent.CLICK,prevBtnClick,false,0,true);
			nextBtn.addEventListener(MouseEvent.CLICK,nextBtnClick,false,0,true);
		}

		//-------------------------------------------------------------------------------------------
		private function photoFrameOver(evt:MouseEvent):void {
			timer.stop();
		}

		private function photoFrameOut(evt:MouseEvent):void {
			//timer.start();
		}

		private function photoFrameClick(evt:MouseEvent):void {
			var currentPhotoFrame:PhotoFrame=evt.currentTarget as PhotoFrame;

			//在这里先判断一下是点击左边的photoFrame还是右边的,获取到两种情况下的subtraction(正、负)
			//通过当前photoFrame的y坐标与中间photoFrame的y坐标的差值来判断需要移动几次
			var subtraction:int=(currentPhotoFrame.x>0)?(0-currentPhotoFrame.y)/20:(currentPhotoFrame.y-0)/20;
			currentID=(currentID+subtraction+len)%len;//相当于下面的算法
			trace(currentPhotoFrame._name+" is clicked >>"+"subtraction="+subtraction);

			/*if (currentPhotoFrame.x>0) {
			subtraction=(0-currentPhotoFrame.y)/20;
			
			if (currentID-Math.abs(subtraction)>=0) {
			currentID+=subtraction;
			} else if (currentID-Math.abs(subtraction)==-1) {
			currentID=len-1;
			} else if (currentID-Math.abs(subtraction)<-1) {
			var XXX:int=currentID-0;
			currentID=(len-1)-(Math.abs(subtraction)-XXX)+1;
			}
			}else if(currentPhotoFrame.x<0){
			subtraction=(currentPhotoFrame.y-0)/20;
			
			if(currentID+Math.abs(subtraction)<=len-1){
			currentID+=subtraction;
			}else if(currentID+Math.abs(subtraction)==len){
			currentID=0;
			}else if(currentID+Math.abs(subtraction)>len){
			var YYY:int=8-currentID;
			currentID=Math.abs(subtraction)-YYY-1;
			}
			}
			*/

			for (var i:int=0; i<len; i++) {
				var photoFrame:PhotoFrame=photoFrameContainer.getChildByName("photoFrame"+i) as PhotoFrame;

				var old_posNum:int=arr_Xcoordinate.indexOf(photoFrame.x);
				var current_posNum:int=(old_posNum+subtraction+len)%len;

				TweenLite.to(photoFrame,0.5,{alpha:arr_alpha[current_posNum],x:arr_Xcoordinate[current_posNum],y:arr_Ycoordinate[current_posNum],scaleX:arr_scale[current_posNum],scaleY:arr_scale[current_posNum]});

				//photoFrame.tf_label.visible = arr_labelVisible[current_posNum];
				photoFrameContainer.setChildIndex(photoFrame,arr_depth[current_posNum]);
				trace(photoFrame._name+"  old_posNum="+old_posNum+"  current_posNum="+current_posNum+"        depth="+arr_depth[current_posNum]);

				if (current_posNum==(len-1)/2) {
					this.tf_currentPhoto.text=arr_photoName[i];
				}
			}
		}

		//-------------------------------------------------------------------------------------------
		//初始化图片状态
		private function initializePhotoState():void {
			for (var i:int=0; i<len; i++) {
				var photoFrame:PhotoFrame=photoFrameContainer.getChildByName("photoFrame"+i) as PhotoFrame;
				TweenLite.to(photoFrame,0.5,{alpha:arr_alpha[i],x:arr_Xcoordinate[i],y:arr_Ycoordinate[i],scaleX:arr_scale[i],scaleY:arr_scale[i]});

				//photoFrame.tf_label.visible = arr_labelVisible[i];
				photoFrameContainer.setChildIndex(photoFrame,arr_depth[i]);

				this.tf_currentPhoto.text=arr_photoName[(len-1)/2];
			}
		}

		//-------------------------------------------------------------------------------------------
		private function prevBtnOver(evt:MouseEvent):void {
			timer.stop();
		}
		private function nextBtnOver(evt:MouseEvent):void {
			timer.stop();
		}

		private function prevBtnOut(evt:MouseEvent):void {
			//timer.start();
		}
		private function nextBtnOut(evt:MouseEvent):void {
			//timer.start();
		}

		private function prevBtnClick(evt:MouseEvent):void {
			currentID++;
			if (currentID==len) {
				currentID=0;
			}
			changePhotoState();
		}

		private function nextBtnClick(evt:MouseEvent):void {
			showNextPhoto();
		}

		private function rotating(evt:TimerEvent):void {
			showNextPhoto();
		}

		private function showNextPhoto():void {
			currentID--;
			if (currentID==-1) {
				currentID=len-1;
			}
			changePhotoState();
		}

		//-------------------------------------------------------------------------------------------
		//调整图片状态
		private function changePhotoState():void {
			for (var i:int=0; i<len; i++) {
				var posNum:int=(currentID+i)%len;

				var photoFrame:PhotoFrame=photoFrameContainer.getChildByName("photoFrame"+i) as PhotoFrame;
				TweenLite.to(photoFrame,0.5,{alpha:arr_alpha[posNum],x:arr_Xcoordinate[posNum],y:arr_Ycoordinate[posNum],scaleX:arr_scale[posNum],scaleY:arr_scale[posNum]});

				//photoFrame.tf_label.visible = arr_labelVisible[posNum];
				photoFrameContainer.setChildIndex(photoFrame,arr_depth[posNum]);

				if (posNum==(len-1)/2) {
					this.tf_currentPhoto.text=arr_photoName[i];
				}
			}
		}
	}
}

PhotoFrame.as

package {
	import flash.display.Sprite;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;

	public class PhotoFrame extends Sprite {
		private var url:String;
		public var _name:String;
		private var link:String;

		private var loader:Loader;

		public function PhotoFrame(url_:String,name_:String,link_:String):void {
			url = url_;
			_name = name_;
			link=link_;

			init();
		}
		private function init():void {
			tf_label.text = _name;

			loader=new Loader();
			loader.load(new URLRequest(url));
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaded,false,0,true);
		}

		private function loaded(evt:Event):void {
			this.bg.addChild(loader);
			loader.x = 5;
			loader.y = 5;
		}
	}
}
原文地址:https://www.cnblogs.com/leon3286/p/1804850.html