管理类"/>
Silverlight StoryboardManager 故事板管理类
写的不好,希望志同道合者给点建议,或者直接修改来完善它,当前版本暂定为V1.0
通过简单的设置ScaleDoubleAnimation、SkewDoubleAnimation、RotateDoubleAnimation、TranslateDoubleAnimation属性快速的实现放大缩小、倾斜、旋转、位移的动画效果
然后执行 storyboardManager.Storyboard.Begin();即可实现简单的动画效果,也可以同时给多个属性赋值,实现多种动画效果组合。
目前还不太灵活,希望大家帮忙改进,或提一些宝贵意见,谢谢
具体使用方法在下面
StoryboardManager全部代码
1 using System;
2 using System.Net;
3 using System.Windows;
4 using System.Windows.Controls;
5 using System.Windows.Documents;
6 using System.Windows.Ink;
7 using System.Windows.Input;
8 using System.Windows.Media;
9 using System.Windows.Media.Animation;
10 using System.Windows.Shapes;
11
12 namespace Alex.Silverlight.Controls
13 {
14 /// <summary>
15 /// 作者:王庆华
16 /// 日期:2009-6-13
17 /// 版本:1.0
18 /// 目的:抛砖引玉,请大家指教,共同进步。同时也希望能给那些刚刚开始学习Silverlight的学弟们一些帮助
19 /// </summary>
20 public class StoryboardManager
21 {
22 private const string PROPERTYPATH_PART = " (FrameworkElement.RenderTransform).(TransformGroup.Children)[ " ;
23
24 Storyboard _storyboard = null ;
25
26 public Storyboard Storyboard
27 {
28 get { return _storyboard; }
29 }
30
31 private FrameworkElement _frameworkElement;
32
33 public FrameworkElement FrameworkElement
34 {
35 get { return _frameworkElement; }
36 }
37
38 private ScaleDoubleAnimation _scaleDoubleAnimation = new ScaleDoubleAnimation();
39
40 public ScaleDoubleAnimation ScaleDoubleAnimation
41 {
42 get { return _scaleDoubleAnimation; }
43 set
44 {
45 _scaleDoubleAnimation = value;
46 }
47 }
48
49 private SkewDoubleAnimation _skewDoubleAnimation = new SkewDoubleAnimation();
50
51 public SkewDoubleAnimation SkewDoubleAnimation
52 {
53 get { return _skewDoubleAnimation; }
54 set
55 {
56 _skewDoubleAnimation = value;
57 }
58 }
59
60 private RotateDoubleAnimation _rotateDoubleAnimation = new RotateDoubleAnimation();
61
62 public RotateDoubleAnimation RotateDoubleAnimation
63 {
64 get { return _rotateDoubleAnimation; }
65 set
66 {
67 _rotateDoubleAnimation = value;
68 }
69 }
70
71 private TranslateDoubleAnimation _translateDoubleAnimation = new TranslateDoubleAnimation();
72
73 public TranslateDoubleAnimation TranslateDoubleAnimation
74 {
75 get { return _translateDoubleAnimation; }
76 set
77 {
78 _translateDoubleAnimation = value;
79 }
80 }
81
82 private Point _renderTransformOrigin = new Point( 0.5 , 0.5 );
83
84 public Point RenderTransformOrigin
85 {
86 get { return _renderTransformOrigin; }
87 set { _renderTransformOrigin = value; }
88 }
89
90
91 public StoryboardManager(FrameworkElement frameworkElement)
92 {
93 this ._frameworkElement = frameworkElement;
94
95 FrameworkElementSet();
96 _storyboard = new Storyboard();
97
98 _scaleDoubleAnimation.DoubleAnimationChange += new DoubleAnimationChangeEventHandler(DoubleAnimation_DoubleAnimationChange);
99 _skewDoubleAnimation.DoubleAnimationChange += new DoubleAnimationChangeEventHandler(DoubleAnimation_DoubleAnimationChange);
100 _rotateDoubleAnimation.DoubleAnimationChange += new DoubleAnimationChangeEventHandler(DoubleAnimation_DoubleAnimationChange);
101 _translateDoubleAnimation.DoubleAnimationChange += new DoubleAnimationChangeEventHandler(DoubleAnimation_DoubleAnimationChange);
102 }
103
104 void DoubleAnimation_DoubleAnimationChange( object sender, DoubleAnimationChangeEventArgs e)
105 {
106 DoubleAnimation doubleAnimation = (DoubleAnimation)sender;
107 _storyboard.Children.Add(doubleAnimation);
108 Storyboard.SetTarget(doubleAnimation, _frameworkElement);
109 Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath(PROPERTYPATH_PART + ( int )e.AnimationType + e.Propertypath, new object [] { }));
110 }
111
112 public void FrameworkElementSet()
113 {
114 _frameworkElement.RenderTransform = CreateTransformGroup();
115 _frameworkElement.RenderTransformOrigin = _renderTransformOrigin;
116 }
117
118 private TransformGroup CreateTransformGroup()
119 {
120 TransformGroup transformGroup = new TransformGroup();
121
122 ScaleTransform scaleTransform = new ScaleTransform();
123 SkewTransform skewTransform = new SkewTransform();
124 RotateTransform rotateTransform = new RotateTransform();
125 TranslateTransform translateTransform = new TranslateTransform();
126
127 transformGroup.Children.Add(scaleTransform);
128 transformGroup.Children.Add(skewTransform);
129 transformGroup.Children.Add(rotateTransform);
130 transformGroup.Children.Add(translateTransform);
131
132 return transformGroup;
133 }
134 }
135
136
137 public delegate void DoubleAnimationChangeEventHandler( object sender, DoubleAnimationChangeEventArgs e);
138
139 public class DoubleAnimationChangeEventArgs : EventArgs
140 {
141 public AnimationType AnimationType { set ; get ; }
142 public DoubleAnimation Value { set ; get ; }
143 public string Propertypath { set ; get ; }
144 }
145
146 public class ScaleDoubleAnimation
147 {
148 private DoubleAnimation _scaleX;
149
150 public DoubleAnimation ScaleX
151 {
152 get { return _scaleX; }
153 set
154 {
155 _scaleX = value;
156 if (DoubleAnimationChange != null )
157 DoubleAnimationChange( this .ScaleX, new DoubleAnimationChangeEventArgs()
158 {
159 AnimationType = AnimationType.Scale,
160 Value = value,
161 Propertypath = " ].(ScaleTransform.ScaleX) "
162 });
163 }
164 }
165
166 private DoubleAnimation _scaleY;
167
168 public DoubleAnimation ScaleY
169 {
170 get { return _scaleY; }
171 set
172 {
173 _scaleY = value;
174 if (DoubleAnimationChange != null )
175 DoubleAnimationChange( this .ScaleY, new DoubleAnimationChangeEventArgs()
176 {
177 AnimationType = AnimationType.Scale,
178 Value = value,
179 Propertypath = " ].(ScaleTransform.ScaleY) "
180 });
181 }
182 }
183
184 public event DoubleAnimationChangeEventHandler DoubleAnimationChange;
185 }
186
187 public class SkewDoubleAnimation
188 {
189 private DoubleAnimation _angleX;
190
191 public DoubleAnimation AngleX
192 {
193 get { return _angleX; }
194 set
195 {
196 _angleX = value;
197 if (DoubleAnimationChange != null )
198 DoubleAnimationChange( this .AngleX, new DoubleAnimationChangeEventArgs()
199 {
200 AnimationType = AnimationType.Skew,
201 Value = value,
202 Propertypath = " ].(SkewTransform.AngleX) "
203 });
204 }
205 }
206
207 private DoubleAnimation _angleY;
208
209 public DoubleAnimation AngleY
210 {
211 get { return _angleY; }
212 set
213 {
214 _angleY = value;
215 if (DoubleAnimationChange != null )
216 DoubleAnimationChange( this .AngleY, new DoubleAnimationChangeEventArgs()
217 {
218 AnimationType = AnimationType.Skew,
219 Value = value,
220 Propertypath = " ].(SkewTransform.AngleY) "
221 });
222 }
223 }
224
225 public event DoubleAnimationChangeEventHandler DoubleAnimationChange;
226 }
227
228 public class RotateDoubleAnimation
229 {
230 private DoubleAnimation _angle;
231
232 public DoubleAnimation Angle
233 {
234 get { return _angle; }
235 set
236 {
237 _angle = value;
238 if (DoubleAnimationChange != null )
239 DoubleAnimationChange( this .Angle, new DoubleAnimationChangeEventArgs()
240 {
241 AnimationType = AnimationType.Rotate,
242 Value = value,
243 Propertypath = " ].(RotateTransform.Angle) "
244 });
245 }
246 }
247
248 public event DoubleAnimationChangeEventHandler DoubleAnimationChange;
249 }
250
251 public class TranslateDoubleAnimation
252 {
253 private DoubleAnimation _x;
254
255 public DoubleAnimation X
256 {
257 get { return _x; }
258 set
259 {
260 _x = value;
261 if (DoubleAnimationChange != null )
262 DoubleAnimationChange( this .X, new DoubleAnimationChangeEventArgs()
263 {
264 AnimationType = AnimationType.Translate,
265 Value = value,
266 Propertypath = " ].(TranslateTransform.X) "
267 });
268 }
269 }
270
271 private DoubleAnimation _y;
272
273 public DoubleAnimation Y
274 {
275 get { return _y; }
276 set
277 {
278 _y = value;
279 if (DoubleAnimationChange != null )
280 DoubleAnimationChange( this .Y, new DoubleAnimationChangeEventArgs()
281 {
282 AnimationType = AnimationType.Translate,
283 Value = value,
284 Propertypath = " ].(TranslateTransform.Y) "
285 });
286 }
287 }
288
289 public event DoubleAnimationChangeEventHandler DoubleAnimationChange;
290 }
291
292 public enum AnimationType
293 {
294 Scale,
295 Skew,
296 Rotate,
297 Translate
298 }
299 }
使用方法
void MouseLeave( object sender, MouseEventArgs e)
{
StoryboardManager storyboardManager = new StoryboardManager(sender as FrameworkElement);
storyboardManager.Storyboard.Begin();
}
void MouseEnter( object sender, MouseEventArgs e)
{
StoryboardManager storyboardManager = new StoryboardManager(sender as FrameworkElement);
storyboardManager.ScaleDoubleAnimation.ScaleX = new DoubleAnimation() { To = 1.05 , Duration = TimeSpan.FromSeconds( 0.2 ) };
storyboardManager.ScaleDoubleAnimation.ScaleY = new DoubleAnimation() { To = 1.05 , Duration = TimeSpan.FromSeconds( 0.2 ) };
storyboardManager.SkewDoubleAnimation.AngleX = new DoubleAnimation() { To = 1.05 , Duration = TimeSpan.FromSeconds( 0.2 ) };
storyboardManager.SkewDoubleAnimation.AngleY = new DoubleAnimation() { To = 1.05 , Duration = TimeSpan.FromSeconds( 0.2 ) };
storyboardManager.RotateDoubleAnimation.Angle = new DoubleAnimation() { To = 360 , Duration = TimeSpan.FromSeconds( 0.2 ) };
storyboardManager.Storyboard.Begin();
}
源码地址:.zip
转载于:.html
更多推荐
Silverlight StoryboardManager 故事板管理类
发布评论