流体网格高度动画UWP

时间:2019-06-01 20:41:39

标签: animation uwp

我必须创建一个网格高度的动画(这使用麦克风的db数据,我已经模拟了随机的数据接收)。但是我创建的动画一点也不流畅。如何获得动画来制作一个示例,例如带有四个彩色条(我的意思是流畅的动画)的Google Assistant(Google Play应用)?

MainPage.xaml:

<Grid>
    <Grid x:Name="ColorGrid" Background="Blue" Height="150" Width="40" CornerRadius="20"/>
</Grid>

MainPage.xaml.cs:

DispatcherTimer TimerHeight = new DispatcherTimer();
Storyboard storyboard1 = new Storyboard();
double AnimationTime = 50;

public MainPage()
{
    this.InitializeComponent();
    TimerHeight.Interval = TimeSpan.FromMilliseconds(50);
    TimerHeight.Tick += TimerHeight_Tick;
    TimerHeight.Start();
}

private void TimerHeight_Tick(object sender, object e)
{
    double ActualHeight = GetRandomNumber(150,350);
    //ColorGrid.Height = ActualHeight;
    StartAnimation(ColorGrid, ActualHeight);
}

public double GetRandomNumber(double minimum, double maximum)
{
    Random random = new Random();
    return random.NextDouble() * (maximum - minimum) + minimum;
}

private void StartAnimation(Grid GridColor, double GridHight)
{
    storyboard1 = new Storyboard();
    var AnimationOne = new EasingDoubleKeyFrame() { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(AnimationTime)), Value = GridHight, EasingFunction = new QuarticEase() { EasingMode = EasingMode.EaseOut } };
    var AnimOne = new DoubleAnimationUsingKeyFrames();
    AnimOne.EnableDependentAnimation = true;
    AnimOne.KeyFrames.Add(AnimationOne);
    Storyboard.SetTargetProperty(AnimOne, "(FrameworkElement.Height)");
    Storyboard.SetTarget(AnimOne, GridColor);
    storyboard1.Children.Add(AnimOne);
    storyboard1.Begin();
}

尽管我使用了动画,但好像有镜头。如何获得流畅的动画?

谢谢。

2 个答案:

答案 0 :(得分:0)

您好,欢迎来到我们美丽的社区!

首先,您是否尝试在没有所有Visual Studio的调试系统的情况下运行该应用程序? 因为它很重,可能会导致动画不稳定,所以我有时也会遇到这种情况。 因此,首先要做的就是部署您的应用程序,然后只需单击它就可以运行它,就像从商店中安装了它一样。

第二...您不是要在这么少的毫秒内为太多动画制作动画吗? 尝试将TimeSpan设置得更长一些(例如1000),然后看看会发生什么...

答案 1 :(得分:0)

  

尽管我使用了动画,但好像有镜头。如何获得流畅的动画?

如果要平滑制作动画,可以将AnimationTimeKeyTime)设置更大。

对于我的测试,如果将AnimationTime设置为600,则动画将非常平滑。

private void StartAnimation(Grid GridColor, double GridHight)
{
    storyboard1 = new Storyboard();
    var AnimationOne = new EasingDoubleKeyFrame() {
        KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(600)),
        Value = GridHight,
        EasingFunction = new QuarticEase() { EasingMode = EasingMode.EaseOut } };
    var AnimOne = new DoubleAnimationUsingKeyFrames();
    AnimOne.EnableDependentAnimation = true;
    AnimOne.KeyFrames.Add(AnimationOne);
    Storyboard.SetTargetProperty(AnimOne, "(FrameworkElement.Height)");
    Storyboard.SetTarget(AnimOne, GridColor);
    storyboard1.Children.Add(AnimOne);
    storyboard1.Begin();
}