31 March, 2010

Creating an XML / Flash Slideshow with Captions

  1. Introduction
  2. Creating External Assets
  3. Processing XML File
  4. Creating Movie Clip Container
  5. Calling Images from Source Folder
  6. Implementing Transition Effect
  7. Points to Ponder
  8. Final Words

Introduction

In order to improve the quality of visitor’s experience it is often needed to make a website mix of rich contents including graphics and animation etc. As a very prominent say express that an image is worth of thousand words, so how gigantic can an animation be… let us presume.

This article guides about to create a Flash Slideshow using XML. Usually, when we create a slideshow in flash, all the images get integrated into the movie itself, doing this slashes the reusability of the movie file (swf) and updatability of the movie content. Here I created an information bridge between the source of my images and flash movie (swf) that is through XML file.

Every time when the flash movie has been played, it looks for information from XML file. That provides parameters like Width, Height, Speed of Slide show and the source of images and their titles (captions) as well. Once flash movie gets all information that is been processed to be integrated in flash itself and the effect would be the same if we don’t put an XML file in between. But use of XML file eases the updatability of content and the reuse of flash movie at other places (websites). Moreover, I have used transition class to enhance the feel of sliding effect between images.

Creating External Assets

External assets mean to prepare all the images of slideshow, XML information file and an FLA project.

First of all create a directory (folder) name is ‘Slideshow’. Now create one more directory inside it and name as ‘images’. Put all the images you want to show on slide in this folder (images). To get an impressive effect keep the dimension (width and height) of all images equal.

Now go back to your ‘Slideshow’ directory, create an XML file here format of XML file must be like this:

Collapse
<slideshow width="475" height="390" speed="2"> 	<image url="images/gal1_img1.jpg" title="Product 1"/> 	<image url="images/gal1_img2.jpg" title="Product 2"/> 	<image url="images/gal1_img3.jpg" title="Product 3"/> 	<image url="images/gal1_img4.jpg" title="Product 4"/> 	<image url="images/gal1_img5.jpg" title="Product 5"/> 	<image url="images/gal1_img6.jpg" title="Product 6"/> 	<image url="images/gal1_img7.jpg" title="Product 7"/> 	<image url="images/gal1_img8.jpg" title="Product 8"/> 	<image url="images/gal1_img9.jpg" title="Product 9"/> 	<image url="images/gal1_img10.jpg" title="Product 10"/> </slideshow>  

See the <slideshow> have three attributes, width, height and speed. Give values according to your requirement. Remember that the unit of values is pixels (px) for width and height and seconds (sec) for speed. Now inside<slideshow> node put as many image <image> nodes as you want. This (<image>) node has two attributes url and title. URL attribute has to be valued with a relative url of each image file from the place where you will be putting .SWF or .FLA file and TITLE attribute represents the text which you want to show with slide. Name your XML as ‘slideshow.xml’.

Finally, you need to create a .FLA file in your ‘Slideshow’ directory. Create new flash document, set its dimensions as 500x500 (in pixels) and set frames rate as 30 (fps). Now write this code for the actions of frame1 of layer1:

Collapse
import mx.transitions.Tween; import mx.transitions.easing.*;  

Above two lines have been added to use methods of tween class, these help us to enhance the effect of sliding. You’ll see in moveSlide() function I’m using Tween and also accessing easeOut property.

Now we have all our external assets ready to use and will be coding for processing of XML information, calling & integrating images, and then moving slides. All this code has to be written for actions of frame1 of layer1.

Processing XML File

To process XML file we need to add this code (just below our import statements) for actions of frame1 of layer1.

Collapse
var myShowXML = new XML(); myShowXML.ignoreWhite = true; myShowXML.load("slideshow.xml");  myShowXML.onLoad = function() { 	_root.myWidth = myShowXML.firstChild.attributes.width; 	_root.myHeight = myShowXML.firstChild.attributes.height; 	_root.mySpeed = myShowXML.firstChild.attributes.speed; 	_root.myImages = myShowXML.firstChild.childNodes; 	_root.myImagesNo = myImages.length;  	createContainer();    // Used to create an empty movie clip (movie container) 	callImages();         // Used to extract and push all the images into an array };  

Here we are using an object (myShowXML) of type XML Class. This object will represent our information file, which is an XML file. For this object ‘ignoreWhite’ property has been set to true, this is simply to ignore white space which has been put between XML node in slideshow.xml file. Load method has been used to pass the path of XML file which has to be looked in for slide information.

While loading XML file (at onLoad event), we are going to collect all the information into variables reside at the _root of movie, _root usually represents the current executing clip on time line. I created variables like myWidth, myHeight etc and given them values extracted from XML file. Now createContainer() function has been called which function is responsible to create an empty movie clip (container). Once empty movie clip will be created another functioncallImages() will be call which is responsible to collect images and put them into an array. Inside this another functionmoveSlide() will be called on a regular interval of specified time (time value specified for speed attribute in XML file).

Creating Movie Clip Container

The code which represents our createContainer() function is given below:

Collapse
function createContainer()  { 	_root.createEmptyMovieClip("myContainer",_root.getNextHighestDepth());  	myContainer.lineStyle(5,0x000000,100); 	myContainer.lineTo(_root.myWidth,0); 	myContainer.lineTo(_root.myWidth,_root.myHeight); 	myContainer.lineTo(0,_root.myHeight); 	myContainer.lineTo(0,0);  	myContainer._x = (Stage.width-myContainer._width)/2; 	myContainer._y = (Stage.height-myContainer._height)/2; }  

In first line of above code a method to create an empty movie clip has been called. First parameter "myContainer" represents the name of container and second parameter is used to extract highest depth of layers and levels in movie. Second parameter _root.getNextHighestDepth() ensures that Flash renders the movie clip in front of all other objects on the same level and layer in the current movie clip.

In next few lines I have set a border to the container. First of all I have given a style to this border using statementmyContainer.lineStyle(5,0x000000,100);. Here first parameter (5 in my case) represents the width of border, second parameter (0x000000 in my case) represents hexadecimal code of border color, and the third parameter (100 in my case) represents the opacity (or alpha) value of border.

Once the style of the border has been set, the program start plotting it from top left corner of the container and goes to the top right corner of the container, then to bottom right corner of the container, then to bottom left corner of the container and finally come back to top left corner of the container. All this has been done in four statements written after styling statement. See there is use of myWidth and myHeight variables values of which has been extracted from XML file.

Now we have an empty container on stage which is stated at the top left corner of document. That is if I have set width=300 and height=300 in XML file and the width and height of my document in FLA file are 500 and 500. My container will be at top left corner with having white space on document. So last two statements have been used to set the position of container on stage, in all cases the container will be centered horizontally and will be set to the middle on document. This has been done by changing the coordinates of top left corner of container from 0, 0 to some other value calculated suing width and height of container as well as stage.

Calling Images from Source Folder

It is time to call images from folder and put them into action.

Collapse
function callImages()  { 	_root.myMCL = new MovieClipLoader(); 	_root.myPreloader = new Object(); 	_root.myClips_array = [];  	_root.myMCL.addListener(_root.myPreloader);  	_root.myPreloader.onLoadStart = function(target) {                 _root.createTextField("myText_txt",_root.getNextHighestDepth(),0,0,100,20); 		_root.myText_txt._x = (Stage.width-_root.myText_txt._width)/2; 		_root.myText_txt._y = (Stage.height-_root.myText_txt._height)/2; 		_root.myText_txt.autoSize = "center"; 	};  	_root.myPreloader.onLoadProgress = function(target) { 		_root.myText_txt.text = "Loading.. "+_root.myClips_array.length+"/"+_root.myImagesNo+" Completed"; 	};  	_root.myPreloader.onLoadComplete = function(target) { 		_root.myClips_array.push(target); 		target._alpha = 0; 		if (_root.myClips_array.length == _root.myImagesNo)                 { 			_root.myText_txt._y = myContainer._y + myContainer._height; 			_root.target_mc = -1; 			moveSlide();                        setInterval(moveSlide,(_root.mySpeed*1000)+1000); 		} 	};  	for (i=0; i<_root.myimagesno; temp_url =" _root.myImages[i].attributes.url;" temp_mc =" myContainer.createEmptyMovieClip(i,">

In top few lines I have declared a MovieClipLoader object, a Preloader object and an array put clips. Initially MovieClipLoader object has been filled in with Preloader object.

At onLoadStart event of Preloader object, a blank TextField has been created and centered into movie container. At onLoadProgress event of Preloader object, this text field is been assigned progress values. That is, loading this image from these number of images. At onLoadComplete event of Preloader object images have been pushed into an array and the value of alpha for current target has been set zero. Now the vertical position of TextField has been changed to put it at the bottom of Movie clip container and the current target image has been set to a negative value. Finally in this eventmoveSlide() function has been called in for regular intervals of the time that has been extracted from XML file (speed value in seconds).

The moveSlide() function is responsible to apply transition effects on current clip to fade out and then by incrementing the value of current target apply effects on new image to fade in. This is also responsible to feed into the caption field the corresponding value that has been extracted from XML file for current slide. Later in this article moveSlide()function will be discussed in detail.

Now come back the bottom few lines of callImages() function, here is a loop which runs for every image in myImagesarray and create a MoveClip for current image in our base Movie clip container. This clip has then loaded into MovieClipLoader object. This has to be done for all the images in Array. In simple words this loop is the responsible to put results by using what has been buffered for long in program.

Implementing Transition Effect

As I wrote multiple times in above context that moveSlide() function is responsible to set transition effects in our flash slideshow. Below is the code for moveSlide() function:

Collapse
function moveSlide()  { 	current_mc = _root.myClips_array[_root.target_mc]; 	new Tween(current_mc, "_alpha", Strong.easeOut, 100, 0, 1, true);  	_root.target_mc++;  	if (_root.target_mc>=_root.myImagesNo) { 		_root.target_mc = 0; 	}  	_root.myText_txt.text = _root.myImages[target_mc].attributes.title; 	next_mc = _root.myClips_array[_root.target_mc]; 	new Tween(next_mc, "_alpha", Strong.easeOut, 0, 100, 1, true); }  

In top two line of above function the current target has been extracted from clips array, this has now been dulled with an easeOut effect. Now the value of movie clip target has been incremented, there is a condition in between to check whether the cycle has been completed if yes value of movie clip target has been assigned as zero to recycle slideshow. In bottom three lines of above function the title value (caption) of current image has been extracted and fed into text field then the clip has been assigned to be next movie clip and sharpened with easeOut effect on document.

Finally we get an XML / Slideshow working and this is easier to reuse with ease of content updatability. Now come to your excitement while testing your flash movie.

Points to Ponder

Practical stuff is attached as Demo. You will easily understand when download. Remember for this program to work you need Flash Version 7.0 or later.

Final Wordsto Ponder

I hope you find the stuff helpful. Thanks for reading. Good Luck

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mohd Arshad (Sam)


Member

Software professional with demonstrated strength in windows-based and web-based software development. Have 4 years of experience with the full software development lifecycle including requirements, design, development, testing/QA, deployment, training & support. Have 1 year experience managing groups, planning and executing implementations. Practical working knowledge of all aspects of IT. Possess strong insight into practical business considerations.


www.cherisys.com
www.webstreet.in
www.mohdarshad.in


Occupation:Software Developer (Senior)
Company:Cherisys Technologies, WebStreet.in
Location:India India

What’s new in RadChart for 2010 Q1 (Silverlight / WPF)

What’s new in RadChart for 2010 Q1 (Silverlight / WPF): "

Greetings, RadChart fans! It is with great pleasure that I present this short highlight of our accomplishments for the Q1 release :). Weve worked very hard to make the best silverlight and WPF charting product even better. Here is some of what we did during the past few months.

 

1) Zooming&Scrolling and the new sampling engine:

Without a doubt one of the most important things we did. This new feature allows you to bind your chart to a very large set of data with blazing performance. Dont take my word for it give it a try!

 title=

 

2) New Smart Label Positioning and Spider-like labels feature:

This new feature really helps with very busy graphs. You can play with the different settings we offer in this example.

RadChart_SmartLabels

 

 

3) Sorting and Filtering.

Much like our RadGridview control the chart now allows you to sort and filter your data out of the box with a single line of code!

RadChart_Sorting_Filter

 

4) Legend improvements

Weve also been paying attention to those of you who wanted a much improved legend. It is now possible to customize the look and feel of legend items and legend position with a single click.

RadChart_LegendFeatures

 

 

5) Custom palette brushes.

You have told us that you want to easily customize all palette colors using a single clean API from both XAML and code behind. The new custom palette brushes API does exactly that.

RadChart_CustomPalettes

 

There are numerous other improvements as well, as much improved themes, performance optimizations and other features that we did. If you want to dig in further check the release notes and changes and backwards compatibility topics.

 

Feel free to share the pains and gains of working with RadChart. Our team is always open to receiving constructive feedback and beer :-)

Did you know that DotNetSlackers also publishes .net articles written by top known .net Authors? We already have over 80 articles in several categories including Silverlight. Take a look: here.





Email this Article


"

Building Interactive User Interfaces with Microsoft ASP.NET AJAX: Refreshing An UpdatePanel With JavaScript

Building Interactive User Interfaces with Microsoft ASP.NET AJAX: Refreshing An UpdatePanel With JavaScript: "


The ASP.NET AJAX UpdatePanel provides a quick and easy way to implement a snappier, AJAX-based user interface in an ASP.NET WebForm. In a nutshell, UpdatePanels allow page
developers to refresh selected parts of the page (instead of refreshing the entire page). Typically, an UpdatePanel contains user interface elements that would normally trigger
a full page postback - controls like Buttons or DropDownLists that have their AutoPostBack property set to True. Such controls, when placed inside an UpdatePanel,
cause a partial page postback to occur. On a partial page postback only the contents of the UpdatePanel are refreshed, avoiding the 'flash' of having the entire page reloaded.
(For a more in-depth look at the UpdatePanel control, refer back to the Using the UpdatePanel
installment in this article series.)


Triggering a partial page postback refreshes the contents within an UpdatePanel, but what if you want to refresh an UpdatePanel's contents via JavaScript? Ideally,
the UpdatePanel would have a client-side function named something like Refresh that could be called from script to perform a partial page postback and refresh
the UpdatePanel. Unfortunately, no such function exists. Instead, you have to write script that triggers a partial page postback for the UpdatePanel you want to refresh.
This article looks at how to accomplish this using just a single line of markup/script and includes a working demo you can download and try out for yourself.
Read on to learn more!

Read More >

Did you know that DotNetSlackers also publishes .net articles written by top known .net Authors? We already have over 80 articles in several categories including Silverlight. Take a look: here.





Email this Article


"

TOP 5 costly Stored Procedures in a SQL Server Database

TOP 5 costly Stored Procedures in a SQL Server Database: "

I was recently doing a query to find out the Stored Procedures which took maximum time to execute. Here’s the query (thanks to gbn) I executed on a sample database (AdventureWorks) using SQL Server 2005/2008:

SELECT TOP 5 obj.name, max_logical_reads, max_elapsed_time
FROM sys.dm_exec_query_stats a
CROSS APPLY sys.dm_exec_sql_text(sql_handle) hnd
INNER JOIN sys.sysobjects obj on hnd.objectid = obj.id
ORDER BY max_logical_reads DESC

OUTPUT

image

If you know of a better way, please share it in the comments section.

"

GROUPING SETS in SQL Server 2008

GROUPING SETS in SQL Server 2008: "

With the new GROUPING SETS operator included in SQL Server 2008, you have more control over what is aggregated. Here’s a practical example of using GROUPING SETS in SQL Server 2008

SAMPLE DATA

--DROP TABLE #Student

CREATE TABLE #Student
(
StudentID int ,
CourseYear smallint,
Semester smallint,
Marks float
)

INSERT INTO #Student VALUES (1, 2008, 1, 5.6)
INSERT INTO #Student VALUES (1, 2008, 2, 6.5)
INSERT INTO #Student VALUES (1, 2008, 3, 8.9)
INSERT INTO #Student VALUES (1, 2008, 4, 9.1)
INSERT INTO #Student VALUES (1, 2009, 1, 4.4)
INSERT INTO #Student VALUES (1, 2009, 2, 7.9)
INSERT INTO #Student VALUES (1, 2009, 3, 8.5)
INSERT INTO #Student VALUES (1, 2009, 4, 8.7)
INSERT INTO #Student VALUES (2, 2008, 1, 5.4)
INSERT INTO #Student VALUES (2, 2008, 2, 9.9)
INSERT INTO #Student VALUES (2, 2008, 3, 8.5)
INSERT INTO #Student VALUES (2, 2008, 4, 4.7)
INSERT INTO #Student VALUES (2, 2009, 1, 6.4)
INSERT INTO #Student VALUES (2, 2009, 2, 7.9)
INSERT INTO #Student VALUES (2, 2009, 3, 7.4)
INSERT INTO #Student VALUES (2, 2009, 4, 9.7)

Let’s do some common GROUPBY operations

-- Calculate Total Marks obtained by student each year
SELECT StudentID, CourseYear, SUM(Marks) AS TotalMarks
FROM #Student
GROUP BY StudentID, CourseYear
ORDER BY StudentID

GROUPING SETS SQLServer

-- Calculate Total Marks obtained by student
-- grouped by Semester for both years
SELECT StudentID, Semester, SUM(Marks) AS TotalMarks
FROM #Student
GROUP BY StudentID, Semester
ORDER BY StudentID

GROUPING SETS SQLServer

However with the GROUPING SETS operator, you can define different aggregate groups in a single query as shown below

SELECT StudentID, CourseYear, Semester, SUM(Marks) AS TotMarks
FROM #Student
GROUP BY GROUPING SETS((StudentID, CourseYear), (StudentID, Semester))
ORDER BY StudentID, Semester

GROUPING SETS SQLServer

Here’s a very nice article by Craig on GROUPING SETS in SQL Server 2008

"

Find Primary Key of a SQL Server Table

Find Primary Key of a SQL Server Table: "

Here’s a quick way to find the primary key of a SQL Server 2005/2008 Table using TABLE_CONSTRAINTS

USE Northwind
go
SELECT
ISKC.TABLE_SCHEMA, ISKC.TABLE_NAME, ISKC.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS ISTC
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS ISKC
ON ISTC.CONSTRAINT_TYPE = 'PRIMARY KEY' AND
ISTC.CONSTRAINT_NAME = ISKC.CONSTRAINT_NAME
WHERE ISKC.TABLE_NAME = 'Employees'
ORDER BY ISKC.TABLE_NAME, ISKC.ORDINAL_POSITION

Primary Key SQL Server

Another way that I know of is to make a join between sys.indexes, sys.index_columns and sys.columns and get the desired results.

"