开发者

Improve animation smoothness (moving of controls)

开发者 https://www.devze.com 2023-02-25 03:58 出处:网络
I have implemented the animation of moving of a grid control in the following manner: <Grid HorizontalAlignment=\"Center\"

I have implemented the animation of moving of a grid control in the following manner:

<Grid 
    HorizontalAlignment="Center" 
    VerticalAlignment="Center">
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
  </Grid.RowDefinitions>
  <Grid.Style>
    <Style TargetType="Grid">
      <Style.Triggers>
        <DataTrigger 
            Binding="{Binding ElementName=rootLayout, Path=IsVisible}" 
            Value="True">
          <DataTrigger.EnterActions>
            <BeginStoryboard>
              <Storyboard>
                <ThicknessAnimation 
                    Storyboard.TargetProperty="Margin"                      
                    From="-500,0,0,0"
                    To="0,0,0,0" 
                    Duration="0:0:0.5" />
              </Storyboard>
            </BeginStoryboard>
          </DataTrigger.EnterActions>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </Grid.Style>

  <Border 
      Grid.RowSpan="2" 
      Background="Black"
      CornerRadius="6" >          
    <Border.Effect>
      <DropShadowEffect />
    </Border.Effect>
  </Border>

  <TextBlock 
      Grid.Row="0" 
      Width="400" 
      Height="200" 
      Margin="20,20,20,10" 
      Text="{Binding Path=MessageText}" />

  <Button 
      Grid.Row="1" 
      Margin="20,5,20,15" 
      HorizontalAlignment="Right" 
      Width="75" 
      Content="OK" 
      Command="{Binding Path=CloseDialogCommand}" />

</G开发者_运维技巧rid>

The animation works fine, but it's ugly. It is shaky / jittery / jerky and it really looks unprofessional. Is there a way to improve this? Am I using the right approach with animating the value change on the Margin property in order to move the grid? I've read about RenderTransform, but I don't know how to use it in my case.

Also, the animation looks unnatural. I know this can be improved but I don't know how. What are these properties and can they help me enhance my animation:

  • AccelerationRatio
  • DecelerationRatio
  • EasingFunction
  • IsAdditive
  • IsCumulative
  • SpeedRatio

Thanks for helping!

P.S. I am trying to put as much code as possible in XAML, so I'd prefer that approach, but really, if there's anything to improve this...


Use easing functions, a simple DoubleAnimation and RenderTransform, e.g.

<Button Content="Test">
    <Button.RenderTransform>
        <TranslateTransform/>
    </Button.RenderTransform>
    <Button.Triggers>
        <EventTrigger RoutedEvent="Button.Click">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Storyboard.TargetProperty="RenderTransform.X"
                                     From="-500" To="0">
                        <DoubleAnimation.EasingFunction>
                            <CubicEase EasingMode="EaseOut"/>
                        </DoubleAnimation.EasingFunction>
                    </DoubleAnimation>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Button.Triggers>
</Button>

This should be quite smooth, check out this overview on easing functions to get an idea of how they affect the animation.

Also note that the duration has a strong effect on what an animation looks like, depending on what easing function you use setting high duration values is needed because they slow down significantly at the end.

0

精彩评论

暂无评论...
验证码 换一张
取 消