wpf – 当左边距变为负时,动画减慢

前端之家收集整理的这篇文章主要介绍了wpf – 当左边距变为负时,动画减慢前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图让一个多边形从屏幕左侧完整的移动到屏幕上,然后完全从屏幕右侧移开,然后再次返回.

我已经得到了这个工作.但是,由于某种原因,一旦左边距变为负值,动画就会突然减慢.一旦左边距变为正值,它将再次加速.

为什么会发生这种情况?我该如何阻止?

以下是完整的代码

  1. <Window x:Class="Geometry.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow" Height="350" Width="525">
  5. <Window.Resources>
  6. <PathGeometry x:Key="MyGeometry">
  7. <PathGeometry.Figures>
  8. <PathFigure>
  9. <PathFigure.Segments>
  10. <LineSegment Point="0.30,0" />
  11. <LineSegment Point="0.70,1" />
  12. <LineSegment Point="0.40,1" />
  13. <LineSegment Point="0,0" />
  14. </PathFigure.Segments>
  15. </PathFigure>
  16. </PathGeometry.Figures>
  17. </PathGeometry>
  18. <Storyboard x:Key="MovingAnimation">
  19. <ThicknessAnimationUsingKeyFrames RepeatBehavior="1:0:0" FillBehavior="HoldEnd" Storyboard.TargetName="_path" Storyboard.TargetProperty="Margin" >
  20. <DiscreteThicknessKeyFrame KeyTime="0:0:0" Value="-2.0,0" />
  21. <LinearThicknessKeyFrame KeyTime="0:0:10" Value="1.0,0" />
  22. <LinearThicknessKeyFrame KeyTime="0:0:20" Value="-2.0,0" />
  23. </ThicknessAnimationUsingKeyFrames>
  24. </Storyboard>
  25. </Window.Resources>
  26. <Window.Triggers>
  27. <EventTrigger RoutedEvent="Window.Loaded">
  28. <BeginStoryboard Storyboard="{StaticResource MovingAnimation}" ></BeginStoryboard>
  29. </EventTrigger>
  30. </Window.Triggers>
  31. <Grid>
  32. <Grid.RowDefinitions>
  33. <RowDefinition Height="Auto" />
  34. <RowDefinition Height="*" />
  35. </Grid.RowDefinitions>
  36. <StackPanel Grid.Row="0" Orientation="Horizontal">
  37. <Label>Margin:</Label>
  38. <TextBlock Text="{Binding ElementName=_path,Path=Margin.Left,StringFormat={}{0:0.#}}" />
  39. </StackPanel>
  40. <Canvas Name="_canvas" Grid.Row="1">
  41. <Border Margin="0" Width="1" Height="1" VerticalAlignment="Center" HorizontalAlignment="Center">
  42. <Border.RenderTransform>
  43. <ScaleTransform
  44. ScaleX="{Binding ElementName=_canvas,Path=ActualWidth}"
  45. ScaleY="{Binding ElementName=_canvas,Path=ActualHeight}"
  46. CenterX="0"
  47. CenterY="0">
  48. </ScaleTransform>
  49. </Border.RenderTransform>
  50. <Path
  51. Name="_path"
  52. Fill="#CCCCFF"
  53. Data="{StaticResource MyGeometry}"
  54. Width="1.0"
  55. Height="1.0"
  56. >
  57. </Path>
  58. </Border>
  59. </Canvas>
  60. </Grid>
  61. </Window>

解决方法

动画保证金属性将触发额外的度量/安排通行证,从而导致更多的性能影响(尽管在这个例子中可能不明显).另一方面,“仅渲染”属性的动画将不会触发布局重新布置,因此更具性能友好.

请,看看有点简单的方法做什么,我想,你想得到:

  1. <Window x:Class="Geometry.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="MainWindow" Height="518" Width="530">
  5. <Window.Resources>
  6. <PathGeometry x:Key="MyGeometry">
  7. <PathGeometry.Figures>
  8. <PathFigure>
  9. <PathFigure.Segments>
  10. <LineSegment Point="0.30,0" />
  11. <LineSegment Point="0.70,1" />
  12. <LineSegment Point="0.40,1" />
  13. <LineSegment Point="0,0" />
  14. </PathFigure.Segments>
  15. </PathFigure>
  16. </PathGeometry.Figures>
  17. </PathGeometry>
  18. <Storyboard x:Key="MovingAnimation">
  19. <DoubleAnimationUsingKeyFrames RepeatBehavior="1:0:0" FillBehavior="HoldEnd" Storyboard.TargetName="_scaleTransform" Storyboard.TargetProperty="CenterX" >
  20. <LinearDoubleKeyFrame KeyTime="0:0:0" Value="1.2" />
  21. <LinearDoubleKeyFrame KeyTime="0:0:10" Value="-0.5" />
  22. <LinearDoubleKeyFrame KeyTime="0:0:20" Value="1.2" />
  23. </DoubleAnimationUsingKeyFrames>
  24. </Storyboard>
  25. </Window.Resources>
  26. <Window.Triggers>
  27. <EventTrigger RoutedEvent="Window.Loaded">
  28. <BeginStoryboard Storyboard="{StaticResource MovingAnimation}" ></BeginStoryboard>
  29. </EventTrigger>
  30. </Window.Triggers>
  31. <Grid>
  32. <Grid.RowDefinitions>
  33. <RowDefinition Height="Auto" />
  34. <RowDefinition Height="*" />
  35. </Grid.RowDefinitions>
  36. <StackPanel Grid.Row="0" Orientation="Horizontal">
  37. <Label>Margin:</Label>
  38. <TextBlock Text="{Binding ElementName=_scaleTransform,Path=CenterX,StringFormat={}{0:0.#}}" VerticalAlignment="Center" />
  39. </StackPanel>
  40. <!--
  41. <Border Grid.Row="1" Margin="150" BorderBrush="Red" BorderThickness="1">
  42. -->
  43. <Grid Name="_canvas" Grid.Row="1">
  44. <Path Name="_path" Fill="#CCCCFF" Data="{StaticResource MyGeometry}"
  45. Width="1.0"
  46. Height="1.0"
  47. HorizontalAlignment="Center"
  48. VerticalAlignment="Center">
  49. <Path.RenderTransform>
  50. <ScaleTransform x:Name="_scaleTransform"
  51. ScaleX="{Binding ElementName=_canvas,Path=ActualHeight}"
  52. CenterX="1.2"
  53. CenterY="0.5">
  54. </ScaleTransform>
  55. </Path.RenderTransform>
  56. </Path>
  57. </Grid>
  58. <!--
  59. </Border>
  60. -->
  61. </Grid>
  62. </Window>

猜你在找的CSS相关文章