本文介绍了矩阵动画错误。在WPF在code背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作的动画。

绘制路径与使用ArcSegment。

和沿该路径移动。

不过,从路径的动画和起点的起点不同。

我应该修改?

这一形象..后,立即开始播放动画。

圈位置是在路径的中间。

我要帮助我。请...

动画的一部分。来源$ C ​​$ C

我创建演示版本。
我的源$ C ​​$ C写的韩文。所以创建演示版

如果您继续单击第二个按钮。

出发点不同的路径和出发点圆的。

如果有其他的方法,可能是

当您更改源不要紧所有。

这就是成功。我们期待着良好的效果。

您是一个非常好的人。谢谢

XAML

 <窗​​口x:类=WpfApplication7.MainWindow
    的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
    的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
    标题=主窗口HEIGHT =400WIDTH =830>
<网格和GT;
    <按钮X:NAME =num_btCONTENT =1,创建数字的Horizo​​ntalAlignment =左VerticalAlignment =评出的WIDTH =118点击=num_bt_Click/>
    <按钮X:名称=开始CONTENT =2.启动动画的Horizo​​ntalAlignment =左保证金= - 2,50,0,0VerticalAlignment =评出的WIDTH =118点击=draw_bt_Click />
    <标签内容=继续合点击的Horizo​​ntalAlignment =左保证金=0,97,0,0VerticalAlignment =评出的WIDTH =116/>
    <标签内容=启动动画BTN的Horizo​​ntalAlignment =左保证金=0,120,0,0VerticalAlignment =评出的WIDTH =116/>
    <帆布X:NAME =画布的Horizo​​ntalAlignment =左HEIGHT =373保证金=137,0,0,0VerticalAlignment =评出的WIDTH =685/>< /网格和GT;

后面

code

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls的;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
用System.Windows.Media.Animation;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;命名空间WpfApplication7
{
///<总结>
/// MainWindow.xaml에대한상호작용논리
///< /总结>
公共部分类主窗口:窗口
{
    INT num_count = 10; //使用的数据数
    清单< INT> sampleNumbers; //保存了一些用于排序
    诠释计数= 1; //计数
    椭圆[]社交圈;    清单<双> SaveCircleStartPoint;
    结构SaveCircleProperty
    {
        公共双Circle_stposX;
        公共双Circle_stposY;
        公共双弧度;
    }    清单< SaveCircleProperty> SaveCircleInfos;    //保存更改分类信息
    结构SortingTraceInfo
    {
        公众诠释的位置; //改变位置
        公众诠释TargetPosition; //将改变位置
        公众诠释[] SortingNumbers; //排序号码
    }    //保存更改分类信息
    清单< SortingTraceInfo> sortingInfos;
    INT sortingInfosIndex;    公共主窗口()
    {
        的InitializeComponent();
    }    私人无效num_bt_Click(对象发件人,RoutedEventArgs E)
    {
        随机_random =新的随机(); //创建数
        INT [] = NUM​​ INT新[10];
        sampleNumbers =新的List< INT>();        的for(int i = 0; I< num_count;我++)
        {
            民[I] = _random.Next(1,50);
            sampleNumbers.Add(NUM [I]); //保存数数据
        }        冒泡();
        画圆(num_count,sampleNumbers);
    }    私人无效draw_bt_Click(对象发件人,RoutedEventArgs E)
    {
        //绘制路径,并开始动画
        SortingTraceInfo traceInfo = this.sortingInfos [this.sortingInfosIndex ++];        draw_path(traceInfo.Position,traceInfo.TargetPosition);
    }    //画出圆
    私人无效画圆(INT NUM,列表与LT; INT>大小)
    {
        //保存位置画一个圆
        SaveCircleStartPoint =新的List<双>();
        SaveCircleInfos =新的List< SaveCircleProperty>();        //要通过输入NUM数创建圈子
        圈=新的椭圆[NUM]
        INT位置=((685 / NUM) - 5); //循环起点        INT X = 50; //宽
        INT Y = 115; //高度        的for(int i = 0; I<民;我++)
        {
            双circlesize =尺寸[I] + 10;
            双半径= circlesize / 2; //半径
            双st_posX = X - 半径; //圆形的X轴位置
            双st_posY = Y - 半径; //圆形Y轴位置
            SaveCircleProperty CP =新SaveCircleProperty();            //定义圆财产
            圈[I] =新的椭圆();
            圈[I] .Name点=圈子+ i.ToString();
            圈[I] .Stroke = Brushes.Red;
            圈[I] .StrokeThickness = 5;
            圈[I] .WIDTH = circlesize;
            圈[I] .Height = circlesize;            // canvs的位置
            Canvas.SetLeft(圈[I],st_posX); //起始点x轴
            Canvas.SetTop(圈[I],st_posY); //起点Y轴            //保存circls的财产
            SaveCircleStartPoint.Add(st_posX);
            cp.Circle_stposX = st_posX;
            cp.Circle_stposY = st_posY;
            cp.radian =半径;
            SaveCircleInfos.Add(CP);            //起点
            X + =位置;            //添加帆布
            canvas.Children.Add(圈[I]);
            this.RegisterName(圈[I] .name和圈[I]);
        }
    }    私人无效draw_path(INT POS1,POS2 INT)
    {
        SaveCircleProperty SCP = this.SaveCircleInfos [POS1]
        SaveCircleProperty SCP2 = this.SaveCircleInfos [POS2]
        create_path(scp.Circle_stposX,scp.Circle_stposY,scp2.Circle_stposX,scp2.Circle_stposY,scp.radian,scp2.radian,POS1,POS2);
    }    私人无效create_path(双的startPoint,双startPoint2,双端点,双端点2,双radian1,双radian2,INT位置,诠释tartgetPosition)
    {
        // REGIST名
        NameScope.SetNameScope(这一点,新名称范围());
        NameScope.SetNameScope(帆布,新名称范围());        // circls位置复位
        Canvas.SetLeft(圈[位置],0);
        Canvas.SetTop(圈[位置],0);        Canvas.SetLeft(圈[tartgetPosition],0);
        Canvas.SetTop(圈[tartgetPosition],0);        ///<总结>
        ///圈左边的路去从左至右
        ///< /总结>        的PathGeometry pathGeometryLeft =新的PathGeometry();
        的PathFigure Leftfigure =新的PathFigure();
        Leftfigure.StartPoint =新的点(的startPoint,startPoint2); // x轴,y轴起点        //点​​(x​​轴,y轴)
        Leftfigure.Segments.Add(新ArcSegment(新点(端点,startPoint2),新的大小(150,终端 - 的startPoint),90,假的,SweepDirection.Clockwise,真实));
        pathGeometryLeft.Figures.Add(Leftfigure);
        路径Leftpath =新路径();
        Leftpath.Data = pathGeometryLeft;
        Leftpath.Stroke = Brushes.Green;
        canvas.Children.Add(Leftpath);        MatrixTransform LeftcircleMatrixTransform =新MatrixTransform();
        圈[位置] .RenderTransform = LeftcircleMatrixTransform;        this.RegisterName(LeftCircleMatrixTransform+ count.ToString(),LeftcircleMatrixTransform);        pathGeometryLeft.Freeze();        //创建一个MatrixAnimationUsingPath移动
        //沿着动画路径圈
        //其MatrixTransform。
        MatrixAnimationUsingPath Leftmatrixanimation =新MatrixAnimationUsingPath();
        Leftmatrixanimation.PathGeometry = pathGeometryLeft;
        Leftmatrixanimation.Duration = TimeSpan.FromSeconds(5);
        Leftmatrixanimation.DoesRotateWithTangent = TRUE;        //设置动画为目标的矩阵属性
        //名为ButtonMatrixTransform的MatrixTransform的。
        Storyboard.SetTargetName(LeftmatrixanimationLeftCircleMatrixTransform+ count.ToString());
        Storyboard.SetTargetProperty(Leftmatrixanimation,新的PropertyPath(Matr​​ixTransform.MatrixProperty));        //创建一个Storyboard,以遏制和应用动画。
        故事板LeftpathAnimationStoryboard =新的故事板();
        LeftpathAnimationStoryboard.Children.Add(Leftmatrixanimation);        LeftpathAnimationStoryboard.Begin(本);        ///<总结>
        ///从权圈的路到左(路径)
        ///< /总结>        的PathGeometry RightpathGeometry =新的PathGeometry();
        的PathFigure Rightfigure =新的PathFigure();
        Rightfigure.StartPoint =新的点(端点,端点2 +(radian2 * 2)); //点​​¯x축,Y축시작점        //点​​(x​​축끝,Y축끝점)
        Rightfigure.Segments.Add(新ArcSegment(新点(的startPoint,端点2 +(radian2 * 2)),新的大小(150,终端 - 的startPoint),90,假的,SweepDirection.Clockwise,真实));        // this.RegisterName(RightmyArcSegment,Rightfigure.Segments);        RightpathGeometry.Figures.Add(Rightfigure);
        路径Rightpath =新路径();
        Rightpath.Data = RightpathGeometry;
        Rightpath.Stroke = Brushes.Red;
        canvas.Children.Add(Rightpath);        MatrixTransform RightcircleMatrixTransform =新MatrixTransform();
        圈[tartgetPosition] .RenderTransform = RightcircleMatrixTransform;        this.RegisterName(RightCircleMatrixTransform+ count.ToString(),RightcircleMatrixTransform);        RightpathGeometry.Freeze();        MatrixAnimationUsingPath Rightmatrixanimation =新MatrixAnimationUsingPath();
        Rightmatrixanimation.PathGeometry = RightpathGeometry;
        Rightmatrixanimation.Duration = TimeSpan.FromSeconds(10);        //设置动画的DoesRotateWithTangent财产
        //为true,这样除了旋转矩形
        //要移动它。
        Rightmatrixanimation.DoesRotateWithTangent = TRUE;        //设置动画为目标的矩阵属性
        //名为CircleMatrixTransform的MatrixTransform的。
        Storyboard.SetTargetName(RightmatrixanimationRightCircleMatrixTransform+ count.ToString());
        Storyboard.SetTargetProperty(Rightmatrixanimation,新的PropertyPath(Matr​​ixTransform.MatrixProperty));        //创建一个Storyboard,以遏制和应用动画。
        故事板RightpathAnimationStoryboard =新的故事板();
        RightpathAnimationStoryboard.Children.Add(Rightmatrixanimation);        RightpathAnimationStoryboard.Begin(本);        椭圆温度= NULL;
        TEMP =圈[位置]
        圈[位置] =圈[tartgetPosition]
        圈[tartgetPosition] =温度;
        算上++;
    }
    //버블정렬
    私人列表< INT>冒泡()
    {
        sortingInfos =新的List< SortingTraceInfo>();
        清单< INT>排序=新的List< INT>(sampleNumbers);        的for(int i = 0; I< sorting.Count - 1;我++)
        {
            对于(INT J = 0; J< sorting.Count - 1 - I; J ++)
            {
                如果(排序[J]>分类[J + 1])
                {
                    交换(排序,J,J + 1);                    SortingTraceInfo的SortInfo =新SortingTraceInfo(); //
                    sortInfo.Position = j的; //保存改变位置
                    sortInfo.TargetPosition = J + 1; //保存会改变立场
                    sortInfo.SortingNumbers = sorting.ToArray(); //排序号码保存到数组
                    sortingInfos.Add(的SortInfo); //변경정보등을sortingInfos리스트에저장
                }
            }
        }
        返回排序;
    }    私人无效掉期(名单< INT> NUM,INT I,诠释J)
    {
        INT TEMP = NUM​​ [I]
        NUM [I] = NUM​​ [J]。
        NUM [J] =温度;
    }
  }
}


解决方案

 私人无效draw_path(INT POS1,POS2 INT)
    {
        VAR圈= canvas.Children.OfType<椭圆方式>()排序依据(Q =>(双)q.GetValue(Canvas.LeftProperty))了ToList()。        VAR CIRCLE1 =圈[POS1]
        VAR CIRCLE2 =圈[POS2]        //水平动画CIRCLE1
        故事板SB1 =新的故事板();
        双1跳转=(双)circle1.GetValue(Canvas.LeftProperty);
        双TO1 =(双)circle2.GetValue(Canvas.LeftProperty)+ circle2.ActualWidth / 2 - circle1.ActualWidth / 2;
        DoubleAnimation是DA1 =新DoubleAnimation是(1跳转,TO1,新的持续时间(TimeSpan.FromSeconds(0.6)));        Storyboard.SetTarget(SB1,CIRCLE1);
        Storyboard.SetTargetProperty(SB1,新的PropertyPath(Canvas.LeftProperty));        sb1.Children.Add(DA1);        //水平动画CIRCLE2
        故事板SB2 =新的故事板();
        双from2 =(双)circle2.GetValue(Canvas.LeftProperty);
        双TO2 =(双)circle1.GetValue(Canvas.LeftProperty)+ circle1.ActualWidth / 2 - circle2.ActualWidth / 2;
        DoubleAnimation是DA2 =新DoubleAnimation是(from2,TO2,新的持续时间(TimeSpan.FromSeconds(0.6)));        Storyboard.SetTarget(SB2,CIRCLE2);
        Storyboard.SetTargetProperty(SB2,新的PropertyPath(Canvas.LeftProperty));        sb2.Children.Add(DA2);        //垂直动画CIRCLE1
        故事板SB3 =新的故事板();
        双from3 =(双)circle1.GetValue(Canvas.TopProperty);
        双TO3 =(双)circle1.GetValue(Canvas.TopProperty)+ circle1.ActualWidth;
        DoubleAnimation是DA3 =新DoubleAnimation是(from3,TO3,新的持续时间(TimeSpan.FromSeconds(0.3)));
        da3.AutoReverse = TRUE;
        da3.AccelerationRatio = 0.1;        Storyboard.SetTarget(SB3,CIRCLE1);
        Storyboard.SetTargetProperty(SB3,新的PropertyPath(Canvas.TopProperty));        sb3.Children.Add(DA3);        //垂直动画CIRCLE2
        故事板SB4 =新的故事板();
        双from4 =(双)circle2.GetValue(Canvas.TopProperty);
        双TO4 =(双)circle2.GetValue(Canvas.TopProperty) - circle2.ActualWidth;
        DoubleAnimation是DA4 =新DoubleAnimation是(from4,TO4,新的持续时间(TimeSpan.FromSeconds(0.3)));
        da4.AutoReverse = TRUE;
        da4.AccelerationRatio = 0.1;        Storyboard.SetTarget(SB4,CIRCLE2);
        Storyboard.SetTargetProperty(SB4,新的PropertyPath(Canvas.TopProperty));        sb4.Children.Add(DA4);        sb1.Begin();
        sb2.Begin();
        sb3.Begin();
        sb4.Begin();
    }

希望,它有助于

I making animation.

Draw a path with use a ArcSegment.

And moves along to the path.

However, different from startingpoint of animation and startingpoint of path.

What should modify?

this image .. Immediately after the starting the animation.

Circle location is in the middle of the Path.

I Want to help me. please...

animation part. source code

i create demo version.my source code written in korean. so create demo version

If you continue to click the second button.

Different starting point of the path and starting point Of a circle.

If there are other methods that may be.

It does not matter all when you change the source.

This is just to succeed. We look forward to good results.

You are a really good person. thank you

xaml

<Window x:Class="WpfApplication7.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="400" Width="830">
<Grid>
    <Button x:Name="num_bt" Content="1.create number" HorizontalAlignment="Left" VerticalAlignment="Top" Width="118" Click="num_bt_Click"/>
    <Button x:Name="start" Content="2.start animation" HorizontalAlignment="Left" Margin="-2,50,0,0" VerticalAlignment="Top" Width="118" Click="draw_bt_Click"/>
    <Label Content="contiue click" HorizontalAlignment="Left" Margin="0,97,0,0" VerticalAlignment="Top" Width="116"/>
    <Label Content="start animation btn" HorizontalAlignment="Left" Margin="0,120,0,0" VerticalAlignment="Top" Width="116"/>
    <Canvas x:Name="canvas" HorizontalAlignment="Left" Height="373" Margin="137,0,0,0" VerticalAlignment="Top" Width="685"/>

</Grid>

code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication7
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
    int num_count = 10;  // use data number
    List<int> sampleNumbers;    // Save a number to use for sorting
    int count = 1;  // count
    Ellipse[] Circle;

    List<double> SaveCircleStartPoint;
    struct SaveCircleProperty
    {
        public double Circle_stposX;
        public double Circle_stposY;
        public double radian;
    }

    List<SaveCircleProperty> SaveCircleInfos;

    // save changed sorting info
    struct SortingTraceInfo
    {
        public int Position;    // change position
        public int TargetPosition;  // will change position
        public int[] SortingNumbers;    // sorting numbers
    }

    // save changed sorting info 
    List<SortingTraceInfo> sortingInfos;
    int sortingInfosIndex;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void num_bt_Click(object sender, RoutedEventArgs e)
    {
        Random _random = new Random(); // create number
        int[] num = new int[10];
        sampleNumbers = new List<int>();

        for (int i = 0; i < num_count; i++)
        {
            num[i] = _random.Next(1, 50);
            sampleNumbers.Add(num[i]);   // save number data
        }

        BubbleSort();
        drawCircle(num_count, sampleNumbers);
    }

    private void draw_bt_Click(object sender, RoutedEventArgs e)
    {
        // draw a path and start animation
        SortingTraceInfo traceInfo = this.sortingInfos[this.sortingInfosIndex++];

        draw_path(traceInfo.Position, traceInfo.TargetPosition);
    }

    // draw circle
    private void drawCircle(int num, List<int> size)
    {
        // saving a position to draw a circle
        SaveCircleStartPoint = new List<double>();
        SaveCircleInfos = new List<SaveCircleProperty>();

        // To create the circle by the number of input num
        Circle = new Ellipse[num];
        int location = ((685 / num) - 5); // circle startpoint

        int x = 50; // Width
        int y = 115; // Height

        for (int i = 0; i < num; i++)
        {
            double circlesize = size[i] + 10;
            double radius = circlesize / 2; // radius
            double st_posX = x - radius;    // circular X-axis position
            double st_posY = y - radius;    // circular Y-axis position
            SaveCircleProperty cp = new SaveCircleProperty();

            // define circle property
            Circle[i] = new Ellipse();
            Circle[i].Name = "circle" + i.ToString();
            Circle[i].Stroke = Brushes.Red;
            Circle[i].StrokeThickness = 5;
            Circle[i].Width = circlesize;
            Circle[i].Height = circlesize;

            // position of canvs
            Canvas.SetLeft(Circle[i], st_posX); // startpoint x-axis
            Canvas.SetTop(Circle[i], st_posY); // startpoint Y-axis

            // save circls's property
            SaveCircleStartPoint.Add(st_posX);
            cp.Circle_stposX = st_posX;
            cp.Circle_stposY = st_posY;
            cp.radian = radius;
            SaveCircleInfos.Add(cp);

            // startpoint 
            x += location;

            // add canvas
            canvas.Children.Add(Circle[i]);
            this.RegisterName(Circle[i].Name, Circle[i]);
        }
    }

    private void draw_path(int pos1, int pos2)
    {
        SaveCircleProperty scp = this.SaveCircleInfos[pos1];
        SaveCircleProperty scp2 = this.SaveCircleInfos[pos2];
        create_path(scp.Circle_stposX, scp.Circle_stposY, scp2.Circle_stposX, scp2.Circle_stposY, scp.radian, scp2.radian, pos1, pos2);
    }

    private void create_path(double startPoint, double startPoint2, double endPoint, double endPoint2, double radian1, double radian2, int position, int tartgetPosition)
    {
        // regist name
        NameScope.SetNameScope(this, new NameScope());
        NameScope.SetNameScope(canvas, new NameScope());

        // circls position reset
        Canvas.SetLeft(Circle[position], 0);
        Canvas.SetTop(Circle[position], 0);

        Canvas.SetLeft(Circle[tartgetPosition], 0);
        Canvas.SetTop(Circle[tartgetPosition], 0);

        ///<summary>
        /// Circle left the road going from left to right
        ///</summary>

        PathGeometry pathGeometryLeft = new PathGeometry();
        PathFigure Leftfigure = new PathFigure();
        Leftfigure.StartPoint = new Point(startPoint, startPoint2); // x-axis , y-axis start point

        // point(x-axis, y-axis) 
        Leftfigure.Segments.Add(new ArcSegment(new Point(endPoint, startPoint2), new Size(150, endPoint - startPoint), 90, false, SweepDirection.Clockwise, true));
        pathGeometryLeft.Figures.Add(Leftfigure);
        Path Leftpath = new Path();
        Leftpath.Data = pathGeometryLeft;
        Leftpath.Stroke = Brushes.Green;
        canvas.Children.Add(Leftpath);

        MatrixTransform LeftcircleMatrixTransform = new MatrixTransform();
        Circle[position].RenderTransform = LeftcircleMatrixTransform;

        this.RegisterName("LeftCircleMatrixTransform" + count.ToString(), LeftcircleMatrixTransform);

        pathGeometryLeft.Freeze();

        // Create a MatrixAnimationUsingPath to move the
        // circle along the path by animating
        // its MatrixTransform.
        MatrixAnimationUsingPath Leftmatrixanimation = new MatrixAnimationUsingPath();
        Leftmatrixanimation.PathGeometry = pathGeometryLeft;
        Leftmatrixanimation.Duration = TimeSpan.FromSeconds(5);
        Leftmatrixanimation.DoesRotateWithTangent = true;

        // Set the animation to target the Matrix property
        // of the MatrixTransform named "ButtonMatrixTransform".
        Storyboard.SetTargetName(Leftmatrixanimation, "LeftCircleMatrixTransform" + count.ToString());
        Storyboard.SetTargetProperty(Leftmatrixanimation, new PropertyPath(MatrixTransform.MatrixProperty));

        // Create a Storyboard to contain and apply the animation.
        Storyboard LeftpathAnimationStoryboard = new Storyboard();
        LeftpathAnimationStoryboard.Children.Add(Leftmatrixanimation);

        LeftpathAnimationStoryboard.Begin(this);

        /// <summary>
        /// The road to the right circle from right to left (path)
        /// </summary>

        PathGeometry RightpathGeometry = new PathGeometry();
        PathFigure Rightfigure = new PathFigure();
        Rightfigure.StartPoint = new Point(endPoint, endPoint2 + (radian2 * 2)); // x축 , y축 시작점

        // point(x축 끝, y축 끝점) 
        Rightfigure.Segments.Add(new ArcSegment(new Point(startPoint, endPoint2 + (radian2 * 2)), new Size(150, endPoint - startPoint), 90, false, SweepDirection.Clockwise, true));

        // this.RegisterName("RightmyArcSegment", Rightfigure.Segments);

        RightpathGeometry.Figures.Add(Rightfigure);
        Path Rightpath = new Path();
        Rightpath.Data = RightpathGeometry;
        Rightpath.Stroke = Brushes.Red;
        canvas.Children.Add(Rightpath);

        MatrixTransform RightcircleMatrixTransform = new MatrixTransform();
        Circle[tartgetPosition].RenderTransform = RightcircleMatrixTransform;

        this.RegisterName("RightCircleMatrixTransform" + count.ToString(), RightcircleMatrixTransform);

        RightpathGeometry.Freeze();

        MatrixAnimationUsingPath Rightmatrixanimation = new MatrixAnimationUsingPath();
        Rightmatrixanimation.PathGeometry = RightpathGeometry;
        Rightmatrixanimation.Duration = TimeSpan.FromSeconds(10);

        // Set the animation's DoesRotateWithTangent property
        // to true so that rotates the rectangle in addition
        // to moving it.
        Rightmatrixanimation.DoesRotateWithTangent = true;

        // Set the animation to target the Matrix property
        // of the MatrixTransform named "CircleMatrixTransform".
        Storyboard.SetTargetName(Rightmatrixanimation, "RightCircleMatrixTransform" + count.ToString());
        Storyboard.SetTargetProperty(Rightmatrixanimation, new PropertyPath(MatrixTransform.MatrixProperty));

        // Create a Storyboard to contain and apply the animation.
        Storyboard RightpathAnimationStoryboard = new Storyboard();
        RightpathAnimationStoryboard.Children.Add(Rightmatrixanimation);

        RightpathAnimationStoryboard.Begin(this);

        Ellipse temp = null;
        temp = Circle[position];
        Circle[position] = Circle[tartgetPosition];
        Circle[tartgetPosition] = temp;
        count++;
    }


    // 버블 정렬
    private List<int> BubbleSort()
    {
        sortingInfos = new List<SortingTraceInfo>();
        List<int> sorting = new List<int>(sampleNumbers);

        for (int i = 0; i < sorting.Count - 1; i++)
        {
            for (int j = 0; j < sorting.Count - 1 - i; j++)
            {
                if (sorting[j] > sorting[j + 1])
                {
                    Swap(sorting, j, j + 1);

                    SortingTraceInfo sortInfo = new SortingTraceInfo(); // 
                    sortInfo.Position = j;  // save change position
                    sortInfo.TargetPosition = j + 1;    // save will change position
                    sortInfo.SortingNumbers = sorting.ToArray();   // sorting number saved to array 
                    sortingInfos.Add(sortInfo); // 변경 정보등을 sortingInfos 리스트에 저장
                }
            }
        }
        return sorting;
    }

    private void Swap(List<int> num, int i, int j)
    {
        int temp = num[i];
        num[i] = num[j];
        num[j] = temp;
    }
  }
}
解决方案
    private void draw_path(int pos1, int pos2)
    {
        var circles = canvas.Children.OfType<Ellipse>().OrderBy(q => (double)q.GetValue(Canvas.LeftProperty)).ToList();

        var circle1 = circles[pos1];
        var circle2 = circles[pos2];

        // horizontal animation for circle1
        Storyboard sb1 = new Storyboard();
        double from1 = (double)circle1.GetValue(Canvas.LeftProperty);
        double to1 = (double)circle2.GetValue(Canvas.LeftProperty) + circle2.ActualWidth / 2 - circle1.ActualWidth / 2;
        DoubleAnimation da1 = new DoubleAnimation(from1, to1, new Duration(TimeSpan.FromSeconds(0.6)));

        Storyboard.SetTarget(sb1, circle1);
        Storyboard.SetTargetProperty(sb1, new PropertyPath(Canvas.LeftProperty));

        sb1.Children.Add(da1);

        // horizontal animation for circle2
        Storyboard sb2 = new Storyboard();
        double from2 = (double)circle2.GetValue(Canvas.LeftProperty);
        double to2 = (double)circle1.GetValue(Canvas.LeftProperty) + circle1.ActualWidth / 2 - circle2.ActualWidth / 2;
        DoubleAnimation da2 = new DoubleAnimation(from2, to2, new Duration(TimeSpan.FromSeconds(0.6)));

        Storyboard.SetTarget(sb2, circle2);
        Storyboard.SetTargetProperty(sb2, new PropertyPath(Canvas.LeftProperty));

        sb2.Children.Add(da2);

        // vertical animation for circle1
        Storyboard sb3 = new Storyboard();
        double from3 = (double)circle1.GetValue(Canvas.TopProperty);
        double to3 = (double)circle1.GetValue(Canvas.TopProperty) + circle1.ActualWidth;
        DoubleAnimation da3 = new DoubleAnimation(from3, to3, new Duration(TimeSpan.FromSeconds(0.3)));
        da3.AutoReverse = true;
        da3.AccelerationRatio = 0.1;

        Storyboard.SetTarget(sb3, circle1);
        Storyboard.SetTargetProperty(sb3, new PropertyPath(Canvas.TopProperty));

        sb3.Children.Add(da3);

        // vertical animation for circle2
        Storyboard sb4 = new Storyboard();
        double from4 = (double)circle2.GetValue(Canvas.TopProperty);
        double to4 = (double)circle2.GetValue(Canvas.TopProperty) - circle2.ActualWidth;
        DoubleAnimation da4 = new DoubleAnimation(from4, to4, new Duration(TimeSpan.FromSeconds(0.3)));
        da4.AutoReverse = true;
        da4.AccelerationRatio = 0.1;

        Storyboard.SetTarget(sb4, circle2);
        Storyboard.SetTargetProperty(sb4, new PropertyPath(Canvas.TopProperty));

        sb4.Children.Add(da4);

        sb1.Begin();
        sb2.Begin();
        sb3.Begin();
        sb4.Begin();
    }

Hope, it helps

这篇关于矩阵动画错误。在WPF在code背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 07:34