Flex performs image switching (playback)
Java Code
<? XML version ="1.0"Encoding ="UTF-8"?>
<Mx: Application xmlns: MX =Http://www.adobe.com/2006/mxml"Layout ="Absolute"
Backgroundcolor = "white" width = "300" Height = "200" creationcomplete = "getresult ()">
<Mx: style>
. Vbox {
Back groundcolor: #000000;
Textalign: center;
Borderstyle: solid;
Bordercolor: # ffffff;
}
</MX: style>
<Mx: SCRIPT>
<! [CDATA [
Import MX. containers. vbox;
Import flash. utils. setinterval;
Import MX. containers. Canvas;
Import MX. Controls. label;
Import MX. Controls. Button;
Import MX. Controls. text;
Import MX. Controls. Alert;
Import MX. rpc. Events .*;
Import MX. Collections .*;
Import MX. Controls .*;
Import flash. utils .*;
Import flash. Events. timerevent;
Import flash. Events. contextmenuevent;
Private var newslistxml: XML;
Private var newslist: xmllistcollection;
Private var picurl: String = "E: \ Ad \\";
Private var newsarray: array = new array; // global variable, used to hold the vbox set
Private var currnews: News;
Private var newsarraylength: int;
// Send a request to read XML file information
Public Function getresult (): void {
Feed. url = "http: // 127.0.0.1: 8080/webroot/file/bigads. xml"; // XML file path
Feed. Send (null );
}
// Post-processing function for XML request success
Public Function resulthandler (Event: resultevent): void {
Canvasid. setstyle ("backgroundimage", picurl + "5.jpg ");
Title. Text = "HA 1 ";
Newslistxml = XML (event. Result );
Init ();
Autoplay ();
}
Public Function tititilestyle (): void {
VaR glowfilter: glowfilter = new glowfilter ();
Glowfilter. Color = 0 xffffff;
VaR samplefilters: array = title. filters;
Samplefilters. Push (glowfilter );
Title. Filters = samplefilters;
}
// Function to be processed when an error occurs
Public Function handlefault (Event: faultevent): void {
Alert. Show (event. fault. faultstring, "error ");
}
// Dynamically create tags
Public Function Init (): void {
Newslist = new xmllistcollection (newslistxml. Children (). Child (1). Children ());
VaR LB: label;
VaR vbox: vbox;
For (var I: Int = 0; I <newslist. length; I ++ ){
VaR newsobject: Object = newslist. getitemat (I );
Lb = new label ();
VaR picid: String = newsobject. @ PK;
LB. ID = "pic" + picid; // save ID
LB. Text = string (I + 1 );
VaR picname: String = newsobject. @ PK + ". jpg"; // Save the image path
LB. Name = picname; // name of the saved Image
LB. setstyle ("color", "# ffffff ");
LB. usehandcursor = true;
LB. buttonmode = true;
LB. mousechildren = false;
LB. Height = 15;
LB. width = 22;
VaR title: String = newsobject. Entry. (@ key = "title ");
Vbox = new vbox ();
Vbox. ID = "" + I;
Vbox. addchild (LB );
Vbox. stylename = "vbox ";
Addindexevent (LB, vbox); // dynamically add events to the Tx Tag Element
Hboxindexset. addchild (vbox );
VaR news: News = new news ();
News. seq = I;
News. ID = newsobject. @ PK;
News. Title = newsobject. Entry. (@ key = "title ");
News. IMG = newsobject. @ PK + ". jpg ";
News. indexbox = vbox;
Newsarray [I] = News;
}
Titilestyle ();
Newsarraylength = newsarray. length;
Currnews = newsarray [newsArrayLength-1];
Setinterval (function (): void {autoplay () ;}, 3000); // use setinterval in as2.0
/// Var tTime: timer = new timer (2000,0 );
/// TTime. addeventlistener (timerevent. Timer, function (): void {autoplay () ;}); // setinterval is replaced by time in as3.0
/// TTime. Start ();
}
// Dynamically add events for tags
Public Function addindexevent (tagetobject: object, box: vbox): void {// which tag ID is represented by parameter 1 and Variable Parameter 2
Tagetobject. addeventlistener (mouseevent. Click, function (): void {clickindex (box );});
}
// Process event Functions
Public Function clickindex (Box: vbox): void {
Totop (newsarray [box. ID])
}
// Automatic switch (playback)
Public Function autoplay (): void {
VaR currseq: Int = currnews. seq;
VaR nextseq: Int = (currseq + 1) % newsarraylength;
VaR nextnews: News = newsarray [nextseq];
Totop (nextnews );
}
// Switch between images and background colors
Public Function totop (nextnews: News): void {
Currnews. indexbox. setstyle ("backgroundcolor", "#000000 ");
Nextnews. indexbox. setstyle ("backgroundcolor", "Red ");
Canvasid. setstyle ("backgroundimage", picurl + nextnews. IMG );
Title. Text = nextnews. title;
Currnews = nextnews;
}
// Open a browser (unavailable for the moment)
// Public function showpic (): void {
// NavigatetoURL (New URLRequest ("http://www.baidu.com"), "_ blank ");
//}
]>
</MX: SCRIPT>
<Mx: httpservice id = "Feed" resultformat = "E4X" result = "resulthandler (event )"
Fault = "handlefault (event);" useproxy = "false"/>
<Mx: vbox width = "300" Height = "200" verticalalign = "bottom" verticalgap = "0"
Id = "canvasid" horizontalcenter = "0" verticalcenter = "0" verticalscrollpolicy = "off"
Horizontalscrollpolicy = "off">
<Mx: Label x = "123" Y = "180" text = "label" Height = "26"
Color = "#000000" fontsize = "20" fontweight = "bold" id = "title"/>
<Mx: hbox x = "211" Y = "180" width = "100%"
Backgroundcolor = "#000000" backgroundalpha = "0.50" horizontalalign = "right" id = "hboxindexset"
Horizontalgap = "1" horizontalscrollpolicy = "off"/>
</MX: vbox>
</MX: Application>
Http://www.javaeye.com/upload/attachment/35803/afcc61a9-46ec-3980-b4ac-cddbca1815e6.bmp