Guest User

Untitled

a guest
Aug 17th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. Optimizing Fading Box Animations
  2. <Storyboard x:Key="uiStoryboardTile" AutoReverse="True" RepeatBehavior="Forever">
  3. <ColorAnimationUsingKeyFrames
  4. Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
  5. <EasingColorKeyFrame KeyTime="0:0:2" Value="Transparent"/>
  6. </ColorAnimationUsingKeyFrames>
  7. </Storyboard>
  8.  
  9. <Window.Resources>
  10. <Rectangle x:Key="Color0" Fill="#FFFFCFFF" />
  11. <Rectangle x:Key="Color1" Fill="#FFFFC2C2" />
  12. <Rectangle x:Key="Color2" Fill="#FFFFEFD2" />
  13. ...
  14. </Window.Resources>
  15.  
  16. <Storyboard x:Key="BackgroundAnimation" AutoReverse="True" RepeatBehavior="Forever">
  17. <ColorAnimationUsingKeyFrames Storyboard.Target="{StaticResource Color0}"
  18. Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
  19. BeginTime="0:0:0:0"
  20. AutoReverse="True"
  21. Duration="00:00:03.00">
  22. <ColorKeyFrameCollection>
  23. <EasingColorKeyFrame Value="Transparent" />
  24. </ColorKeyFrameCollection>
  25. </ColorAnimationUsingKeyFrames>
  26.  
  27. <!-- Add keyframes for each color, varying start and duration -->
  28. ...
  29. </Storyboard>
  30.  
  31. // I'll leave the implementation of GetRandomColorId to you
  32. resourceId = GetRandomColorId(MAX_COLORS);
  33.  
  34. Shape source = (Shape)this.FindResource("Color" + resourceId);
  35. Binding binding = new Binding
  36. {
  37. Path = new PropertyPath("Fill"),
  38. Source = source
  39. };
  40.  
  41. rect.SetBinding(Shape.FillProperty, binding);
  42.  
  43. // Disclaimer: This code was written in the SO text editor. Might not be correct.
  44. foreach (MyRect rect in MyRectangles)
  45. {
  46. if (rect.FadingIn)
  47. {
  48. rect.Opacity += 0.1;
  49. if (rect.Opacity >= 1) { rect.FadingIn = false; }
  50. }
  51. else
  52. {
  53. rect.Opacity -= 0.1;
  54. if (rect.Opacity <= 0 ) { rect.FadingIn = true; }
  55. }
  56. }
  57.  
  58. class MyRect
  59. {
  60. public Shape Rectangle;
  61. public bool FadingIn;
  62.  
  63. public double Opacity
  64. {
  65. get { return Rectangle.Opacity; }
  66. set { Rectangle.Opacity = value }
  67. }
  68.  
  69. //... etc.
  70. }
Add Comment
Please, Sign In to add comment