public void StartAnimation(IEnumerable <string> imageNames, TimeSpan interval) { Storyboard storyboard = new Storyboard(); ObjectAnimationUsingKeyFrames animation = new ObjectAnimationUsingKeyFrames(); Storyboard.SetTarget(animation, image); // Tie a specific animation timeline to an object, in this case, an image object Storyboard.SetTargetProperty(animation, new PropertyPath(Image.SourceProperty)); // Tie the animation to a property of the targeted object, //in this case, target the source of the image. TimeSpan currentInterval = TimeSpan.FromMilliseconds(0); foreach (string imageName in imageNames) { ObjectKeyFrame keyFrame = new DiscreteObjectKeyFrame(); keyFrame.Value = CreateImageFromAssets(imageName); keyFrame.KeyTime = currentInterval; animation.KeyFrames.Add(keyFrame); currentInterval = currentInterval.Add(interval); } storyboard.RepeatBehavior = RepeatBehavior.Forever; storyboard.AutoReverse = true; storyboard.Children.Add(animation); storyboard.Begin(); }
/// <summary> /// 使用透明度和显示状态的改变来进行动画隐藏该控件 /// </summary> /// <param name="element">要执行动画的控件</param> /// <param name="duration">持续时间</param> /// <param name="completed">执行完成后的行为</param> public static void BeginHideWithOpacityAndVisibility(this DependencyObject element, TimeSpan duration, Action completed) { Storyboard sb = new Storyboard(); DoubleAnimationUsingKeyFrames daukf = new DoubleAnimationUsingKeyFrames(); daukf.SetValue(Storyboard.TargetProperty, element); daukf.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("(FrameworkElement.Opacity)")); EasingDoubleKeyFrame f1 = new EasingDoubleKeyFrame(1, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0))); EasingDoubleKeyFrame f2 = new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(duration)); daukf.KeyFrames.Add(f1); daukf.KeyFrames.Add(f2); ObjectAnimationUsingKeyFrames oaukf = new ObjectAnimationUsingKeyFrames(); oaukf.SetValue(Storyboard.TargetProperty, element); oaukf.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath("(UIElement.Visibility)")); DiscreteObjectKeyFrame f3 = new DiscreteObjectKeyFrame(Visibility.Visible, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0))); DiscreteObjectKeyFrame f4 = new DiscreteObjectKeyFrame(Visibility.Collapsed, KeyTime.FromTimeSpan(duration)); oaukf.KeyFrames.Add(f3); oaukf.KeyFrames.Add(f4); sb.Children.Add(daukf); sb.Children.Add(oaukf); sb.Completed += (s, e) => { if (completed != null) { completed(); } }; sb.Begin(); }
public void CreateFlashStoryBoard(TimeSpan interval) { flashStoryboard = new Storyboard(); ObjectAnimationUsingKeyFrames animation = new ObjectAnimationUsingKeyFrames(); Storyboard.SetTarget(animation, image); Storyboard.SetTargetProperty(animation, new PropertyPath(Control.VisibilityProperty)); TimeSpan currentInterval = TimeSpan.FromMilliseconds(0); ObjectKeyFrame keyFrameVisible = new DiscreteObjectKeyFrame(); keyFrameVisible.Value = Visibility.Visible; keyFrameVisible.KeyTime = currentInterval; animation.KeyFrames.Add(keyFrameVisible); currentInterval = currentInterval.Add(interval); ObjectKeyFrame keyFrameHidden = new DiscreteObjectKeyFrame(); keyFrameHidden.Value = Visibility.Hidden; keyFrameHidden.KeyTime = currentInterval; animation.KeyFrames.Add(keyFrameHidden); currentInterval = currentInterval.Add(interval); flashStoryboard.RepeatBehavior = RepeatBehavior.Forever; flashStoryboard.AutoReverse = true; flashStoryboard.Children.Add(animation); }
private void OnKnowldgeClick(object sender, RoutedEventArgs e) { try { string name = (e.OriginalSource as Button).Tag as string; BitmapImage bitmap = new BitmapImage(new Uri("pack://application:,,,/Images/Reading/" + name)); popimage.Source = bitmap; Storyboard show = new Storyboard(); DoubleAnimation d1 = new DoubleAnimation(1, TimeSpan.FromMilliseconds(300)); show.Children.Add(d1); Storyboard.SetTarget(d1, pop); Storyboard.SetTargetProperty(d1, new PropertyPath("Opacity")); ObjectAnimationUsingKeyFrames d2 = new ObjectAnimationUsingKeyFrames(); ObjectKeyFrame k1 = new DiscreteObjectKeyFrame { KeyTime = TimeSpan.FromMilliseconds(0), Value = Visibility.Visible }; d2.KeyFrames.Add(k1); show.Children.Add(d2); Storyboard.SetTarget(d2, pop); Storyboard.SetTargetProperty(d2, new PropertyPath("Visibility")); show.Begin(); } catch (Exception) { } }
private static ObjectAnimationUsingKeyFrames GetAnimation(Image imageControl, BitmapSource source) { var animation = AnimationCache.GetAnimation(source, GetRepeatBehavior(imageControl)); if (animation != null) { return(animation); } if (GetDecoder(source, out GifFile gifMetadata) is GifBitmapDecoder decoder && decoder.Frames.Count > 1) { var fullSize = GetFullSize(decoder, gifMetadata); int index = 0; animation = new ObjectAnimationUsingKeyFrames(); var totalDuration = TimeSpan.Zero; BitmapSource baseFrame = null; foreach (var rawFrame in decoder.Frames) { var metadata = GetFrameMetadata(decoder, gifMetadata, index); var frame = MakeFrame(fullSize, rawFrame, metadata, baseFrame); var keyFrame = new DiscreteObjectKeyFrame(frame, totalDuration); animation.KeyFrames.Add(keyFrame); totalDuration += metadata.Delay; switch (metadata.DisposalMethod) { case FrameDisposalMethod.None: case FrameDisposalMethod.DoNotDispose: baseFrame = frame; break; case FrameDisposalMethod.RestoreBackground: if (IsFullFrame(metadata, fullSize)) { baseFrame = null; } else { baseFrame = ClearArea(frame, metadata); } break; case FrameDisposalMethod.RestorePrevious: // Reuse same base frame break; } index++; } animation.Duration = totalDuration; animation.RepeatBehavior = GetActualRepeatBehavior(imageControl, decoder, gifMetadata); AnimationCache.AddAnimation(source, GetRepeatBehavior(imageControl), animation); AnimationCache.IncrementReferenceCount(source, GetRepeatBehavior(imageControl)); return(animation); } return(null); }
public void StartFlapping(TimeSpan interval) { List <string> imageNames = new List <string>() { "Bee animation 1.png", "Bee animation 2.png", "Bee animation 3.png", "Bee animation 4.png" }; Storyboard storyboard = new Storyboard(); ObjectAnimationUsingKeyFrames animation = new ObjectAnimationUsingKeyFrames(); Storyboard.SetTarget(animation, image); Storyboard.SetTargetProperty(animation, new PropertyPath("Source")); TimeSpan currentInteval = TimeSpan.FromMilliseconds(0); foreach (string imageName in imageNames) { ObjectKeyFrame keyFrame = new DiscreteObjectKeyFrame(); keyFrame.Value = CreateImageFromAssets(imageName); keyFrame.KeyTime = currentInteval; animation.KeyFrames.Add(keyFrame); currentInteval = currentInteval.Add(interval); } storyboard.RepeatBehavior = RepeatBehavior.Forever; storyboard.AutoReverse = true; storyboard.Children.Add(animation); storyboard.Begin(); }
private void DisplayFadeMessage(String Message) { txtFadeMessage.Visibility = Visibility.Visible; txtFadeMessage.Text = Message; DoubleAnimationUsingKeyFrames _OpacityAnimation = new DoubleAnimationUsingKeyFrames(); DoubleKeyFrameCollection keyFrames = _OpacityAnimation.KeyFrames; LinearDoubleKeyFrame fr = new LinearDoubleKeyFrame(); fr.Value = 1; fr.KeyTime = TimeSpan.FromSeconds(0); keyFrames.Add(fr); LinearDoubleKeyFrame frEnd = new LinearDoubleKeyFrame(); frEnd.Value = 0.0; frEnd.KeyTime = TimeSpan.FromSeconds(3); keyFrames.Add(frEnd); ObjectAnimationUsingKeyFrames _VisibilityAnimation = new ObjectAnimationUsingKeyFrames(); DiscreteObjectKeyFrame frHide = new DiscreteObjectKeyFrame(); frHide.KeyTime = TimeSpan.FromSeconds(4); frHide.Value = Visibility.Collapsed; _VisibilityAnimation.KeyFrames.Add(frHide); Storyboard _Storyboard = new Storyboard(); _Storyboard.Children.Add(_OpacityAnimation); Storyboard.SetTargetProperty(_OpacityAnimation, "TextBlock.Opacity"); _Storyboard.Children.Add(_VisibilityAnimation); Storyboard.SetTargetProperty(_VisibilityAnimation, "TextBlock.Visibility"); Storyboard.SetTarget(_Storyboard, txtFadeMessage); _Storyboard.Begin(); }
private Storyboard CreateCloseStoryboard(UIElement target) { var objKeyframe = new DiscreteObjectKeyFrame() { KeyTime = TimeSpan.FromMilliseconds(201), Value = Visibility.Collapsed, }; var objAnim = new ObjectAnimationUsingKeyFrames(); objAnim.KeyFrames.Add(objKeyframe); Storyboard.SetTarget(objAnim, target); Storyboard.SetTargetProperty(objAnim, nameof(Visibility)); var opacityAnim = new DoubleAnimation() { Duration = TimeSpan.FromMilliseconds(200), To = 0, }; Storyboard.SetTarget(opacityAnim, target); Storyboard.SetTargetProperty(opacityAnim, nameof(Opacity)); var sb = new Storyboard(); sb.Children.Add(objAnim); sb.Children.Add(opacityAnim); return(sb); }
private Storyboard CreateOpenStoryboard(UIElement target) { var objKeyframem = new DiscreteObjectKeyFrame { KeyTime = TimeSpan.Zero, Value = Visibility.Visible }; var objAnim = new ObjectAnimationUsingKeyFrames(); objAnim.KeyFrames.Add(objKeyframem); Storyboard.SetTarget(objAnim, target); Storyboard.SetTargetProperty(objAnim, nameof(Visibility)); var opactiyAnim = new DoubleAnimation { Duration = TimeSpan.FromMilliseconds(200), To = 1, From = 0, }; Storyboard.SetTarget(opactiyAnim, target); Storyboard.SetTargetProperty(opactiyAnim, nameof(Opacity)); var sb = new Storyboard(); sb.Children.Add(objAnim); sb.Children.Add(opactiyAnim); return(sb); }
/// <summary> /// Starts an animation to a particular value on the specified dependency property. /// You can pass in an event handler to call when the animation has completed. /// </summary> public static void StartAnimation(UIElement animatableElement, DependencyProperty dependencyProperty, Visibility toValue, double animationDurationSeconds, EventHandler completedEvent) { ObjectAnimationUsingKeyFrames objanimation = new ObjectAnimationUsingKeyFrames(); DiscreteObjectKeyFrame dobjf = new DiscreteObjectKeyFrame(toValue, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0))); objanimation.BeginTime = TimeSpan.FromMilliseconds(0); objanimation.Duration = TimeSpan.FromMilliseconds(animationDurationSeconds); objanimation.KeyFrames = new ObjectKeyFrameCollection() { dobjf }; objanimation.Completed += delegate(object sender, EventArgs e) { // // When the animation has completed bake final value of the animation // into the property. // animatableElement.SetValue(dependencyProperty, animatableElement.GetValue(dependencyProperty)); CancelAnimation(animatableElement, dependencyProperty); if (completedEvent != null) { completedEvent(sender, e); } }; objanimation.Freeze(); objanimation.BeginAnimation(dependencyProperty, objanimation); }
private static void InitAnimationOrImage(Image imageControl) { BitmapSource source = GetAnimatedSource(imageControl) as BitmapSource; if (source != null) { var decoder = GetDecoder(source) as GifBitmapDecoder; if (decoder != null && decoder.Frames.Count > 1) { var animation = new ObjectAnimationUsingKeyFrames(); var keyTime = TimeSpan.Zero; var totalDuration = TimeSpan.Zero; foreach (var frame in decoder.Frames) { var keyFrame = new DiscreteObjectKeyFrame(frame, keyTime); animation.KeyFrames.Add(keyFrame); var duration = GetFrameDelay(frame); totalDuration += duration; keyTime = keyTime + duration; } animation.Duration = totalDuration; animation.RepeatBehavior = RepeatBehavior.Forever; imageControl.Source = decoder.Frames[0]; imageControl.BeginAnimation(Image.SourceProperty, animation); return; } } imageControl.Source = source; return; }
public static void AnimateOpacity(DependencyObject target, int from, int to, double duration) { DoubleAnimation fade = new DoubleAnimation() { From = from, To = to, Duration = TimeSpan.FromMilliseconds(duration), EnableDependentAnimation = true, }; Storyboard.SetTarget(fade, target); Storyboard.SetTargetProperty(fade, "Opacity"); DiscreteObjectKeyFrame dokf = new DiscreteObjectKeyFrame() { KeyTime = (from > to) ? TimeSpan.FromMilliseconds(duration) : TimeSpan.FromMilliseconds(0), Value = (from > to) ? Visibility.Collapsed : Visibility.Visible }; ObjectAnimationUsingKeyFrames oaukf = new ObjectAnimationUsingKeyFrames() { EnableDependentAnimation = true }; oaukf.KeyFrames.Add(dokf); Storyboard.SetTarget(oaukf, target); Storyboard.SetTargetProperty(oaukf, "Visibility"); Storyboard openpane = new Storyboard(); openpane.Children.Add(fade); openpane.Children.Add(oaukf); openpane.Begin(); }
public static void ObjectAnimation(Storyboard Board, DependencyObject Element, string Property, object From, object To, double Duration = 350, double Delay = 0) { ObjectAnimationUsingKeyFrames d = new ObjectAnimationUsingKeyFrames(); DiscreteObjectKeyFrame still = new DiscreteObjectKeyFrame(); still.Value = From; still.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0)); if (0 < Delay) { DiscreteObjectKeyFrame still_still = new DiscreteObjectKeyFrame(); still_still.Value = To; still_still.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(Delay)); d.KeyFrames.Add(still_still); } DiscreteObjectKeyFrame move = new DiscreteObjectKeyFrame(); move.Value = To; move.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(Delay + Duration)); d.Duration = new Duration(TimeSpan.FromMilliseconds(Delay + Duration)); d.KeyFrames.Add(still); d.KeyFrames.Add(move); Storyboard.SetTarget(d, Element); Storyboard.SetTargetProperty(d, new PropertyPath(Property)); Board.Children.Add(d); }
public virtual void BeginCurrent(UIElement element, Action complate = null) { element.RenderTransformOrigin = RenderTransformOrigin; Panel.SetZIndex(element, Reverse ? 1 : 0); if (this.BeginTime == 0.0) { element.Visibility = Visibility.Visible; } else { Storyboard storyboard = new Storyboard(); ObjectAnimationUsingKeyFrames frames = new ObjectAnimationUsingKeyFrames(); DiscreteObjectKeyFrame keyFrame1 = new DiscreteObjectKeyFrame(Visibility.Hidden, KeyTime.FromTimeSpan(TimeSpan.Zero)); DiscreteObjectKeyFrame keyFrame2 = new DiscreteObjectKeyFrame(Visibility.Visible, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(this.BeginTime))); frames.KeyFrames.Add(keyFrame1); frames.KeyFrames.Add(keyFrame2); Storyboard.SetTarget(frames, element); Storyboard.SetTargetProperty(frames, new PropertyPath(FrameworkElement.VisibilityProperty)); storyboard.Children.Add(frames); storyboard.Begin(); } }
private void flipCard(Uri uri) { cardFlipStory = new Storyboard(); DoubleAnimation flipAnimation = new DoubleAnimation(); flipAnimation.Duration = TimeSpan.FromMilliseconds(500); flipAnimation.To = -1; flipAnimation.FillBehavior = FillBehavior.Stop; cardFlipStory.Children.Add(flipAnimation); Storyboard.SetTargetProperty(flipAnimation, new PropertyPath("RenderTransform.ScaleX")); Storyboard.SetTarget(flipAnimation, Card); DoubleAnimation opacityAnimation = new DoubleAnimation(); opacityAnimation.Duration = TimeSpan.FromMilliseconds(500); opacityAnimation.From = 1; opacityAnimation.To = 0; opacityAnimation.AutoReverse = true; Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity")); Storyboard.SetTarget(opacityAnimation, Card); cardFlipStory.Children.Add(opacityAnimation); ObjectAnimationUsingKeyFrames keyframe = new ObjectAnimationUsingKeyFrames(); Storyboard.SetTargetProperty(keyframe, new PropertyPath(Image.SourceProperty)); Storyboard.SetTarget(keyframe, Card); DiscreteObjectKeyFrame d1 = new DiscreteObjectKeyFrame(); d1.KeyTime = TimeSpan.FromMilliseconds(500); d1.Value = new BitmapImage(uri); keyframe.KeyFrames.Add(d1); cardFlipStory.Children.Add(keyframe); cardFlipStory.Begin(); }
public static void ShowHidePrincipal(Window window, string target, EventHandler OnComplete, Visibility visibility) { int seconds = 1; Storyboard storyboard = new Storyboard(); TimeSpan time = new TimeSpan(0, 0, seconds); DoubleAnimation animationFade = new DoubleAnimation( visibility == Visibility.Hidden ? 0 : 1, new Duration(time)); Storyboard.SetTargetName(animationFade, target); Storyboard.SetTargetProperty(animationFade, new PropertyPath(Frame.OpacityProperty)); ObjectAnimationUsingKeyFrames animationHide = new ObjectAnimationUsingKeyFrames(); ObjectKeyFrame hideFrame = new DiscreteObjectKeyFrame(visibility, KeyTime.FromTimeSpan(time)); animationHide.KeyFrames.Add(hideFrame); Storyboard.SetTargetName(animationHide, target); Storyboard.SetTargetProperty(animationHide, new PropertyPath(Frame.VisibilityProperty)); storyboard.Children.Add(animationFade); storyboard.Children.Add(animationHide); storyboard.Completed += OnComplete; storyboard.Begin(window); }
public void StartAnimation(IEnumerable <string> imageNames, TimeSpan interval) { var storyboard = new Storyboard(); var animation = new ObjectAnimationUsingKeyFrames(); Storyboard.SetTarget(animation, image); Storyboard.SetTargetProperty(animation, new PropertyPath(Image.SourceProperty)); var currentInterval = TimeSpan.FromMilliseconds(0); foreach (var imageName in imageNames) { var keyFrame = new DiscreteObjectKeyFrame { Value = CreateImageFromAssets(imageName), KeyTime = currentInterval }; animation.KeyFrames.Add(keyFrame); currentInterval = currentInterval.Add(interval); } storyboard.RepeatBehavior = RepeatBehavior.Forever; storyboard.AutoReverse = true; storyboard.Children.Add(animation); storyboard.Begin(); }
/// <summary> /// Creates storyboard which player ship flash animation. /// </summary> private void CreateFlashingAnimation() { _flashStoryboard = new Storyboard(); ObjectAnimationUsingKeyFrames animation = new ObjectAnimationUsingKeyFrames(); Storyboard.SetTarget(animation, image); Storyboard.SetTargetProperty(animation, nameof(image.Visibility)); ObjectKeyFrame visibleFrame = new DiscreteObjectKeyFrame() { Value = Windows.UI.Xaml.Visibility.Visible, KeyTime = TimeSpan.FromMilliseconds(0), }; ObjectKeyFrame invisibleFrame = new DiscreteObjectKeyFrame() { Value = Windows.UI.Xaml.Visibility.Collapsed, KeyTime = TimeSpan.FromMilliseconds(500), }; animation.KeyFrames.Add(visibleFrame); animation.KeyFrames.Add(invisibleFrame); _flashStoryboard.Children.Add(animation); _flashStoryboard.AutoReverse = true; _flashStoryboard.RepeatBehavior = RepeatBehavior.Forever; }
private static ObjectAnimationUsingKeyFrames CreateAnimation(IList <ImageSource> imageFrames) { // Initialize the animation for this object. var result = new ObjectAnimationUsingKeyFrames { Duration = s_animationDuration, }; // Creates the frames for the animation. var frameDuration = FullDuration / s_lightFrames.Value.Count; int framePoint = 0; var keyFrames = new ObjectKeyFrameCollection(); foreach (var frame in imageFrames) { var keyFrame = new DiscreteObjectKeyFrame { KeyTime = new TimeSpan(0, 0, 0, 0, framePoint), Value = frame, }; keyFrames.Add(keyFrame); framePoint += frameDuration; } result.KeyFrames = keyFrames; return(result); }
private void ShowNextImage(Image img) { const double transition_time = 0.9; Storyboard sb = new Storyboard(); // *************************** // Animate Opacity 1.0 --> 0.0 // *************************** DoubleAnimation fade_out = new DoubleAnimation(1.0, 0.0, TimeSpan.FromSeconds(transition_time)); fade_out.BeginTime = TimeSpan.FromSeconds(0); // Use the Storyboard to set the target property. Storyboard.SetTarget(fade_out, img); Storyboard.SetTargetProperty(fade_out, new PropertyPath(Image.OpacityProperty)); // Add the animation to the StoryBoard. sb.Children.Add(fade_out); // ********************************* // Animate displaying the new image. // ********************************* ObjectAnimationUsingKeyFrames new_image_animation = new ObjectAnimationUsingKeyFrames(); // Start after the first animation has finisheed. new_image_animation.BeginTime = TimeSpan.FromSeconds(transition_time); // Add a key frame to the animation. // It should be at time 0 after the animation begins. DiscreteObjectKeyFrame new_image_frame = new DiscreteObjectKeyFrame(Images[ImageNumber], TimeSpan.Zero); new_image_animation.KeyFrames.Add(new_image_frame); // Use the Storyboard to set the target property. Storyboard.SetTarget(new_image_animation, img); Storyboard.SetTargetProperty(new_image_animation, new PropertyPath(Image.SourceProperty)); // Add the animation to the StoryBoard. sb.Children.Add(new_image_animation); // *************************** // Animate Opacity 0.0 --> 1.0 // *************************** // Start when the first animation ends. DoubleAnimation fade_in = new DoubleAnimation(0.0, 1.0, TimeSpan.FromSeconds(transition_time)); fade_in.BeginTime = TimeSpan.FromSeconds(transition_time); // Use the Storyboard to set the target property. Storyboard.SetTarget(fade_in, img); Storyboard.SetTargetProperty(fade_in, new PropertyPath(Image.OpacityProperty)); // Add the animation to the StoryBoard. sb.Children.Add(fade_in); // Start the storyboard on the img control. sb.Begin(img); }
/// <summary> /// Starts new animation from given images. /// </summary> /// <param name="imageNames">Names of images in assets folder</param> /// <param name="interval">Interval between animation frames</param> public void StartAnimation(IEnumerable <string> imageNames, TimeSpan interval) { Storyboard storyboard = new Storyboard(); ObjectAnimationUsingKeyFrames animation = new ObjectAnimationUsingKeyFrames(); // We're going to animate image inside our control. Storyboard.SetTarget(animation, image); // Animation relies on changing value of property source Storyboard.SetTargetProperty(animation, nameof(image.Source)); TimeSpan currentInterval = TimeSpan.FromMilliseconds(0); foreach (string imageName in imageNames) { // We're creating individual frames from given images ObjectKeyFrame keyFrame = new DiscreteObjectKeyFrame(); keyFrame.Value = CreateImageFromAssets(imageName); keyFrame.KeyTime = currentInterval; animation.KeyFrames.Add(keyFrame); currentInterval = currentInterval.Add(interval); } // We're configuring our storyboard which will play animations storyboard.RepeatBehavior = RepeatBehavior.Forever; storyboard.AutoReverse = true; storyboard.Children.Add(animation); storyboard.Begin(); }
public void StartAnimation(IEnumerable <string> imageNames, TimeSpan interval) { try { Storyboard storyboard = new Storyboard(); ObjectAnimationUsingKeyFrames animation = new ObjectAnimationUsingKeyFrames(); Storyboard.SetTarget(animation, image); Storyboard.SetTargetProperty(animation, new PropertyPath(Image.SourceProperty)); TimeSpan currentInterval = TimeSpan.FromMilliseconds(0); foreach (string imageName in imageNames) { ObjectKeyFrame keyFrame = new DiscreteObjectKeyFrame(); keyFrame.Value = CreateImageFromAssets(imageName); keyFrame.KeyTime = currentInterval; animation.KeyFrames.Add(keyFrame); currentInterval = currentInterval.Add(interval); } storyboard.RepeatBehavior = RepeatBehavior.Forever; storyboard.AutoReverse = true; storyboard.Children.Add(animation); storyboard.Begin(); } catch (System.IO.IOException) { } }
public override Storyboard GetAnimationStoryboard(DependencyObject animationTarget) { var storyboard = new Storyboard { FillBehavior = FillBehavior.HoldEnd }; var opacityKeyFrames = new DoubleAnimationUsingKeyFrames(); var visibilityKeyFrames = new ObjectAnimationUsingKeyFrames(); var visibilityFrame = new DiscreteObjectKeyFrame() { KeyTime = KeyTime.FromTimeSpan(TimeSpan.Zero), Value = Visibility.Visible }; var opacityStartFrame = new EasingDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.Zero), Value = 0, }; var opacityEndFrame = new EasingDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(Duration), Value = 1, }; visibilityKeyFrames.KeyFrames.Add(visibilityFrame); opacityKeyFrames.KeyFrames.Add(opacityStartFrame); opacityKeyFrames.KeyFrames.Add(opacityEndFrame); SetStoryboardBindings(animationTarget, visibilityKeyFrames, VISIBILITY_PROPERTY_PATH, storyboard); SetStoryboardBindings(animationTarget, opacityKeyFrames, OPACITY_PROPERTY_PATH, storyboard); return(storyboard); }
/// <summary> /// 路径走向 /// </summary> /// <param name="orientation">0正向 1反向</param> /// <param name="data">路径数据</param> private void MatrixStory(int orientation, string data, int durationTime) { Border border = new Border(); border.Width = 10; border.Height = 10; border.Visibility = Visibility.Collapsed; if (orientation == 0) { border.Background = new SolidColorBrush(Colors.Blue); } else { border.Background = new SolidColorBrush(Colors.Green); data = ConvertReverseData(data); } this.MyCanvas.Children.Add(border); Canvas.SetLeft(border, -border.Width / 2); Canvas.SetTop(border, -border.Height / 2); border.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5); MatrixTransform matrix = new MatrixTransform(); TransformGroup groups = new TransformGroup(); groups.Children.Add(matrix); border.RenderTransform = groups; //NameScope.SetNameScope(this, new NameScope()); string registname = "matrix" + Guid.NewGuid().ToString().Replace("-", ""); this.RegisterName(registname, matrix); MatrixAnimationUsingPath matrixAnimation = new MatrixAnimationUsingPath(); matrixAnimation.PathGeometry = PathGeometry.CreateFromGeometry(Geometry.Parse(data)); matrixAnimation.Duration = new Duration(TimeSpan.FromSeconds(durationTime)); matrixAnimation.DoesRotateWithTangent = true;//旋转 //matrixAnimation.FillBehavior = FillBehavior.Stop; Storyboard story = new Storyboard(); story.Children.Add(matrixAnimation); Storyboard.SetTargetName(matrixAnimation, registname); Storyboard.SetTargetProperty(matrixAnimation, new PropertyPath(MatrixTransform.MatrixProperty)); #region 控制显示与隐藏 ObjectAnimationUsingKeyFrames ObjectAnimation = new ObjectAnimationUsingKeyFrames(); ObjectAnimation.Duration = matrixAnimation.Duration; DiscreteObjectKeyFrame kf1 = new DiscreteObjectKeyFrame(Visibility.Visible, TimeSpan.FromMilliseconds(1)); ObjectAnimation.KeyFrames.Add(kf1); story.Children.Add(ObjectAnimation); //Storyboard.SetTargetName(border, border.Name); Storyboard.SetTargetProperty(ObjectAnimation, new PropertyPath(UIElement.VisibilityProperty)); #endregion story.FillBehavior = FillBehavior.Stop; story.Begin(border, true); }
protected ObjectAnimationUsingKeyFrames CreateObjectKeyFrameAnimation(TimeSpan timeSpan, object value, DependencyObject target, string path) { ObjectAnimationUsingKeyFrames animation = new ObjectAnimationUsingKeyFrames(); DiscreteObjectKeyFrame frame = new DiscreteObjectKeyFrame(); frame.KeyTime = timeSpan; frame.Value = value; animation.KeyFrames.Add(frame); Storyboard.SetTarget(animation, target); Storyboard.SetTargetProperty(animation, path); return(animation); }
public void CreateReversal(double startTime, Button btn, BitmapImage background) { var chgBg = new ObjectAnimationUsingKeyFrames(); DiscreteObjectKeyFrame dk = new DiscreteObjectKeyFrame(background); dk.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(startTime + 0.5)); dk.Value = background; chgBg.KeyFrames.Add(dk); Storyboard.SetTargetName(chgBg, btn.Name); DependencyProperty[] propertyChain2 = new DependencyProperty[] { Button.BackgroundProperty, ImageBrush.ImageSourceProperty }; Storyboard.SetTargetProperty(chgBg, new PropertyPath("(0).(1)", propertyChain2)); var transformOrigin = new PointAnimationUsingKeyFrames(); EasingPointKeyFrame kf1 = new EasingPointKeyFrame(new System.Windows.Point(0.5, 0), KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0))); EasingPointKeyFrame kf2 = new EasingPointKeyFrame(new System.Windows.Point(0.5, 0), KeyTime.FromTimeSpan(TimeSpan.FromSeconds(startTime))); EasingPointKeyFrame kf3 = new EasingPointKeyFrame(new System.Windows.Point(0.5, 0.5), KeyTime.FromTimeSpan(TimeSpan.FromSeconds(startTime + 1))); transformOrigin.KeyFrames.Add(kf1); transformOrigin.KeyFrames.Add(kf2); transformOrigin.KeyFrames.Add(kf3); Storyboard.SetTargetName(transformOrigin, btn.Name); DependencyProperty[] propertyChain1 = new DependencyProperty[] { Button.RenderTransformOriginProperty }; Storyboard.SetTargetProperty(transformOrigin, new PropertyPath(Control.RenderTransformOriginProperty)); var d_transform = new DoubleAnimationUsingKeyFrames(); EasingDoubleKeyFrame kf4 = new EasingDoubleKeyFrame(-1, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0))); EasingDoubleKeyFrame kf5 = new EasingDoubleKeyFrame(-1, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(startTime))); EasingDoubleKeyFrame kf6 = new EasingDoubleKeyFrame(1, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(startTime + 1))); d_transform.KeyFrames.Add(kf4); d_transform.KeyFrames.Add(kf5); d_transform.KeyFrames.Add(kf6); Storyboard.SetTargetName(d_transform, btn.Name); DependencyProperty[] propertyChain3 = new DependencyProperty[] { Control.RenderTransformProperty, TransformGroup.ChildrenProperty, ScaleTransform.ScaleXProperty }; Storyboard.SetTargetProperty(d_transform, new PropertyPath("(0).(1)[0].(2)", propertyChain3)); reversalSB.Children.Add(transformOrigin); reversalSB.Children.Add(d_transform); reversalSB.Children.Add(chgBg); }
/// <summary> /// Initiates a spritesheet animation /// </summary> /// <param name="shape">Shape to animate on (will create an ImageBrush)</param> /// <param name="spriteSheetColumns">Spritesheet columns</param> /// <param name="spriteSheetRows">Spritesheet rows</param> /// <param name="image">The spritesheet image</param> /// <param name="width">Width of the sprite on the spritesheet</param> /// <param name="height">Height of the sprite on the spritesheet</param> /// <param name="keyframeTime">Time that each keyframe should have</param> /// <returns>Storyboard created</returns> public static Storyboard BeginSpriteSheetStoryboard(Shape shape, int spriteSheetColumns, int spriteSheetRows, BitmapImage image, double width, double height, int keyframeTime) { ImageBrush ib = new ImageBrush() { Stretch = Stretch.None, AlignmentX = AlignmentX.Left, AlignmentY = AlignmentY.Top }; ib.Transform = new CompositeTransform(); ib.ImageSource = image; shape.Fill = ib; Storyboard sb = new Storyboard(); sb.RepeatBehavior = RepeatBehavior.Forever; ObjectAnimationUsingKeyFrames frm = new ObjectAnimationUsingKeyFrames(); ObjectAnimationUsingKeyFrames frm2 = new ObjectAnimationUsingKeyFrames(); frm.BeginTime = new TimeSpan(0, 0, 0); frm2.BeginTime = new TimeSpan(0, 0, 0); int time = 0; for (int j = 0; j < spriteSheetRows; j++) { for (int i = 0; i < spriteSheetColumns; i++) { DiscreteObjectKeyFrame dokf = new DiscreteObjectKeyFrame(); dokf.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(time)); dokf.Value = -(i * width); frm.KeyFrames.Add(dokf); DiscreteObjectKeyFrame dokf2 = new DiscreteObjectKeyFrame(); dokf2.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(time)); dokf2.Value = -(j * height); frm2.KeyFrames.Add(dokf2); time += keyframeTime; } } Storyboard.SetTarget(frm, shape.Fill); Storyboard.SetTarget(frm2, shape.Fill); Storyboard.SetTargetProperty(frm, "(ImageBrush.Transform).(CompositeTransform.TranslateX)"); Storyboard.SetTargetProperty(frm2, "(ImageBrush.Transform).(CompositeTransform.TranslateY)"); sb.Children.Add(frm); sb.Children.Add(frm2); sb.Begin(); return(sb); }
public static ObjectAnimationUsingKeyFrames CreateGifAnimation(GifBitmapDecoder decoder, GifFile gifMetadata) { ObjectAnimationUsingKeyFrames animation; var fullSize = GetFullSize(decoder, gifMetadata); int index = 0; animation = new ObjectAnimationUsingKeyFrames(); var totalDuration = TimeSpan.Zero; BitmapSource baseFrame = null; foreach (var rawFrame in decoder.Frames) { var metadata = GetFrameMetadata(decoder, gifMetadata, index); var frame = MakeFrame(fullSize, rawFrame, metadata, baseFrame); var keyFrame = new DiscreteObjectKeyFrame(frame, totalDuration); animation.KeyFrames.Add(keyFrame); totalDuration += metadata.Delay; switch (metadata.DisposalMethod) { case FrameDisposalMethod.None: case FrameDisposalMethod.DoNotDispose: baseFrame = frame; break; case FrameDisposalMethod.RestoreBackground: if (IsFullFrame(metadata, fullSize)) { baseFrame = null; } else { baseFrame = ClearArea(frame, metadata); } break; case FrameDisposalMethod.RestorePrevious: // Reuse same base frame break; } index++; } animation.Duration = totalDuration; return(animation); }
public void AddSetValue(DependencyObject target, string property, object value) { var anim = new ObjectAnimationUsingKeyFrames(); var kf = new DiscreteObjectKeyFrame() { Value = value }; anim.KeyFrames.Add(kf); SetTarget(target, property, anim); sb.Children.Add(anim); }
/// <summary> /// Reveals data points using a storyboard. /// </summary> /// <param name="dataPoints">The data points to change the state of. /// </param> /// <param name="dataPointCount">The number of data points in the sequence.</param> /// <param name="newState">The state to change to.</param> private void StaggeredStateChange(IEnumerable <DataPoint> dataPoints, int dataPointCount, DataPointState newState) { if (PlotArea == null || dataPointCount == 0) { return; } Storyboard stateChangeStoryBoard = new Storyboard(); dataPoints.ForEachWithIndex((dataPoint, count) => { // Create an Animation ObjectAnimationUsingKeyFrames objectAnimationUsingKeyFrames = new ObjectAnimationUsingKeyFrames(); Storyboard.SetTarget(objectAnimationUsingKeyFrames, dataPoint); Storyboard.SetTargetProperty(objectAnimationUsingKeyFrames, new PropertyPath("State")); // Create a key frame DiscreteObjectKeyFrame discreteObjectKeyFrame = new DiscreteObjectKeyFrame(); discreteObjectKeyFrame.Value = newState; // Create the specified animation type switch (AnimationSequence) { case AnimationSequence.Simultaneous: discreteObjectKeyFrame.KeyTime = TimeSpan.Zero; break; case AnimationSequence.FirstToLast: discreteObjectKeyFrame.KeyTime = TimeSpan.FromMilliseconds(1000 * ((double)count / dataPointCount)); break; case AnimationSequence.LastToFirst: discreteObjectKeyFrame.KeyTime = TimeSpan.FromMilliseconds(1000 * ((double)(dataPointCount - count - 1) / dataPointCount)); break; } // Add the Animation to the Storyboard objectAnimationUsingKeyFrames.KeyFrames.Add(discreteObjectKeyFrame); stateChangeStoryBoard.Children.Add(objectAnimationUsingKeyFrames); }); stateChangeStoryBoard.Duration = new Duration(AnimationSequence.Simultaneous == AnimationSequence ? TimeSpan.FromTicks(1) : TimeSpan.FromMilliseconds(1001)); _storyBoardQueue.Enqueue( stateChangeStoryBoard, (sender, args) => { stateChangeStoryBoard.Stop(); }); }