FrameRates.axaml代码

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Height="410.4" Width="405.6"
        x:Class="AvaloniaUI.FrameRates"
        Title="FrameRates">
    <Grid Background="LightGoldenrodYellow" RowDefinitions="*,auto,auto">
        <Border Background="White" BorderBrush="DarkGray" BorderThickness="3"
               Width="300" Height="300"
               HorizontalAlignment="Center" VerticalAlignment="Center">
            <Canvas ClipToBounds="True">
                <Ellipse x:Name="ellipse" Fill="Red" Width="10" Height="10"/>
            </Canvas>
        </Border>

        <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center" Margin="10">
            <TextBlock VerticalAlignment="Center">Desired Frame Rate:</TextBlock>
            <TextBox x:Name="txtFrameRate" Width="50" Text="60"/>
        </StackPanel>

        <Button Grid.Row="2" HorizontalAlignment="Center" Padding="3" Margin="3"
                Content="Repeat"
                Click="OnRepeatClick"/>
    </Grid>
</Window>

FrameRates.axaml.cs代码

using Avalonia;
using Avalonia.Animation;
using Avalonia.Animation.Easings;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Styling;
using System;

namespace AvaloniaUI;

public partial class FrameRates : Window
{
    private Animation moveXY;
    public FrameRates()
    {
        InitializeComponent();
        moveXY = new Animation
        {
            Duration = TimeSpan.FromSeconds(2.5),
            Children =
            {
                new KeyFrame()
                {
                    Cue = new Cue(0d),
                    Setters =
                    {
                        new Setter(Canvas.LeftProperty, 0d),
                        new Setter(Canvas.TopProperty, 300d) // 起点位置
                    }
                },
                new KeyFrame
                {
                    Cue = new Cue(0.5d),
                    Setters =
                    {
                        new Setter(Canvas.LeftProperty, 150d),
                        new Setter(Canvas.TopProperty, 0d) // 抛物线顶点
                    }
                },
                new KeyFrame
                {
                    Cue = new Cue(1d),
                    Setters =
                    {
                        new Setter(Canvas.LeftProperty, 300d),
                        new Setter(Canvas.TopProperty, 300d) // 终点位置
                    }
                }
            }
        };
    }

    private void OnRepeatClick(object? sender, RoutedEventArgs e)
    {
        if (!int.TryParse(txtFrameRate.Text, out var fps))
            fps = 60;
        var iterationXY = (ulong)(moveXY.Duration.TotalSeconds * fps);
        moveXY.IterationCount = new IterationCount(iterationXY);
        moveXY.RunAsync(ellipse);
    }
}

运行效果

image

 

posted on 2025-09-27 11:51  dalgleish  阅读(15)  评论(0)    收藏  举报