Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Optimizing Fading Box Animations
- <Storyboard x:Key="uiStoryboardTile" AutoReverse="True" RepeatBehavior="Forever">
- <ColorAnimationUsingKeyFrames
- Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
- <EasingColorKeyFrame KeyTime="0:0:2" Value="Transparent"/>
- </ColorAnimationUsingKeyFrames>
- </Storyboard>
- <Window.Resources>
- <Rectangle x:Key="Color0" Fill="#FFFFCFFF" />
- <Rectangle x:Key="Color1" Fill="#FFFFC2C2" />
- <Rectangle x:Key="Color2" Fill="#FFFFEFD2" />
- ...
- </Window.Resources>
- <Storyboard x:Key="BackgroundAnimation" AutoReverse="True" RepeatBehavior="Forever">
- <ColorAnimationUsingKeyFrames Storyboard.Target="{StaticResource Color0}"
- Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
- BeginTime="0:0:0:0"
- AutoReverse="True"
- Duration="00:00:03.00">
- <ColorKeyFrameCollection>
- <EasingColorKeyFrame Value="Transparent" />
- </ColorKeyFrameCollection>
- </ColorAnimationUsingKeyFrames>
- <!-- Add keyframes for each color, varying start and duration -->
- ...
- </Storyboard>
- // I'll leave the implementation of GetRandomColorId to you
- resourceId = GetRandomColorId(MAX_COLORS);
- Shape source = (Shape)this.FindResource("Color" + resourceId);
- Binding binding = new Binding
- {
- Path = new PropertyPath("Fill"),
- Source = source
- };
- rect.SetBinding(Shape.FillProperty, binding);
- // Disclaimer: This code was written in the SO text editor. Might not be correct.
- foreach (MyRect rect in MyRectangles)
- {
- if (rect.FadingIn)
- {
- rect.Opacity += 0.1;
- if (rect.Opacity >= 1) { rect.FadingIn = false; }
- }
- else
- {
- rect.Opacity -= 0.1;
- if (rect.Opacity <= 0 ) { rect.FadingIn = true; }
- }
- }
- class MyRect
- {
- public Shape Rectangle;
- public bool FadingIn;
- public double Opacity
- {
- get { return Rectangle.Opacity; }
- set { Rectangle.Opacity = value }
- }
- //... etc.
- }
Add Comment
Please, Sign In to add comment