如何使用动画来画一条直线? [英] How to use animations to draw a straight line?

查看:200
本文介绍了如何使用动画来画一条直线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想画一个线,后面的C#通用应用$我的C $℃。我试着使用Line类和行不显示出来,但我不知道如何使动画所以它看起来像线在屏幕被绘制雨后春笋般冒出来,或代替它从外部转移,而不是屏幕的位置。我想看到的动画绘制从A点行至B点。任何想法,我怎么能做到这一点?谢谢!


我很感激​​,如果有人可以提供这样的一个例子


我曾试图按照这种合作不成功怎么办您动画在C#中的画布上线?

I want to draw a line in my code behind C# Universal app. I tried using the Line class and the line does show up, but I don't know how to make an animation so it looks like the line is being drawn in the screen instead of popping up, or instead of transferring it from outside of the screen to the location. I would like to see the animation drawing the line from point A to point B. Any idea how I can achieve this? Thanks!
I am very grateful if someone could provide an example of this
I have tried to follow this co unsuccessfully How do you animate a line on a canvas in C#?

推荐答案

你可以试试这个是这样的:

you can try this like this:

简短的解释:


  • 有两个点P1和P2。无论你喜欢,你可以设置。

  • 的DURATION毫秒是毫秒的动画需要。

  • 的stepMS是一个新行多久绘制毫秒。

  • 定时器TMR做的动漫作品

我们需要计算每一步的宽度,线的角度K和其偏置D。然后,我们开始计时。每一tick会做相同的:

we need to calculate the width of each step, the angle "k" of the line and its offset "d". Then we start the timer. Each "Tick" will do the same:


  • 增加步数

  • 沿线计算新端点

  • 拿捏

  • increase the step counter
  • calculate the new endpoint along the line
  • draw the line

在最后一个步骤的线绘制从P1到P2一个differen颜色

In the last step the line is drawn with a differen color from p1 to p2.

这是一个窗体的code ...

This is a Form's code...

using System;
using System.Drawing;
using System.Windows.Forms;

namespace TestSO {

    public partial class Form1 : Form {
        public Form1() { }

        PointF p1 = new PointF(10, 10);
        PointF p2 = new PointF(170, 30);

        float durationMS = 5000;
        float stepMS = 100;

        float stepWidthX;
        float k;
        float d;

        private Timer tmr = new Timer();

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            stepWidthX = (p2.X-p1.X)/ (durationMS / stepMS);

            k = (p2.Y - p1.Y) / (p2.X - p1.X);
            d = (p2.X * p1.Y - p1.X * p2.Y) / (p2.X - p1.X);

            tmr.Tick += tmr_Tick;
            tmr.Interval = (int)stepMS;
            tmr.Start();
        }

        private int stepCounter = 0;
        void tmr_Tick(object sender, EventArgs e) {
            stepCounter++;

            float x = p1.X + stepCounter * stepWidthX;
            float y = k * x + d;
            this.CreateGraphics().DrawLine(Pens.Black, p1, new PointF(x, y));


            if(stepCounter * stepMS > durationMS){
                stepCounter = 0;
                tmr.Stop();
                this.CreateGraphics().DrawLine(Pens.Red, p1, p2);
            }
        }

    }
}

这篇关于如何使用动画来画一条直线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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