2017-06-13 40 views
0

想象一下,我有一条线条,它是用三种颜色渐变的:深红色,红色和浅红色。我想在这一行中改变这些颜色的位置。我的目的是显示一些东西正沿着线路移动。 我不知道如何创建动画来改变颜色渐变的每一种颜色的位置。动画C#中线条上的渐变颜色.net

enter image description here

我发现这一点:https://docs.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/how-to-animate-the-position-or-color-of-a-gradient-stop

,但它是不是太清楚。

+0

在winforms中,您可以使用LinearGradientBrush并在Tick事件中向外移动起点。 – TaW

+1

我添加了它。 @TaW –

回答

2

下面是一个例子:

enter image description here

它使用LineraGradientBrush,移动限定矩形左上角的起始点和画旋转矩形到PictureBox

Point p1 = Point.Empty; 

private void timer1_Tick(object sender, EventArgs e) 
{ 
    int deltaX = -3; 
    int deltaY = -3; 
    p1 = new Point(p1.X + deltaX , p1.Y + deltaY); // roll.. 
    if (p1.X < deltaX * 1000) p1 = Point.Empty; // ..around 
    pictureBox1.Invalidate(); 

} 

private void pictureBox1_Paint(object sender, PaintEventArgs e) 
{ 
    float angle = 33f; 
    if (!timer1.Enabled) return; 
    Rectangle rectG = new Rectangle(p1.X, p1.Y, 122, 22); 
    Rectangle rectR = new Rectangle(22, 22, 222, 22); 
    LinearGradientBrush lBrush = new LinearGradientBrush(rectG, 
            Color.Red, Color.Red, angle, false); 

    ColorBlend cblend = new ColorBlend(5); 
    cblend.Colors = new Color[5] 
     { Color.Red, Color.Pink, Color.MistyRose, Color.LightCoral, Color.White }; 
    cblend.Positions = new float[5] { 0f, 0.2f, 0.5f, 0.8f, 1f }; 
    lBrush.InterpolationColors = cblend; 
    lBrush.WrapMode = WrapMode.TileFlipXY; 

    e.Graphics.RotateTransform(angle); 
    e.Graphics.TranslateTransform(22,11); 
    e.Graphics.FillRectangle(lBrush, rectR); 
} 

请注意,这是Winforms你不能得到真正平滑的动画,但如果你在油漆上的控件/表单是DoubleBufered at lea它不会闪烁..