Animation learning step by step [1]: Application of animation in Silverlight

Source: Internet
Author: User
1. Introduction to animation
Animation is an illusion of rapid playback of a series of images (each of which is slightly different from the next image. The brain feels like a changed scene. In a movie, a camera takes many photos (frames) every second to form this illusion. When you use a projector to play these frames, the audience can watch a movie. In Silverlight, objects can be animated by applying animations to individual properties of objects. For example, to increase the size of a uielement, You Need To animation its width and height attributes. To gradually remove a uielement from its field of view, you can animation its opacity attribute. You can animation attributes of many objects in Silverlight.

Note: In Silverlight, you can only set the value typeDouble, color, or pointTo perform simple animation processing. You can also useObjectanimationusingkeyframesAnimation of other types of attributes, but this requiresDiscrete Interpolation(Jump from one value to another), and most people think this is not a real animation.

2. animation example: fade-in and fade-out effects of objects on the page
This example usesDoubleanimation(An animation type that generates double values) animations the opacity attribute of rectangle. Therefore, rectangle gradually enters the field of view and disappears from the field of view.
[1] Create a rectangle element:

 
<Stackpanel> <rectangle mouseleftbuttondown = "mouse_clicked" X: Name = "myanimatedrectangle" width = "100" Height = "100" fill = "blue"/> </stackpanel>

[2] Create doubleanimation:
Because the opacity attribute is of the double type, an animation that generates the double value is required. Doubleanimation is an animation that creates a transition between two double values. To specify the start value of doubleanimation, you can set its from attribute. To specify its termination value, you can set its to attribute.

<Doubleanimation from = "1.0" to = "0.0" Duration = "" autoreverse = "true" repeatbehavior = "forever"/>

Duration refers to the time required to transition from the initial value to the target value. Autoreverse indicates that the animation will run repeatedly, while repeatbehavior indicates that the animation will be repeated indefinitely.

[3] Create a storyboard object:

 
<Storyboard> <doubleanimation from = "1.0" to = "0.0" Duration = "" autoreverse = "true" repeatbehavior = "forever"/> </storyboard>

The aboveCodeCreates a storyboard and places the animation in it. Then, you need to use the targetname additional attribute to specify the object to be animated. In the following code, a target name myanimatedrectangle is specified for doubleanimation, which is the name of the object to be animated.

<Storyboard> <doubleanimationstoryboard. targetname = "myanimatedrectangle" from = "1.0" to = "0.0" Duration = "" autoreverse = "true" repeatbehavior = "forever"/> </storyboard>

Finally, use the targetproperty append property to specify the property for animation processing. In the following code, the animation is configured as the opacity attribute for rectangle.

 
<Storyboard> <doubleanimationstoryboard. targetname = "myanimatedrectangle" storyboard. targetproperty = "opacity" from = "1.0" to = "0.0" Duration = "" autoreverse = "true" repeatbehavior = "forever"/> </storyboard>

[4] Associate storyboard with events
Next, you need to specify when to start playing the animation. You can use events to perform this operation.
1. Use the Demo Board as a resource. Place the storyboard in a resource block so that you can reference the storyboard from code easily to start, stop, pause, and continue operations. The following tag displays the storyboard declared in the resource block of the stackpanel object. Note that you can declare storyboard in any resource block, as long as the resource block and the object you want to perform animation processing are in the same scope. The following code:

 
<Stackpanel> <stackpanel. Resources> <! -- Animates the rectangle's opacity. --> <storyboard X: Name = "mystoryboard"> <doubleanimationstoryboard. targetname = "myanimatedrectangle" storyboard. targetproperty = "opacity" from = "1.0" to = "0.0" Duration = "" autoreverse = "true" repeatbehavior = "forever"/> </storyboard> </stackpanel. resources> <rectanglex: Name = "myanimatedrectangle" width = "100" Height = "100" fill = "blue"/> </stackpanel>

2. attach an event to an element. You can use multiple events to start an animation, including mouse-related events, such as mouseleftbuttondown triggered when you click an object, or loaded events triggered when an object is loaded for the first time. For more information about events, see Silverlight event overview. In this example, the mouseleftbuttondown event is appended to the rectangle so that the event is triggered when you click the rectangle.

 
<Rectangle mouseleftbuttondown = "mouse_clicked" X: Name = "myanimatedrectangle" width = "100" Height = "100" fill = "blue"/>

3. Process eventsProgramControl the animation. Storyboard provides multiple methods that allow you to control the playing of storyboard animations, including begin, stop, pause, and resume. This example uses the begin method. This method starts an animation when the user clicks a rectangle and raises the mouseleftbuttondown event.

 
// When the user clicks the rectangle, the animation begins. Private void mouse_clicked (Object sender, mouseeventargs e) {mystoryboard. Begin ();}

[5] The final run code is:

 
   
    
     
     
      
     
    
   
     click on the rectangle to start the animation. 
    
     
   // background code private void mouse_clicked (Object sender, mouseeventargs e) {mystoryboard. begin () ;}

reference: msdn

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.