微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐