Microsoft Visual Studio 2010 Service Pack 1 includes a number of improvements and new features for MFC developers. One of these changes is an animation API to make it easy for you to create animations in MFC applications. This article will briefly introduce this animation API.
The main parts of this new API are:
ValuesAnimation transitionsAnimation animation controllersAnimation API uses transitions to animate values. The API comes with a number of pre-defined transitions:
CAccelerateDecelerateTransition: animated value speeds up and slows down again.CConstantTransition: animated value is preserved during the entire transition to its initial value.CCubicTransition: animated value reaches the target value with a specified speed.CDiscreteTransition: animated value will be to jump from the baseline value target after a specified period of time.CInstantaneousTransition: animated value immediately jumps to the target value. The duration is always zero.CLinearTransition: animated value will linearly from its initial value in its value target for a determined period.CLinearTransitionFromSpeed: is similar to CLinearTransition, but instead specify a time, you must specify the speed with which the animated value has to pass the initial value for the target value. The duration of the transition is automatically calculated according to the specified speed.CSmoothStopTransition: animated value will rise from its original value to the target value, but the transition will slow to set target to achieve the target with a speed equal to zero.CParabolicTransitionFromAcceleration: animated value will reach its target with a specified speed and acceleration.CReversalTransition: the direction of transition will be modified gently for a term. The final value of the animated value will be the same as the initial value.CSinusoidalTransitionFromRange: animated value will fluctuate between a specified minimum and maximum duration of the transition.CSinusoidalTransitionFromVelocity: animated value range of the initial value for the duration of the transition.Most of the above preset animation transitions are fairly simple to use. Each of them require at least the duration of the transition and most of them require a target value. For example, the transition from the CAccelerateDecelerateTransition is the following constructor:
CAccelerateDecelerateTransition (duration UI_ANIMATION_SECONDS, finalValue DOUBLE DOUBLE accelerationRatio = 0.3, DOUBLE decelerationRatio = 0.3)The first parameter is the duration of the transition. This is the time need to go to the value of departure to the target value. The second parameter is the target value. The next parameters are specific algorithmic parameters that have default values in this case.
CConstantTransition is an exceptional place. You keep the value of the animation to its initial value during the whole transition, its constructor is simply as follows:
CConstantTransition (UI_ANIMATION_SECONDS duration)There is also a CCustomTransition you can use any of the above transitions offer what you are looking for. It has a little more complicated to use. You will need to derive your own class from CCustomInterpolator and implement the InterpolateValue method and maybe a few others.
The following section discuss animation values and give some examples on how to effectively use the above animation transitions. Note that you must initialize COM before the animated Windows API function. You can do this by adding the following line in your InitInstance method:
CoInitialize (0) And the following line in your ExitInstance method: CoUninitialize(). Instead use CoInitialize and CoUninitialize, you can simply use the block in your InitInstance method: if (!) (AfxOleInit()) {AfxMessageBox (_T ("AfxOleInit failed")); return FALSE;}The new API works animation values. There are several classes that encapsulate specific values, which can be animated with some transitions. Different value animation classes are as follows:
CAnimationValue: Animates a single value. This could be used to animate a transparency of an object or a rotation of an object.CAnimationPoint: animates a point. Contains a point and coordinates X and Y. The two coordinates can be animated separately.CAnimationSize: anime size. Size contains a width and height, both can be animated separately.CAnimationColor: anime color. A color contains an RGB value. Each color component can be animated separately.CAnimationRect: animates a rectangle. A rectangle contains a left, top, right and bottom of value and all can be animated separately.It is relatively easy to create a value for the animation. Simply create an instance of the object value of animation that you want to, initialize, add transitions of the animation and specify its parameters as the target value. For example, to set a color animation value, first set as follows:
CAnimationColor m_animClr1;Then, the next line will initialize the color to Red:
m_animClr1 = RGB (255,0,0);And finally assign transitions for each color component.
m_animClr1.AddTransition (new CLinearTransition (2, 0), new CLinearTransition (1, 255), new CLinearTransition (0.5, 128));The line above adds 3 transitions:
The red color component will be increased from 255 (= startup value) over a period of 2 seconds.The green component color 0 pass 0 (= startup value) 255 over a period of 1 seconds.The blue component color ranges from 0 (= startup value) to 128 over a period of 0.5 seconds.Similarly, an animation of rectangle could be defined as follows:
CAnimationRect m_animRect; m_animRect = CRect (0, 0, 100, 100). m_animRect.AddTransition (new CAccelerateDecelerateTransition (2, 100), new CAccelerateDecelerateTransition (2, 100), new (2400) CAccelerateDecelerateTransition new CAccelerateDecelerateTransition (2,200));This animation starts with a rectangle with coordinates at the top left (0, 0) and bottom right (100, 100). Transitions will be animating this rectangle to a contact at the top left (100, 100) and a lower-right, 400, 200) over a period of 2 seconds to an acceleration-deceleration transition.
Inside your WM_PAINT handler, you will use the values of your variables animation to your drawing. For example:
int x 1, y1, x 2, y2; m_animRect.GetLeft .GetValue (X 1) (); m_animRect.GetTop () .GetValue (Y1); m_animRect.GetRight (.GetValue (X 2)); m_animRect.GetBottom (.GetValue (Y2)); COLORREF clr. m_animClr1.GetValue (CLR); pDC-> FillSolidRect (x 1, y1, x 2-x 1, y1 - y2, clr).The above code is first query the rectangle hosted on its current animated coordinates and color variable to its current value of the color. Subsequently it simply draws a rectangle with the coordinates and color.
Work with other classes of animation value is somewhat similar.
No comments:
Post a Comment