您的位置:首页 > 其它

flex 图片切换,可自己定义大小和图片路径

2009-08-18 17:33 274 查看
]<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()" color="#EBF6F8" themeColor="#ECF2F5" borderColor="#EAEFF2" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#FBF6F6, #FBF6F6]">

<mx:Script>
<![CDATA[
import mx.controls.Button;
import mx.controls.Text;
import mx.containers.HBox;

import mx.controls.Alert;
import mx.controls.Image;

private var _width:int;
private var _height:int;
private var _times:int;                  //动画间隔

//数据源
[Bindable]
private var _imageArray:Array=new Array(); 	//图片系列数组

private var _imageMain:Image=new Image();	   //主图片
private var _nextMain:Image=new Image();      //下一个要显示的图片
private var _currentImageNumber:int=0;       //当前图片号
private var _imageMax:int=4;                 //图片总数
private var _time:Timer;                    //计时器
private var _imageMoveCounter:Number=0;  //计数器
// private var _imageSpace:int=400;        //图片变换的时间间隔
private var _textArray:Array=new Array();
private var _num:int=0;

// private var _url:String="http://localhost:8080/testXml/logoImage.xml";
private var _url:String="logoImage.xml";

public function initApp():void {

ExternalInterface.addCallback("getValite",init);
}

private function init():void{

//	this._url=url;
try{
var req:URLRequest=new URLRequest(_url);
new URLLoader(req)
.addEventListener(Event.COMPLETE,
function(event : Event) : void
{
var s:String=event.currentTarget.data;
var example:XML = new XML(s);
_width=example.@width;
_height=example.@height;
_times=example.@timer;
_imageArray=ElementToAttr2(example.children());
nextInit();
}
);

}catch(error:Error){
Alert.show("网络连接出错!");
}

}
private function nextInit():void{
this.width=_width;
this.height=_height;
_time=new Timer(10,0);
_time.start();
_imageMain.alpha=1;
_nextMain.alpha=0;
_time.addEventListener(TimerEvent.TIMER,moveImage);
_imageMax=_imageArray.length;
_nextMain.x=_imageMain.x=0;
_nextMain.y=_imageMain.y=0;
_nextMain.width=_imageMain.width=_imageArray[_currentImageNumber].width;
_nextMain.height=_imageMain.height=_imageArray[_currentImageNumber].height;
_imageMain.source=_imageArray[_currentImageNumber].src;
_nextMain.source=_imageArray[_currentImageNumber].src;
_imageMain.addEventListener(MouseEvent.CLICK,clickImage);
_nextMain.addEventListener(MouseEvent.CLICK,clickImage);
this.addChild(_imageMain);
this.addChild(_nextMain);

var hbox:HBox=new HBox();
hbox.setStyle("verticalAlign","bottom");

hbox.x= _imageMain.x+_imageMain.width-_imageMax*40-10;
hbox.y=_imageMain.y+_imageMain.height-20;

for(var i:int=1;i<_imageMax+1;i++){
var button:Button=new Button();

button.width=30;
button.height=15;

button.label=new String(i);
button.setStyle("fontWeight","normal");
button.setStyle("cornerRadius","0");
button.setStyle("fontSize","9");
button.setStyle("color","#000000");
button.setStyle("backgroundColor","#ffffff");
button.setStyle("fillColors",["#000000","#000000"]);
button.setStyle("fillAlphas",[0.1, 0.1]);
if(i==(_currentImageNumber+1)){
button.setStyle("color","#FF0000");
button.setStyle("backgroundColor","#FF0000");
button.setStyle("fillColors",["#FF0000","#FF0000"]);
}
button.addEventListener(MouseEvent.MOUSE_OVER,checkTextOver);
button.addEventListener(MouseEvent.MOUSE_OUT,checkTextOut);
_textArray.push(button);
hbox.addChild(button);
}
this.addChild(hbox);

}

private function checkTextOver(event:MouseEvent):void{
clearTextColor();
_time.stop();
(event.currentTarget as Button).setStyle("color",0xff0000);
(event.currentTarget as Button).setStyle("fillColors",["#000000","#FF0000"]);
_currentImageNumber= new int((event.currentTarget as Button).label)-1;
_imageMain.source=_imageArray[_currentImageNumber].src;
_imageMain.alpha=1;
var n:int=_currentImageNumber+1;
if(n==_imageArray.length)n=0;
_nextMain.source=_imageArray
.src;
_nextMain.alpha=0;
_num=0;

}
private function checkTextOut(event:MouseEvent):void{
(event.currentTarget as Button).setStyle("color",0x000000);
_currentImageNumber= new int((event.currentTarget as Button).label)-1;
_imageMoveCounter=0;

_time.start();

}

private function clickImage(event:MouseEvent):void{

var request:URLRequest = new URLRequest(_imageArray[_currentImageNumber].url);
navigateToURL(request,'_blank');
//  navigateToURL(request,'_self');

}

private function moveImage(event:Event):void{

_imageMoveCounter++;

if(_imageMoveCounter==_times){
_currentImageNumber++;
if(_currentImageNumber>=_imageMax)_currentImageNumber=0;

if(_imageMain.alpha>_nextMain.alpha){
_nextMain.source=_imageArray[_currentImageNumber].src;

}else{
_imageMain.source=_imageArray[_currentImageNumber].src;
}
_num=_num==0?1:0;

_imageMoveCounter=0;
clearTextColor();
(_textArray[_currentImageNumber] as Button).setStyle("color",0xff0000);
(_textArray[_currentImageNumber] as Button).setStyle("fillColors",["#000000","#FF0000"]);

}else if(0<_imageMoveCounter&&_imageMoveCounter<_times/2){
if(_num==0){
_imageMain.alpha-=2.0/_times;
_nextMain.alpha+=2.0/_times;
}else if(_num==1){
_imageMain.alpha+=2.0/_times;
_nextMain.alpha-=2.0/_times;
}

}
else if(_times/2<_imageMoveCounter&&_imageMoveCounter<_times){

if(_num==0){
_nextMain.alpha+=2.0/_times;
_imageMain.alpha-=2.0/_times;
if(_imageMain.alpha<0)
_imageMain.alpha=0;
if(_nextMain.alpha>1)
_nextMain.alpha=1;
}else if(_num==1){
_imageMain.alpha+=2.0/_times;
_nextMain.alpha-=2.0/_times;
if(_imageMain.alpha>1)
_imageMain.alpha=1;
if(_nextMain.alpha<0)
_nextMain.alpha=0;
}

}
}
private function clearTextColor():void{
for(var k:int=0;k<_imageMax;k++){
(_textArray[k] as Button).setStyle("color",0x000000);
(_textArray[_currentImageNumber] as Button).setStyle("fillColors",["#000000","#000000"]);
}

}
//属性形式的XML
public function ElementToAttr2(results:XMLList):Array
{

var array:Array=new Array();

for each(var child:XML in results){
var obj:Object=new Object();
var attNamesList:XMLList = child.@*;

for(var i:int=0;i<attNamesList.length();i++)
{
obj[attNamesList[i].name().toString()]=attNamesList[i].toString();

}
array.push(obj);

}
return array;
}

]]>
</mx:Script>

</mx:Application>


演示路径:

http://www.lvwlv.com/showModel/logo.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: