Example #1
0
        /// <summary>Add animation to visual element.</summary>
        /// <param name="element">The visual element being animated.</param>
        /// <param name="name">Name of animation.</param>
        /// <param name="transform">Transformation for animation.</param>
        /// <param name="callback">Action to call back.</param>
        /// <param name="length">Duration of animation.</param>
        /// <param name="easing">Non-linear easing of animation.</param>
        /// <returns>A <see cref="Task{Boolean}"/> representing the asynchronous operation.</returns>
        private static Task <bool> ColorAnimation(VisualElement element, string name, Func <double, Color> transform, Action <Color> callback, uint length, Easing easing)
        {
            if (element is null)
            {
                throw new ArgumentNullException(nameof(element));
            }

            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("message", nameof(name));
            }

            if (transform is null)
            {
                throw new ArgumentNullException(nameof(transform));
            }

            if (callback is null)
            {
                throw new ArgumentNullException(nameof(callback));
            }

            easing ??= Easing.Linear;
            var taskCompletionSource = new TaskCompletionSource <bool>();

            element.Animate(name, transform, callback, 16, length, easing, (v, c) => taskCompletionSource.SetResult(c));
            return(taskCompletionSource.Task);
        }
Example #2
0
        static Task <bool> ColorAnimation(VisualElement element, string name, Func <double, Color> transform, Action <Color> callback, uint length, Easing easing)
        {
            easing = easing ?? Easing.Linear;
            var taskCompletionSource = new TaskCompletionSource <bool>();

            element.Animate <Color>(name, transform, callback, 16, length, easing, (v, c) => taskCompletionSource.SetResult(c));
            return(taskCompletionSource.Task);
        }
Example #3
0
        public static Task <bool> SizeTo(this VisualElement self, double startSize, double endSize, Action <double> callback, uint length, Easing easing = null)
        {
            easing = easing ?? Easing.Linear;
            var taskCompletionSource = new TaskCompletionSource <bool>();

            self.Animate("SizeTo", callback, startSize, endSize, 16, length, easing, (v, c) => taskCompletionSource.SetResult(c));

            return(taskCompletionSource.Task);
        }
Example #4
0
        public static void ColorTo(VisualElement self, Color fromColor, Color toColor, uint length = 250, Easing easing = null)
        {
            Func <double, Color> transform = (t) =>
                                             Color.FromRgba(fromColor.R + t * (toColor.R - fromColor.R),
                                                            fromColor.G + t * (toColor.G - fromColor.G),
                                                            fromColor.B + t * (toColor.B - fromColor.B),
                                                            1);

            Task.Factory.StartNew(() => self.Animate("CBGC", new Animation(t => self.BackgroundColor = transform(t), 0, 1), 16, length, easing));
        }
Example #5
0
        /// <summary>
        /// Invoke VisualElement HeightRequest animation.
        /// </summary>
        /// <param name="sender">Sender.</param>
        protected override void Invoke(VisualElement sender)
        {
            SetDefaultValueIfNeeded(sender.Height);

            sender.Animate(nameof(HeightRequestDoubleAnimation), new Animation((d) =>
            {
                sender.HeightRequest = AnimationUtil.CalcCurrentValue(From, To, d);
            }),
                           length: Length,
                           easing: EasingValueConverter.Convert(Easing));
        }
Example #6
0
        /// <summary>
        /// Invoke Label LineHeight animation.
        /// </summary>
        /// <param name="sender">Sender.</param>
        protected override void Invoke(VisualElement sender)
        {
            SetDefaultValueIfNeeded((sender as Label).LineHeight);

            var animation = new Animation((d) =>
            {
                (sender as Label).LineHeight = AnimationUtil.CalcCurrentValue(From, To, d);
            });

            sender.Animate(nameof(LabelLineHeightDoubleAnimation), animation, length: Length, easing: EasingValueConverter.Convert(Easing));
        }
Example #7
0
        /// <summary>
        /// Invoke Button CornerRadius animation.
        /// </summary>
        /// <param name="sender">Sender.</param>
        protected override void Invoke(VisualElement sender)
        {
            SetDefaultValueIfNeeded((sender as Button).CornerRadius);

            sender.Animate(nameof(ButtonCornerRadiusAnimation), new Animation((d) =>
            {
                (sender as Button).CornerRadius = AnimationUtil.CalcCurrentValue(From, To, d);
            }),
                           length: Length,
                           easing: EasingValueConverter.Convert(Easing));
        }
Example #8
0
        /// <summary>
        /// Invoke View Margin Animation.
        /// </summary>
        /// <param name="sender">Sender.</param>
        protected override void Invoke(VisualElement sender)
        {
            SetDefaultValueIfNeeded((sender as View).Margin);

            sender.Animate(nameof(MarginThicknessAnimation), new Animation((d) =>
            {
                (sender as View).Margin = AnimationUtil.CalcCurrentValue(From, To, d);
            }),
                           length: Length,
                           easing: EasingValueConverter.Convert(Easing));
        }
        /// <summary>
        /// Invoke StackLayout Spacing animation.
        /// </summary>
        /// <param name="sender">Sender.</param>
        protected override void Invoke(VisualElement sender)
        {
            SetDefaultValueIfNeeded((sender as StackLayout).Spacing);

            var animation = new Animation((d) =>
            {
                (sender as StackLayout).Spacing = AnimationUtil.CalcCurrentValue(From, To, d);
            });

            sender.Animate(nameof(StackLayoutSpacingDoubleAnimation), animation, length: Length, easing: EasingValueConverter.Convert(Easing));
        }
        /// <summary>
        /// Invoke TimePicker TextColor animation.
        /// </summary>
        /// <param name="sender">Sender.</param>
        protected override void Invoke(VisualElement sender)
        {
            SetDefaultValueIfNeeded((sender as TimePicker).TextColor);

            sender.Animate(nameof(TimePickerTextColorAnimation), new Animation((d) =>
            {
                (sender as TimePicker).TextColor = AnimationUtil.CalcCurrentValue(From, To, d);
            }),
                           length: Length,
                           easing: EasingValueConverter.Convert(Easing));
        }
Example #11
0
        /// <summary>
        /// Invoke Button FontSize animation.
        /// </summary>
        /// <param name="sender">Sender.</param>
        protected override void Invoke(VisualElement sender)
        {
            SetDefaultValueIfNeeded((sender as Button).FontSize);

            var animation = new Animation((d) =>
            {
                (sender as Button).FontSize = AnimationUtil.CalcCurrentValue(From, To, d);
            });

            sender.Animate(nameof(ButtonFontSizeDoubleAnimation), animation, length: Length, easing: EasingValueConverter.Convert(Easing));
        }
Example #12
0
        /// <summary>
        /// Run color animation
        /// </summary>
        /// <returns>Animation task</returns>
        public static Task ColorTo(this VisualElement self, Color fromColor, Color toColor, string animationName, Action <Color> callback, uint length = 250, Easing easing = null)
        {
            easing = easing ?? Easing.Linear;
            var taskCompletionSource = new TaskCompletionSource <bool>();

            self.Animate(animationName, (t) =>
            {
                callback(ColorTransform(t, fromColor, toColor));
            }, 16, length, easing, (v, c) => taskCompletionSource.SetResult(c));

            return(taskCompletionSource.Task);
        }
        public static Task <bool> AnimateAsync <T>(this VisualElement element, string name, Func <double, T> transform,
                                                   Action <T> callback = null, uint rate = 16, uint length = 250, Easing easing = null)
        {
            easing   = easing ?? Easing.Linear;
            callback = callback ?? (_ => { });
            var taskCompletionSource = new TaskCompletionSource <bool>();

            element.Animate(name, transform, callback, rate, length, easing,
                            (v, c) => taskCompletionSource.SetResult(c));

            return(taskCompletionSource.Task);
        }
Example #14
0
        public static Task <bool> CallAnimation <t>(VisualElement element, string name, Func <double, t> transform, Action <t> callBack
                                                    , uint length, Easing easing, bool repeat)
        {
            easing = easing ?? Easing.Linear;
            var taskCompletionSource = new TaskCompletionSource <bool>();

            element.Animate(name, transform, callBack, 16, length, easing, (v, c) => { if (!repeat)
                                                                                       {
                                                                                           taskCompletionSource.SetResult(c);
                                                                                       }
                            }, () => repeat);
            return(taskCompletionSource.Task);
        }
    // gist.github.com/danvanderboom/0319256fa3f1f2f50be2
    public static void ShiftColorTo(this VisualElement view, Color sourceColor, Color targetColor, Action <Color> setter, uint length = 250, Easing easing = null)
    {
        view.Animate("ShiftColorTo",
                     x =>
        {
            var red   = sourceColor.R + (x * (targetColor.R - sourceColor.R));
            var green = sourceColor.G + (x * (targetColor.G - sourceColor.G));
            var blue  = sourceColor.B + (x * (targetColor.B - sourceColor.B));
            var alpha = sourceColor.A + (x * (targetColor.A - sourceColor.A));

            setter(Color.FromRgba(red, green, blue, alpha));
        },
                     length: length,
                     easing: easing);
    }
        private static Task <bool> BackgroundColorAnimation(VisualElement element, string name,
                                                            Func <double, Color> transform, uint length, Easing easing)
        {
            var taskCompletionSource = new TaskCompletionSource <bool>();

            element.Animate(
                name,
                transform,
                (color) => element.BackgroundColor = color,
                16,
                length,
                easing ?? Easing.Linear,
                (v, c) => taskCompletionSource.SetResult(c));

            return(taskCompletionSource.Task);
        }
Example #17
0
        static Task <bool> ColorAnimation(VisualElement view,
                                          string name,
                                          Func <double, Color> transform,
                                          Action <Color> callback,
                                          uint length,
                                          Easing easing)
        {
            easing = easing ?? Easing.Linear;
            TaskCompletionSource <bool> taskCompletionSource = new TaskCompletionSource <bool>();

            view.Animate <Color>(name, transform, callback, 16, length, easing, (value, canceled) =>
            {
                taskCompletionSource.SetResult(canceled);
            });

            return(taskCompletionSource.Task);
        }
        private static Task <bool> Animate <TPropertyType>(VisualElement view,
                                                           string name,
                                                           Func <double, TPropertyType> transform,
                                                           Action <TPropertyType> callback,
                                                           uint length,
                                                           Easing easing)
        {
            easing = easing ?? Easing.Linear;
            var taskCompletionSource = new TaskCompletionSource <bool>();

            view.Animate <TPropertyType>(name, transform, callback, 16, length, easing, (value, canceled) =>
            {
                taskCompletionSource.SetResult(canceled);
            });

            return(taskCompletionSource.Task);
        }
        protected override async void Invoke(VisualElement sender)
        {
            if (TargetProperty == null)
            {
                throw new NullReferenceException("Null Target property.");
            }

            if (Delay > 0)
                await Task.Delay(Delay);

            SetDefaultFrom((double)sender.GetValue(TargetProperty));

            sender.Animate($"AnimateInt{TargetProperty.PropertyName}", new Animation((progress) =>
            {
                sender.SetValue(TargetProperty, AnimationHelper.GetIntValue((int)From, (int)To, progress));
            }),
            length: Duration,
            easing: EasingHelper.GetEasing(Easing));
        }
Example #20
0
        public static void Begin(this Storyboard storyboard, VisualElement animationRoot, string handle,
                                 StoryboardFinishedCallback finishedCallback = null)
        {
            if (animationRoot == null)
            {
                throw new ArgumentNullException(nameof(animationRoot));
            }
            if (string.IsNullOrWhiteSpace(handle))
            {
                throw new InvalidOperationException("Impossible to begin animations when handle is empty");
            }

            var duration        = storyboard.Duration;
            var animationLength = (uint)duration.ToTimeSpan().TotalMilliseconds;
            var animation       = storyboard.ToAnimation(storyboard.Target ?? animationRoot);

            animationRoot.Animate(handle, animation, length: animationLength,
                                  finished: (x, cancelled) => finishedCallback?.Invoke(animation, x, cancelled));
        }
Example #21
0
 static void ColorAnimation(VisualElement element, string name, Func <double, Color> transform, Action <Color> callback, uint length, Easing easing, Action <Color, bool> finished = null, Func <bool> repeat = null)
 {
     easing = easing ?? Easing.Linear;
     element.Animate <Color>(name, transform, callback, 16, length, easing, finished, repeat);
 }