silverlight应用二:ScaleTransform动画

将对象进行缩放,除了可以利用它的Width和Height属性外,还可以利用ScaleTransform进行变换,如下:

<Image Source="Image/cancel.jpg" x:Name="targetElement"  Width="100" Canvas.Left="100" Canvas.Top="50" Height="50">
            <Image.RenderTransform>
                <TransformGroup>
                    <!--ScaleX="1" ScaleY="1" 初始化时保持不变,中心坐标X=With/2,Y=Height/2-->
                    <ScaleTransform ScaleX="1" ScaleY="1" CenterX="50" CenterY="25"/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </Image.RenderTransform>
        </Image>

后台C#代码如下:

//鼠标进入事件
            targetElement.MouseEnter += (mes,mee) =>
            {
                //x方向
                Storyboard storyboard = new Storyboard();
                DoubleAnimation doubleAnimation = new DoubleAnimation();
                doubleAnimation.To = 2;
                doubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(300));
                Storyboard.SetTarget(doubleAnimation,targetElement);
                Storyboard.SetTargetProperty(doubleAnimation,new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));
                storyboard.Children.Add(doubleAnimation);
                //Y方向
                doubleAnimation = new DoubleAnimation();
                doubleAnimation.To = 2;
                doubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(300));
                Storyboard.SetTarget(doubleAnimation,new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));
                storyboard.Children.Add(doubleAnimation);
                //启动动画
                storyboard.Begin();
            };
            //鼠标移出事件
            targetElement.MouseLeave += (mls,mle) =>
            {
                //x方向
                Storyboard storyboard = new Storyboard();
                DoubleAnimation doubleAnimation = new DoubleAnimation();
                doubleAnimation.To = 1;
                doubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(300));
                Storyboard.SetTarget(doubleAnimation,new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));
                storyboard.Children.Add(doubleAnimation);
                //Y方向
                doubleAnimation = new DoubleAnimation();
                doubleAnimation.To = 1;
                doubleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(300));
                Storyboard.SetTarget(doubleAnimation,new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));
                storyboard.Children.Add(doubleAnimation);
                //启动动画
                storyboard.Begin();
            };

相关文章

如何在Silverlight4(XAML)中绑定IsEnabled属性?我试过简单的...
我正在编写我的第一个vb.net应用程序(但我也会在这里标记c#,...
ProcessFile()是在UIThread上运行还是在单独的线程上运行.如...
我从同行那里听说,对sharepoint的了解对职业生涯有益.我们不...
我正在尝试保存一个类我的类对象的集合.我收到一个错误说明:...
我需要根据Silverlight中的某些配置值设置给定控件的Style.我...