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, targetElement);
                Storyboard.SetTargetProperty(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, targetElement);
                Storyboard.SetTargetProperty(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, targetElement);
                Storyboard.SetTargetProperty(doubleAnimation,
                    new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));
                storyboard.Children.Add(doubleAnimation);
                //启动动画
                storyboard.Begin();
            };

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值