简易的喷泉效果

那两天COO让自身做个喷泉的效益,要把二个个UserControl从八个地方喷出,然后,最后落在最后须求在的岗位。

那两日首席执行官让自己做个喷泉的成效,要把一个个UserControl从1个职责喷出,然后,最终落在结尾供给在的职位。

喷泉成效说白了,便是五个步骤:1、放大,从0放大到必要的倍数;二 、收缩,平移,从加大的翻番还原到UserControl的本来大小,并且定位到结尾的职位。

喷泉成效说白了,就是多个步骤:壹 、放大,从0放大到须求的倍数;贰 、收缩,平移,从推广的翻番还原到UserControl的原本大小,并且定位到结尾的职位。

虽说,唯有两步,可是,作为写动画的新手,照旧有点费事的,所以,采取了先用Blend设计,然后再转移为C#代码的进程。

纵然,只有两步,不过,作为写动画的新手,依旧有点费事的,所以,选取了先用Blend设计,然后再转换为C#代码的经过。

① 、Blend设计单个UserControl的放大和移动作效果果

一 、Blend设计单个UserControl的加大和移动作效果果

① 、在Blend里,新建个品种,然后,在Grid下,放个Canvas,在Canvas下放个Image(先拿Image来设计)

① 、在Blend里,新建个类型,然后,在Grid下,放个Canvas,在Canvas下放个Image(先拿Image来设计)

图片 1

图片 2

二 、在Blend左边的时日线,选中image控件,然后点击上面包车型地铁+号,会弹出新增动画能源的弹窗,点击分明。

② 、在Blend左边的流年线,选中image控件,然后点击上边的+号,会弹出新增动画能源的弹窗,点击显著。

图片 3

图片 4

叁 、依照需求,拖拽左边的时日线(黄线)到相应地点,然后,对红框中的Image控件,进行拓宽、收缩、地方调整等操作。

三 、依照须要,拖拽左侧的岁月线(黄线)到相应地方,然后,对红框中的Image控件,实行拓宽、缩短、地方调整等操作。

图片 5

图片 6

④ 、XAML部分,产生了对应的代码

肆 、XAML部分,产生了对应的代码

<Window.Resources>
        <Storyboard x:Key="StoryboardFountain">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="image">
                <EasingDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:5" Value="5"/>
                <EasingDoubleKeyFrame KeyTime="0:0:10" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="image">
                <EasingDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:5" Value="5"/>
                <EasingDoubleKeyFrame KeyTime="0:0:10" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="image">
                <EasingDoubleKeyFrame KeyTime="0:0:5" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:10" Value="-186.5"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="image">
                <EasingDoubleKeyFrame KeyTime="0:0:5" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:10" Value="-107"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="Storyboard1"/>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Storyboard="{StaticResource StoryboardFountain}"/>
        </EventTrigger>
    </Window.Triggers>
    <Grid>
        <Canvas HorizontalAlignment="Center" VerticalAlignment="Center">
            <Image x:Name="image" Source="C:\Users\dell\Pictures\WP_20160112_18_55_33_Raw_LI.jpg" Width="50" Height="50" Canvas.Left="-0.5" Canvas.Top="-0.5" RenderTransformOrigin="0.5,0.5">
                <Image.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform/>
                        <SkewTransform/>
                        <RotateTransform/>
                        <TranslateTransform/>
                    </TransformGroup>
                </Image.RenderTransform>
            </Image>
        </Canvas>
    </Grid>
<Window.Resources>
        <Storyboard x:Key="StoryboardFountain">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" Storyboard.TargetName="image">
                <EasingDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:5" Value="5"/>
                <EasingDoubleKeyFrame KeyTime="0:0:10" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="image">
                <EasingDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:5" Value="5"/>
                <EasingDoubleKeyFrame KeyTime="0:0:10" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Storyboard.TargetName="image">
                <EasingDoubleKeyFrame KeyTime="0:0:5" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:10" Value="-186.5"/>
            </DoubleAnimationUsingKeyFrames>
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="image">
                <EasingDoubleKeyFrame KeyTime="0:0:5" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:10" Value="-107"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="Storyboard1"/>
    </Window.Resources>
    <Window.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
            <BeginStoryboard Storyboard="{StaticResource StoryboardFountain}"/>
        </EventTrigger>
    </Window.Triggers>
    <Grid>
        <Canvas HorizontalAlignment="Center" VerticalAlignment="Center">
            <Image x:Name="image" Source="C:\Users\dell\Pictures\WP_20160112_18_55_33_Raw_LI.jpg" Width="50" Height="50" Canvas.Left="-0.5" Canvas.Top="-0.5" RenderTransformOrigin="0.5,0.5">
                <Image.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform/>
                        <SkewTransform/>
                        <RotateTransform/>
                        <TranslateTransform/>
                    </TransformGroup>
                </Image.RenderTransform>
            </Image>
        </Canvas>
    </Grid>

 图片 7

 图片 8

二 、把XAML代码转换为C#

贰 、把XAML代码转换为C#

有了XAML代码,转换成C#代码,就便于多了,只要把相应的部分连锁转换,然后运营动画,就OK了

有了XAML代码,转换成C#代码,就便宜多了,只要把相应的一部分连锁转换,然后运转动画,就OK了

图片 9

图片 10

TransformGroup部分更换为如下的格局

TransformGroup部分更换为如下的法子

if (uc.RenderTransform as TransformGroup == null)
{
    TransformGroup tg = new TransformGroup();
    uc.RenderTransform = tg;
    tg.Children.Add(new ScaleTransform());
    tg.Children.Add(new TranslateTransform());
}
if (uc.RenderTransform as TransformGroup == null)
{
    TransformGroup tg = new TransformGroup();
    uc.RenderTransform = tg;
    tg.Children.Add(new ScaleTransform());
    tg.Children.Add(new TranslateTransform());
}

鉴于,实际上只用到了ScaleTransform和TranslateTransform,所以,就只写了八个

鉴于,实际上只用到了ScaleTransform和TranslateTransform,所以,就只写了多少个

图片 11

图片 12

 DoubleAnimationUsingKeyFrames部分更换为如下的法子

 DoubleAnimationUsingKeyFrames部分更换为如下的点子

EasingDoubleKeyFrame edf1 = new EasingDoubleKeyFrame(Init, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(i + first_value)));
EasingDoubleKeyFrame edf2 = new EasingDoubleKeyFrame(Mul, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(i + middle_value)));
EasingDoubleKeyFrame edf3 = new EasingDoubleKeyFrame(Org, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(i + end_value)));

DoubleAnimationUsingKeyFrames daukf1 = new DoubleAnimationUsingKeyFrames();
daukf1.KeyFrames.Add(edf1);
daukf1.KeyFrames.Add(edf2);
daukf1.KeyFrames.Add(edf3);
storyboard.Children.Add(daukf1);
Storyboard.SetTarget(daukf1, uc);
Storyboard.SetTargetProperty(daukf1, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));
EasingDoubleKeyFrame edf1 = new EasingDoubleKeyFrame(Init, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(i + first_value)));
EasingDoubleKeyFrame edf2 = new EasingDoubleKeyFrame(Mul, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(i + middle_value)));
EasingDoubleKeyFrame edf3 = new EasingDoubleKeyFrame(Org, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(i + end_value)));

DoubleAnimationUsingKeyFrames daukf1 = new DoubleAnimationUsingKeyFrames();
daukf1.KeyFrames.Add(edf1);
daukf1.KeyFrames.Add(edf2);
daukf1.KeyFrames.Add(edf3);
storyboard.Children.Add(daukf1);
Storyboard.SetTarget(daukf1, uc);
Storyboard.SetTargetProperty(daukf1, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));

 对比过XAML代码和C#代码未来,就发现,其实XAML转C#,依然没有那么困难的,正是把相应的职责展开有关的添加值,能够一向从XAML里复制过来,如(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX),什么人能记得住呢,反正自身记不住,太笨了。

 对比过XAML代码和C#代码以往,就意识,其实XAML转C#,依然不曾那么狼狈的,正是把相应的职位举行连锁的添加值,能够直接从XAML里复制过来,如(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX),哪个人能记得住呢,反正小编记不住,太笨了。

写好了3个UserControl的意义现在,多少个的兑现起来,就简单了,无外乎正是for循环,设置参数而已。

写好了3个UserControl的功能未来,四个的落实起来,就便于了,无外乎正是for循环,设置参数而已。

概念多个UCShow的类,里面含有了UC和终极要稳住的地方,如下,将急需出示的聚合,都拉长到一个List里就ok了。

概念2个UCShow的类,里面富含了UC和末段要稳住的义务,如下,将须要呈现的联谊,都丰富到多个List里就ok了。

public class UcShow
{
    private UIElement uc;
    private double top;
    private double left;

    public UIElement Uc
    {
        get
        {
            return uc;
        }

        set
        {
            uc = value;
        }
    }

    public double Top
    {
        get
        {
            return top;
        }

        set
        {
            top = value;
        }
    }

    public double Left
    {
        get
        {
            return left;
        }

        set
        {
            left = value;
        }
    }
}
public class UcShow
{
    private UIElement uc;
    private double top;
    private double left;

    public UIElement Uc
    {
        get
        {
            return uc;
        }

        set
        {
            uc = value;
        }
    }

    public double Top
    {
        get
        {
            return top;
        }

        set
        {
            top = value;
        }
    }

    public double Left
    {
        get
        {
            return left;
        }

        set
        {
            left = value;
        }
    }
}

效率如下:

功能如下:

图片 13

图片 14

 

 

喷泉意义的代码如下:

喷泉作用的代码如下:

class Fountain
{
    /// <summary>
    /// 喷泉效果
    /// </summary>
    /// <param name="cav">画布</param>
    /// <param name="uclist">展示集合</param>
    /// <param name="pL">喷出点左</param>
    /// <param name="pT">喷出点上</param>
    /// <param name="Mul">放大倍数</param>
    /// <param name="middle_value">放大时间点</param>
    /// <param name="end_value">还原时间点</param>
    public void FountainAnimation(Canvas cav,List<UcShow> uclist, double pL = 0, double pT = 0, double Mul = 10, double middle_value = 0.5, double end_value = 1)
    {
        if (uclist.Count <= 0)
        {
            return;
        }
        Storyboard storyboard = new Storyboard();

        double Init = 0;
        double Org = 1;
        double first_value = 0;

        for (int i = 0; i < uclist.Count; i++)
        {
            UIElement uc = uclist[i].Uc;
            double Top = uclist[i].Top;
            double Left = uclist[i].Left;
            Canvas.SetLeft(uc, pL);
            Canvas.SetTop(uc, pT);
            if (uc.RenderTransform as TransformGroup == null)
            {
                uc.RenderTransformOrigin = new Point(0.5, 0.5);
                TransformGroup tg = new TransformGroup();
                uc.RenderTransform = tg;
                tg.Children.Add(new ScaleTransform());
                tg.Children.Add(new TranslateTransform());
            }
            double first = i * 0.05 + first_value;
            double middle = i * 0.05 + middle_value;
            double end = i * 0.05 + end_value;
            EasingDoubleKeyFrame edf0 = new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0)));//所有元素起点都是0
            EasingDoubleKeyFrame edf1 = new EasingDoubleKeyFrame(Init, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(first)));
            EasingDoubleKeyFrame edf2 = new EasingDoubleKeyFrame(Mul, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(middle)));
            EasingDoubleKeyFrame edf3 = new EasingDoubleKeyFrame(Org, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(end)));

            DoubleAnimationUsingKeyFrames daukf1 = new DoubleAnimationUsingKeyFrames();
            daukf1.KeyFrames.Add(edf0);
            daukf1.KeyFrames.Add(edf1);
            daukf1.KeyFrames.Add(edf2);
            daukf1.KeyFrames.Add(edf3);
            storyboard.Children.Add(daukf1);
            Storyboard.SetTarget(daukf1, uc);
            Storyboard.SetTargetProperty(daukf1, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));

            DoubleAnimationUsingKeyFrames daukf2 = new DoubleAnimationUsingKeyFrames();
            daukf2.KeyFrames.Add(edf0);
            daukf2.KeyFrames.Add(edf1);
            daukf2.KeyFrames.Add(edf2);
            daukf2.KeyFrames.Add(edf3);
            storyboard.Children.Add(daukf2);
            Storyboard.SetTarget(daukf2, uc);
            Storyboard.SetTargetProperty(daukf2, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));

            DoubleAnimationUsingKeyFrames daukf3 = new DoubleAnimationUsingKeyFrames();
            EasingDoubleKeyFrame edf31 = new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(middle)));
            EasingDoubleKeyFrame edf32 = new EasingDoubleKeyFrame(Top, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(end)));
            daukf3.KeyFrames.Add(edf31);
            daukf3.KeyFrames.Add(edf32);
            storyboard.Children.Add(daukf3);
            Storyboard.SetTarget(daukf3, uc);
            Storyboard.SetTargetProperty(daukf3, new PropertyPath("(Canvas.Top)"));

            DoubleAnimationUsingKeyFrames daukf4 = new DoubleAnimationUsingKeyFrames();
            EasingDoubleKeyFrame edf41 = new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(middle)));
            EasingDoubleKeyFrame edf42 = new EasingDoubleKeyFrame(Left, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(end)));
            daukf4.KeyFrames.Add(edf41);
            daukf4.KeyFrames.Add(edf42);
            storyboard.Children.Add(daukf4);
            Storyboard.SetTarget(daukf4, uc);
            Storyboard.SetTargetProperty(daukf4, new PropertyPath("(Canvas.Left)"));
       cav.Children.Add(uc);
        }
        storyboard.FillBehavior = FillBehavior.HoldEnd;
        storyboard.Begin();
    }
}
class Fountain
{
    /// <summary>
    /// 喷泉效果
    /// </summary>
    /// <param name="cav">画布</param>
    /// <param name="uclist">展示集合</param>
    /// <param name="pL">喷出点左</param>
    /// <param name="pT">喷出点上</param>
    /// <param name="Mul">放大倍数</param>
    /// <param name="middle_value">放大时间点</param>
    /// <param name="end_value">还原时间点</param>
    public void FountainAnimation(Canvas cav,List<UcShow> uclist, double pL = 0, double pT = 0, double Mul = 10, double middle_value = 0.5, double end_value = 1)
    {
        if (uclist.Count <= 0)
        {
            return;
        }
        Storyboard storyboard = new Storyboard();

        double Init = 0;
        double Org = 1;
        double first_value = 0;

        for (int i = 0; i < uclist.Count; i++)
        {
            UIElement uc = uclist[i].Uc;
            double Top = uclist[i].Top;
            double Left = uclist[i].Left;
            Canvas.SetLeft(uc, pL);
            Canvas.SetTop(uc, pT);
            if (uc.RenderTransform as TransformGroup == null)
            {
                uc.RenderTransformOrigin = new Point(0.5, 0.5);
                TransformGroup tg = new TransformGroup();
                uc.RenderTransform = tg;
                tg.Children.Add(new ScaleTransform());
                tg.Children.Add(new TranslateTransform());
            }
            double first = i * 0.05 + first_value;
            double middle = i * 0.05 + middle_value;
            double end = i * 0.05 + end_value;
            EasingDoubleKeyFrame edf0 = new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0)));//所有元素起点都是0
            EasingDoubleKeyFrame edf1 = new EasingDoubleKeyFrame(Init, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(first)));
            EasingDoubleKeyFrame edf2 = new EasingDoubleKeyFrame(Mul, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(middle)));
            EasingDoubleKeyFrame edf3 = new EasingDoubleKeyFrame(Org, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(end)));

            DoubleAnimationUsingKeyFrames daukf1 = new DoubleAnimationUsingKeyFrames();
            daukf1.KeyFrames.Add(edf0);
            daukf1.KeyFrames.Add(edf1);
            daukf1.KeyFrames.Add(edf2);
            daukf1.KeyFrames.Add(edf3);
            storyboard.Children.Add(daukf1);
            Storyboard.SetTarget(daukf1, uc);
            Storyboard.SetTargetProperty(daukf1, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));

            DoubleAnimationUsingKeyFrames daukf2 = new DoubleAnimationUsingKeyFrames();
            daukf2.KeyFrames.Add(edf0);
            daukf2.KeyFrames.Add(edf1);
            daukf2.KeyFrames.Add(edf2);
            daukf2.KeyFrames.Add(edf3);
            storyboard.Children.Add(daukf2);
            Storyboard.SetTarget(daukf2, uc);
            Storyboard.SetTargetProperty(daukf2, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));

            DoubleAnimationUsingKeyFrames daukf3 = new DoubleAnimationUsingKeyFrames();
            EasingDoubleKeyFrame edf31 = new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(middle)));
            EasingDoubleKeyFrame edf32 = new EasingDoubleKeyFrame(Top, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(end)));
            daukf3.KeyFrames.Add(edf31);
            daukf3.KeyFrames.Add(edf32);
            storyboard.Children.Add(daukf3);
            Storyboard.SetTarget(daukf3, uc);
            Storyboard.SetTargetProperty(daukf3, new PropertyPath("(Canvas.Top)"));

            DoubleAnimationUsingKeyFrames daukf4 = new DoubleAnimationUsingKeyFrames();
            EasingDoubleKeyFrame edf41 = new EasingDoubleKeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(middle)));
            EasingDoubleKeyFrame edf42 = new EasingDoubleKeyFrame(Left, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(end)));
            daukf4.KeyFrames.Add(edf41);
            daukf4.KeyFrames.Add(edf42);
            storyboard.Children.Add(daukf4);
            Storyboard.SetTarget(daukf4, uc);
            Storyboard.SetTargetProperty(daukf4, new PropertyPath("(Canvas.Left)"));
       cav.Children.Add(uc);
        }
        storyboard.FillBehavior = FillBehavior.HoldEnd;
        storyboard.Begin();
    }
}

 

 

相关文章