将对象进行缩放,除了可以利用它的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(); };