从0放大到需要的翻番,一、Blend设计单个UserControl的加大和运动效果

<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>

是因为,实际上只用到了ScaleTransform和TranslateTransform,所以,就只写了多个

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

TransformGroup部分更换为如下的艺术

if (uc.RenderTransform as TransformGroup == null)
{
    TransformGroup tg = new TransformGroup();
    uc.RenderTransform = tg;
    tg.Children.Add(new ScaleTransform());
    tg.Children.Add(new TranslateTransform());
}
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;
        }
    }
}

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

图片 1

图片 2

这两天老董让自家做个喷泉的成效,要把一个个UserControl从一个岗位喷出,然后,最后落在终极需要在的职务。

3、按照需要,拖拽左边的岁月线(黄线)到对应地点,然后,对红框中的Image控件,举办拓宽、收缩、地方调整等操作。

图片 3

2、在Blend右边的日子线,选中image控件,然后点击上边的+号,会弹出新增动画资源的弹窗,点击确定。

喷泉效果说白了,就是多少个步骤:1、放大,从0放大到需要的翻番;2、缩小,平移,从推广的倍数还原到UserControl的原本大小,并且定位到最后的地方。

TransformGroup部分更换为如下的点子

二、把XAML代码转换为C#

概念一个UCShow的类,里面包含了UC和尾声要一定的地点,如下,将急需显示的集纳,都助长到一个List里就ok了。

图片 4

喷泉效应说白了,就是多少个步骤:1、放大,从0放大到需要的倍数;2、裁减,平移,从推广的翻番还原到UserControl的原始大小,并且定位到最后的岗位。

 图片 5

尽管,唯有两步,不过,作为写动画的新手,仍然有点费事的,所以,选拔了先用Blend设计,然后再转移为C#代码的过程。

图片 6

功效如下:

1、在Blend里,新建个连串,然后,在Grid下,放个Canvas,在Canvas下放个Image(先拿Image来设计)

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();
    }
}

职能如下:

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;
        }
    }
}

 

有了XAML代码,转换成C#代码,就有益多了,只要把相应的一对连锁转换,然后启动动画,就OK了

概念一个UCShow的类,里面含有了UC和终极要稳住的职务,如下,将急需体现的集合,都增长到一个List里就ok了。

二、把XAML代码转换为C#

 DoubleAnimationUsingKeyFrames部分更换为如下的法门

写好了一个UserControl的效应将来,四个的实现起来,就容易了,无外乎就是for循环,设置参数而已。

2、在Blend左边的时刻线,选中image控件,然后点击下边的+号,会弹出新增动画资源的弹窗,点击确定。

图片 7

图片 8

图片 9

if (uc.RenderTransform as TransformGroup == null)
{
    TransformGroup tg = new TransformGroup();
    uc.RenderTransform = tg;
    tg.Children.Add(new ScaleTransform());
    tg.Children.Add(new TranslateTransform());
}
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)"));

 

一、Blend设计单个UserControl的推广和运动效果

<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>

图片 10

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)"));

图片 11

喷泉效应的代码如下:

 

即使,只有两步,可是,作为写动画的新手,仍旧有点费事的,所以,接纳了先用Blend设计,然后再转移为C#代码的经过。

 对比过XAML代码和C#代码将来,就发现,其实XAML转C#,依然尚未那么难堪的,就是把相应的岗位举办相关的添加值,可以一向从XAML里复制过来,如(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX),何人能记得住呢,反正自己记不住,太笨了。

3、按照需要,拖拽左边的时光线(黄线)到对应地方,然后,对红框中的Image控件,举办推广、缩短、地方调整等操作。

这两天老板让自己做个喷泉的效能,要把一个个UserControl从一个地方喷出,然后,最终落在最终需要在的岗位。

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

 DoubleAnimationUsingKeyFrames部分更换为如下的不二法门

写好了一个UserControl的效应未来,六个的实现起来,就容易了,无外乎就是for循环,设置参数而已。

 图片 12

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();
    }
}

4、XAML部分,暴发了对应的代码

图片 13

图片 14

4、XAML部分,暴发了对应的代码

 

 对比过XAML代码和C#代码以后,就发现,其实XAML转C#,如故尚未那么窘迫的,就是把相应的岗位举行相关的添加值,可以一贯从XAML里复制过来,如(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX),什么人能记得住呢,反正我记不住,太笨了。

喷泉效果的代码如下:

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

相关文章