Windows Phone Mango Sample: 5 Must-Have Animations Pt. 2
Join the DZone community and get the full member experience.Join For Free
this is the second part in jerry nixon's series on how to create the most popular animations for your windows phone apps. animations include a peel-away animation for the tiles and some neat selection and loading animnations.
this article is part 2 in a three part series.
let’s consider my favorite animations in native windows phone animations. i believe that implementing these basic animations can turn your application on its head.
animation 1: the turnstile
this animation “peels” off each listboxitem in a listbox one at a time (from bottom to top when transitioning out, from top to bottom when transitioning in) until the list is gone. animating the projectionplane, this gives the appearance that items are coming toward you.
the start menu uses this animation with the tiles. the application list off the start menu uses this animation with the list items.
this animation takes less than a second. it has two metro-oriented characteristics. the first is a generic illustration that the page is transitioning (the list is going away) – of course our user doesn’t know what a “page” is. second, the currently selected item transitions last. it’s subtle, but reassures the user their selection was properly accounted.
animation 2: select item
this animation selects an item in a list. it is subtle. the selected item is depressed slightly. this is accomplished by using a scaletransform to animate the scalex and scaley properties from 1 to .75 (or some other decimal). it requires the centerx and centery properties to be set to 50% of the listboxitem rendersize width.
note: a sophisticated version of this animation leverages the projectionplane to depress the selected item to the left or right side, based on the user’s touch. this additional complexity might be a bit too subtle for my taste – it also provides considerably little value to the user. fyi: examples of this version are everywhere. it is email, calendar, the application list off the start menu, even the tiles on the start menu.
i am basing this example on the user tapping a listboxitem in a listbox. we’ll handle the selectionchanged event, like this:
in the code above, see the itemcontainergenerator line? this is the class in a listbox that let’s us access the selected listboxitem.
also, see the todo? this anonymous method let’s us do something, like navigate away from this page, as a result of the user’s selection.
in the code above, see how we are setting the element’s rendertransform property to a new scaletransform ? we must assume there is no transform on the item – and we will need it in order to animate it. our scaletransform sets the centerx and centery properties so that it will resize to the center of the existing rendersize. in the event that you already have such a transform in your xaml, then you will need to make some adjustments. then we create the initial storyboard and timeline.
in the code above, we are creating two double animations (for a single storyboard). one to animate scalex and another to animate scaley. we want the element to uniformly animate, so we must animate both. they are basically identical animations. their durations are inherited from the duration of the storyboard we set in the previous code.
in the code above, we wrap up this method by handling the completed event . in this case, i am using an anonymous method as a callback . if you need a refresher on callbacks, check out my previous post . we need to handle the completed event so we can do something after the animation is finished. then we call begin () to start the animation.
get the code here .
animation 3: load item
this animation is so simple and so pleasant. instead of popping a new page’s content, content is gently raised and revealed. the raising is accomplished through translatetransform . y animation. the reveal is accomplished through opacity animation . the whole storyboard duration is .75 seconds. it gives the user that the content is “being brought in”.
the loaded event is the right time to execute this animation. it is important that the layoutroot element in xaml (or whatever you call your root element) have its visibility property set to collapsed. this way the reveal is smooth.
in the code above, i am handling the loaded event, testing the visibility value and calling showhide(), my method to animate the layoutroot element. setting _topfrom to the page’s height let’s the slide animation cross the full height of the phone. it’s a pleasant effect.
in the code above, i am setting up the translatetransform that will let me move the layoutroot from the bottom of the screen to the top. i default the visibility and opacity property of the layoutroot – then later i will start the animation from those values.
in the code above, i am setting up two animations. the first is a double animation for opacity. this animation will animate from opacityfrom to opacityto (both method arguments). the second is a double animation for the x property of the translatetransform. zero is the original position.
in the code above, i am handling the completed event. because showhide() is a generic method for both showing and hiding, it uses a callback. since we are discussing the item load animation to reveal the content, we know that there is nothing to do in the callback other than just let it load.
get all the code here .
animation 4: unload item
the unload item animation is the opposite of the load item animation discussed above. instead of revealing the item from the bottom, it dissolves the content to the bottom. the relationship between the two and the consistency with the rest of the system is very communicative to the user.
in the code above, notice that this is a simple inverse of the reveal logic. we are sending the top to the bottom of the page, and animating opacity to 0. when it is done, we implement the callback to goback (). note that in your code goback () may not be a valid action. update this code to fit your app.
get all the code here .
continue to part 3 .
Opinions expressed by DZone contributors are their own.