Question

I want to animate retangle with using AnimationPoint. How can i do it? P.S. I searhed in google, but did not find what i need. I try this :

 private void drawRect()
    {
        var d = new Grid();

        var moveTranc = new TranslateTransform() { X = 1,Y = 1 };

        var e2 = new Rectangle()
        {
            Stroke = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 0, 200, 0)),
            RenderTransform = moveTranc,
            RadiusX = 100,
            RadiusY = 100,
            RenderTransformOrigin = new Point(0.5f,0.5f),
        };
        var pointAnimation = new PointAnimation()
        {
            Duration = new Duration(TimeSpan.FromSeconds(2)),
            From = new Point(e2.RadiusX,e2.RadiusY),
            To = new Point(450,450)
        };
        var storyBoard = new Storyboard();
         storyBoard.RepeatBehavior = RepeatBehavior.Forever;
         storyBoard.Children.Add(pointAnimation);

         Storyboard.SetTarget(pointAnimation, e2);
         Storyboard.SetTargetProperty(pointAnimation, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X).(TranslateTransform.Y)"));
        d.Children.Add(e2);
        ContentPanel.Children.Add(d);
        storyBoard.Begin();
    }
Was it helpful?

Solution

I found any variant of animating rectangle with using double animation

        Rectangle myRectangle;
        Storyboard sb;

        //creating rectangle
        myRectangle = new Rectangle();
        myRectangle.Width = 200;
        myRectangle.Height = 200;
        Color myColor = Color.FromArgb(255, 255, 0, 0);
        SolidColorBrush myBrush = new SolidColorBrush();
        myBrush.Color = myColor;
        myRectangle.Fill = myBrush;

        // Create the transform
        TranslateTransform moveTransform = new TranslateTransform();
        moveTransform.X = 0;
        moveTransform.Y = 0;
        myRectangle.RenderTransform = moveTransform;


        // Add the rectangle to the tree.
        if (!LayoutRoot.Children.Contains(myRectangle))    LayoutRoot.Children.Add(myRectangle);
        // Create a duration of 2 seconds.
        Duration duration = new Duration(TimeSpan.FromSeconds(1.0));
        // Create two DoubleAnimations and set their properties.
        DoubleAnimation myDoubleAnimationX = new DoubleAnimation();
        sb = new Storyboard();
        sb.Duration = duration;
        sb.Children.Add(myDoubleAnimationX);
        Storyboard.SetTarget(myDoubleAnimationX, moveTransform);
        Storyboard.SetTargetProperty(myDoubleAnimationX, new PropertyPath("X"));

        DoubleAnimation myDoubleAnimationY = new DoubleAnimation();
        sb.Children.Add(myDoubleAnimationY);
        Storyboard.SetTarget(myDoubleAnimationY, moveTransform);
        Storyboard.SetTargetProperty(myDoubleAnimationY, new PropertyPath("Y"));

        myDoubleAnimationX.To = 0;
        myDoubleAnimationX.From = 340;
        myDoubleAnimationY.To = 0;
        myDoubleAnimationY.From = 340;
        // Make the Storyboard a resource.
        if (!LayoutRoot.Resources.Contains("unique_id"))      LayoutRoot.Resources.Add("unique_id", sb);
        sb.Begin();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top