Cool ItemRenderers Made Easy in Flex 4

Cool ItemRenderers Made Easy in Flex 4

Flex 4 makes a lot of things a lot easier to build. After looking at custom components and skins in my previous post, here are a couple of samples focusing on ItemRenderers, States, and Animations.

In this first example, I use the new “depth” attribute together with the new States syntax to make sure that the hovered ItemRenderer is always on top of the other ones. I also use a PostLayoutTransform to “zoom” the hovered ItemRenderer without any impact on the position of the other ItemRenderers in this TileLayout.


Here is the source code for the ItemRenderer.

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
				xmlns:s="library://ns.adobe.com/flex/spark"
				xmlns:mx="library://ns.adobe.com/flex/mx"
				autoDrawBackground="false"
				depth="0" depth.hovered="1">

	<s:states>
		<s:State name="normal" />
		<s:State name="hovered" />
	</s:states>

	<s:postLayoutTransformOffsets>
		<mx:TransformOffsets id="offsets" x.hovered="-40" y.hovered="-40" scaleX.hovered="2" scaleY.hovered="2" />
	</s:postLayoutTransformOffsets>	

	<s:transitions>
		<mx:Transition fromState="normal" toState="hovered" autoReverse="true">
			<s:Animate target="{offsets}" duration="200">
				<s:SimpleMotionPath property="scaleX" />
				<s:SimpleMotionPath property="scaleY" />
				<s:SimpleMotionPath property="x" />
				<s:SimpleMotionPath property="y" />
			</s:Animate>
		</mx:Transition>
		<mx:Transition fromState="hovered" toState="normal" autoReverse="true">
			<s:Animate target="{offsets}" duration="200">
				<s:SimpleMotionPath property="scaleX" />
				<s:SimpleMotionPath property="scaleY" />
				<s:SimpleMotionPath property="x" />
				<s:SimpleMotionPath property="y" />
			</s:Animate>
		</mx:Transition>
	</s:transitions>	

	<mx:Image id="image" source="{data.image}" width="80" height="80" horizontalCenter="0" verticalCenter="0"/>

</s:ItemRenderer>

In this second example, the ItemRenderer uses a simple Rotate3D effect.

Here is the source code for the ItemRenderer.

<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
				xmlns:s="library://ns.adobe.com/flex/spark"
				xmlns:mx="library://ns.adobe.com/flex/mx"
				autoDrawBackground="false"
				depth="0" depth.hovered="1">

	<s:states>
		<s:State name="normal" />
		<s:State name="hovered" />
	</s:states>

	<s:transitions>
		<s:Transition fromState="normal" toState="hovered" autoReverse="true">
			<s:Parallel>
				<s:Rotate3D target="{image}" angleYFrom="0" angleYTo="360" autoCenterProjection="true" autoCenterTransform="true" duration="400"/>
				<s:Fade target="{image}" startDelay="400" duration="200"/>
				<s:Fade target="{group}" startDelay="400" duration="200"/>
			</s:Parallel>
		</s:Transition>
	</s:transitions>

	<s:Rect id="rect" left="0" right="0" top="0" bottom="0">
		<s:fill>
			<s:SolidColor color="black" alpha="0.7"/>
		</s:fill>
	</s:Rect>

	<s:Group id="group" left="0" right="0" top="0" bottom="0" visible.normal="false">
		<s:Label text="{data.firstName}" verticalCenter="-10" horizontalCenter="0" color="white"/>
		<s:Label text="{data.lastName}" verticalCenter="10" horizontalCenter="0" color="white"/>
	</s:Group>

	<mx:Image id="image" source="{data.image}" width="80" height="80" horizontalCenter="0" verticalCenter="0" visible.hovered="false"/>

</s:ItemRenderer>

You can view the complete source code for the application here.

You can download the source code here.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • LinkedIn
  • StumbleUpon
  • Twitter
This entry was posted in Flex. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

4 Comments

  1. Ross R
    Posted January 28, 2010 at 1:33 pm | Permalink

    2 things:

    1. Why did you use scaleX and scaleY to enlarge the image in the first example? It seems to me it would have been simpler to animate on the Z axis.

    2. When you MouseOut of the item its depth reverts immediately to 0, causing it to fall behind the next object in the grid as it animates back down to the start position. How would you remedy this?

  2. Faruk
    Posted January 29, 2010 at 2:44 am | Permalink

    Hi Christophe;

    These are really cool samples, I am following your blog and you always do really good exercises.

    And one more thing;
    I am waiting a RobotLegs sample from you, I hope you have time for that

  3. John P
    Posted January 30, 2010 at 4:31 pm | Permalink

    Christophe,

    Fun stuff. Looking forward to using your expertise to build some knockout prototypes and building compelling BI User Interfaces with the Attivio platform to solve customer problems.

  4. Dennis
    Posted July 25, 2010 at 4:35 pm | Permalink

    Hi Christophe,

    I try-out to run the project but i’m missing the lib, i’m using Flash builder SDK 4.0.0

    I loved your solution because im looking for a nice Image grid example like this

    all the best,

    Dennis

One Trackback

  1. By More Flex 4 / Spark ItemRenderer Samples on February 1, 2010 at 10:24 pm

    [...] Christophe Coenraets Rich Internet Applications, Flex, AIR, Java Skip to content BioUsing Flex with Spring « Cool ItemRenderers Made Easy in Flex 4 [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>