C#动画,使用TranslateTransform和DoubleAnimation是 [英] c# Animation, using TranslateTransform and DoubleAnimation

查看:1687
本文介绍了C#动画,使用TranslateTransform和DoubleAnimation是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的地图,我想从A点移动到D,通过B和C.我已经声明的方法动画方:

I have a simple map and a square that I would like to move from point A to D, through B and C. I've declared a method Animate:

    public void Animate(double[] FirstPoint, double[] SecondPoint, Image img)
    {
        double x1 = FirstPoint[0];
        double x2 = SecondPoint[0];

        double y1 = FirstPoint[1];
        double y2 = SecondPoint[1];

        TranslateTransform trans = new TranslateTransform();
        img.RenderTransform = trans;
        DoubleAnimation anim1 = new DoubleAnimation(y1, y2, TimeSpan.FromSeconds(1));
        DoubleAnimation anim2 = new DoubleAnimation(x1, x2, TimeSpan.FromSeconds(1));
        trans.BeginAnimation(TranslateTransform.YProperty, anim1);
        trans.BeginAnimation(TranslateTransform.XProperty, anim2);
    }

主要的问题是,当我使用这些方法是这样的:

The main problem is that when I use those method like this:

obj.Animate(obj.A, obj.B, Car);
obj.Animate(obj.B, obj.C, Car);
obj.Animate(obj.C, obj.D, Car);

...有当我将只能从C点到D所示动画 MessageBox.Show 动画方法,正确显示动画。

...there's animation shown only from point C to D. When I added a MessageBox.Show to Animate method, it displayed the animation properly.

我觉得我可能没有完全理解其背后使用这些类的动画对象的概念。有什么想法?

I feel that I might not fully understand the concept behind using those classes to animate objects. Any thoughts?

推荐答案

您可以使用此code作为样本的理解,如何与多个动画工作。

You can use this code as the sample for understanding, how to work with multiple animations.

这是满code对于 MainWindow.xaml.cs

public partial class MainWindow : Window
{
    private const string CarTransform = "CarTransform";
    private Image _car;

    public MainWindow()
    {
        InitializeComponent();
        Loaded += MainWindow_Loaded;
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        Loaded -= MainWindow_Loaded;
        // create and add Car image to LayoutRoot grid
        _car = new Image();
        _car.Source = new BitmapImage(new Uri("/car-icon-hi.png", UriKind.Relative));
        _car.Width = 64;
        _car.Height = 64;
        _car.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
        _car.VerticalAlignment = System.Windows.VerticalAlignment.Top;
        _car.Margin = new Thickness(5);
        _car.RenderTransform = new TranslateTransform();
        LayoutRoot.Children.Add(_car);
    }

    private DoubleAnimation CreateAnimationFor(double from, double to, TimeSpan? beginTime, string targetName, DependencyProperty propertyPath)
    {
        DoubleAnimation da = new DoubleAnimation();
        da.From = from;
        da.To = to;
        da.Duration = new Duration(TimeSpan.FromMilliseconds(1000));
        if (beginTime != null)
            da.BeginTime = beginTime;

        Storyboard.SetTargetName(da, targetName);
        Storyboard.SetTargetProperty(da, new PropertyPath(propertyPath));

        return da;
    }

    private void StartAnimationClick(object sender, RoutedEventArgs e)
    {
        TranslateTransform _trans = _car.RenderTransform as TranslateTransform;

        this.RegisterName(CarTransform, _trans); // register name for TranslateTransform instance, this action is needed for working a Storyboard with multiple animations

        Storyboard sb = new Storyboard();
        // from A to B
        sb.Children.Add(CreateAnimationFor(0, 100, null, CarTransform, TranslateTransform.XProperty));
        sb.Children.Add(CreateAnimationFor(0, 0, null, CarTransform, TranslateTransform.YProperty));
        // from B to C
        sb.Children.Add(CreateAnimationFor(100, 100, TimeSpan.FromSeconds(1), CarTransform, TranslateTransform.XProperty));
        sb.Children.Add(CreateAnimationFor(0, 100, TimeSpan.FromSeconds(1), CarTransform, TranslateTransform.YProperty));
        // from C to D
        sb.Children.Add(CreateAnimationFor(100, 300, TimeSpan.FromSeconds(2), CarTransform, TranslateTransform.XProperty));
        sb.Children.Add(CreateAnimationFor(100, 250, TimeSpan.FromSeconds(2), CarTransform, TranslateTransform.YProperty));
        sb.Begin(this);
    }
}

这code的结果:

这篇关于C#动画,使用TranslateTransform和DoubleAnimation是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆