Get current value of double animation

I have a story board in which I make a rectangle moving up like a needle in an MPH car wheel. So, I am doing this from 0 to 60, and I want to be able to get the current value while it is changing, so I can use this value to create a digital sensor.

How can I get the current value from beginning to end of double animation? I have something like this:

DoubleAnimation da = new DoubleAnimation(); da.Duration = new Duration(TimeSpan.FromSeconds(5)); da.From = 0; da.To = 60; RotateTransform rt = new RotateTransform(); rt.CenterX = 35; rt.CenterY = 0; rec3.RenderTransform = rt; rt.BeginAnimation(RotateTransform.AngleProperty, da); 

When he turns the rectangle up, I tried to get that angle, but he just returned zero.

+6
source share
4 answers

You must be doing something wrong, consider this example:

 <Rectangle Width="100" Height="10" Fill="Red" HorizontalAlignment="Left" RenderTransformOrigin="1,0.5"> <Rectangle.RenderTransform> <RotateTransform x:Name="rt" /> </Rectangle.RenderTransform> </Rectangle> <TextBlock Text="{Binding ElementName=rt, Path=Angle}" /> 
 rt.BeginAnimation(RotateTransform.AngleProperty, new DoubleAnimation(0, 60, TimeSpan.FromSeconds(1), FillBehavior.HoldEnd)); 

It displays the value just fine.


If you assign rt.Angle some variable that will have the value of the angle of transformation at this point in time, it will not change at the same time as the rt.Angle property.

If you are new to the data binding that I used in the above code, you can check out the overview .

+3
source

Not sure exactly what the problem you are facing, but I just checked and this code will show you the current angle:

 RotateTransform rt = new RotateTransform(); private void button1_Click(object sender, RoutedEventArgs e) { DoubleAnimation da = new DoubleAnimation(); da.Duration = new Duration(TimeSpan.FromSeconds(10)); da.From = 0; da.To = 60; rt.CenterX = 35; rt.CenterY = 0; rectangle1.RenderTransform = rt; rt.BeginAnimation(RotateTransform.AngleProperty, da); } private void button2_Click(object sender, RoutedEventArgs e) { var x = rt.Angle; // x will have the value of current angle } 

[Change]

If you need to access the private rt.Angle from another class, you can open it through a property, for example:

 public Double CurrentAngle { get { return rt.Angle; } } 
+2
source

Good evening. I had a similar problem, but with getting the position of the element while animating its movement. I solved this in my case using time. This approach is based on knowing the elapsed time since the start of the animation. Say we have this piece of element class code:

 Location = new Point(100, 100); // set animations var moveTime = 5000.0; var function = new SineEase { EasingMode = EasingMode.EaseInOut }; var shift = new Point(1000, 700); var locationAnimationX = new DoubleAnimation { Duration = TimeSpan.FromMilliseconds(moveTime), To = shift .X, EasingFunction = function }; var locationAnimationY = new DoubleAnimation { Duration = TimeSpan.FromMilliseconds(moveTime), To = shift .Y, EasingFunction = function }; // attach animations to this element and its properties Storyboard.SetTarget(locationAnimationX, this); Storyboard.SetTargetProperty(locationAnimationX, "(UIElement.RenderTransform).(CompositeTransform.TranslateX)"); Storyboard.SetTarget(locationAnimationY, this); Storyboard.SetTargetProperty(locationAnimationY, "(UIElement.RenderTransform).(CompositeTransform.TranslateY)"); // create storyboard and start animation of move var moveStory = new Storyboard(); moveStory.Children.Add(locationAnimationX); moveStory.Children.Add(locationAnimationY); moveStory.Begin(); var startTime = DateTime.Now; 

For example, we need to know the current location after a while. So let's get started.

 var elapsedTime = DateTime.Now.Subtract(startTime).TotalMilliseconds; var ratio = elapsedTime / moveTime; var funcCurrentValue = function.Ease(ratio); var currentShift = new Point(shift.X * funcCurrentValue, shift.Y * funcCurrentValue); 

Finally, the current location is set:

 var currentLocation = new Point(Location.X + currentShift.X, Location.Y + currentShift.Y); 
0
source

Despite the fact that some posters here claim differently, I also noticed that it is difficult to access the current value of the animated property during the animation. Here is a good solution that I used to use WPF animations to interpolate values ​​over time in my model.

 public class AnimatedDoubleValue : UIElement { public event PropertyChangedEventHandler PropertyChanged; private void FirePropertyChanged(string name) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name)); } private double _value_cache; // this stores the current animated value! public double Value { get { return (double)GetValue(ValueProperty); } set { if (_value_cache == value) return; _value_cache = value; SetValue(ValueProperty, value); FirePropertyChanged("Value"); } } public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(AnimatedDoubleValue), new UIPropertyMetadata(0.0, ValuePropertyChanged)); private static void ValuePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { var self = (AnimatedDoubleValue)sender; var value = (double)e.NewValue; self.UpdateValue(value); } private void UpdateValue(double value) { _value_cache = value; FirePropertyChanged("Value"); } } 

If I understand correctly, the OP wanted to reuse WPF's animation tools to model it. This is a good idea, since interpolating values ​​according to elapsed time is not trivial.

This AnimatedDoubleValue does not even have to be associated with the WPF widget tree. It can simply be used to receive updates to the animated value. Use the following:

 var doubleValue = new AnimatedDoubleValue(); var animation=new DoubleAnimation() { From=0, To=100, Duration=TimeSpan.FromSeconds(1) }; animation.SetValue(Storyboard.DesiredFrameRateProperty, 30); doubleValue.BeginAnimation(AnimatedDoubleValue.ValueProperty, animation, HandoffBehavior.SnapshotAndReplace); 

Now either listen to the PropertyChanged event on doubleValue, or access the Value property at any time.

0
source

Source: https://habr.com/ru/post/889824/


All Articles