代码之家  ›  专栏  ›  技术社区  ›  Cornelius

如何设置多边形的动画?(阅读:设置轮廓动画以更改形状)

  •  3
  • Cornelius  · 技术社区  · 17 年前

    我目前正在做一个Silverlight项目,我想制作一个简单多边形的动画(实际上是一个梯形)。具体来说,我想在一些事件发生后动态移动四点中的两点。我需要/想要调整大小并将其中一条平行边移动到另一个位置。

    我以前使用过动画,所以故事板和动画的一般概念对我来说并不陌生。但是如何在动画中移动多边形的点呢?是否有具有类似光学效果的替代方案(例如,设置路径动画)?
    是否有类似的PropertyPath可供我使用

    P3AnimBack.SetValue(Storyboard.TargetPropertyProperty, 
        new PropertyPath("(Path.Data).
            (PathGeometry.Figures)[0].(PathFigure.Segments)[0].
            (BezierSegment.Point3)"));
    

    ,如在 Point Animation in Silverlight 3 tutorial ?

    提前谢谢大家。:)

    2 回复  |  直到 17 年前
        2
  •  1
  •   Cornelius    17 年前

    我跟进了 Animated Polyline Interpolations in Silverlight 并且或多或少直接使用了这段代码——“窃取”PointCollectionInterpolator.cs类。

    然后我有了创建所需多边形并准备动画的方法:

    private void CreatePolygon(TextBox txtbx, string prop, Color curcol)
        {
            PointCollectionInterpolator pci = new PointCollectionInterpolator();
            pci.Points1 = new PointCollection() // Start Points
                {
                    new Point(...),
                    new Point(...),
                    new Point(...),
                    new Point(...),
                };
    
            pci.Points2 = new PointCollection() // End Points
                {
                    new Point(...),
                    new Point(...),
                    new Point(...),
                    new Point(...),
                };
    
            Polygon tmpply = new Polygon();
            LayoutRoot.Children.Add(tmpply);
    
            tmpply.Points = pci.InterpolatedPoints;
    
            DoubleAnimation animpci = new DoubleAnimation();
            animpci.Duration = someDuration;
            animpci.From = 0.0;
            animpci.To = 1.0;
            Storyboard.SetTarget(animpci, pci);
            Storyboard.SetTargetProperty(animpci, new PropertyPath("(Progress)"));
            myStoryBoard.Children.Add(animpci);
        }
    

    然后在一些随机事件处理程序中,我启动动画。此外,为了重用该方法,我将端点集合移动到起点集合中,并使用新端点更新插值器。(请记住将进度设置为0.0…)因此每次处理程序启动时,多边形都会无缝地变形为新的多边形。

    private void SomeEventHandler(object sender, RoutedEventArgs e) 
    {
        PointCollectionInterpolator polygonPCI = 
                this.referenceToPointCollectionInterpolator;
        polygonPCI.Points1 = polygonPCI.Points2;
        polygonPCI.Progress = 0.0;
        polygonPCI.Points2 = getNewEndPoints();
        myStoryBoard.Begin();
    }
    

    回想起来,我会将名称从点1和点2分别更改为起点和终点。 希望这有帮助。:)