7

こんにちは、

ここ数週間、高度なメトロノームを作成するプロジェクトに取り組んできました。メトロノームは次のもので構成されています

  1. スイングアーム
  2. 軽い閃光
  3. ビートを表す動的に作成されたユーザー コントロールのコレクション (オン、アクセント付き、オフのいずれかの 4 つ)。
  4. LCD 数値ディスプレイを表示し、選択された BPM (60000/BPM=ミリ秒) のビート間のミリ秒数を計算するユーザーコントロール

ユーザーが BPM を選択してスタートを押すと、次のことが起こります

  1. アームは、掃引ごとに n ミリ秒の割合で 2 つの角度の間をスイングします。
  2. 各アームスイープの終わりにライトが点滅します
  3. インジケーターが作成され、順番に点滅します (各スイープの最後に 1 つ)。

腕と光のフラッシュ アニメーションがコードで作成され、ストーリー ボードに追加され、永遠に繰り返され、自動逆方向に繰り返される問題が解決されました。

インジケーターはコードで作成され、各アーム スイープ アニメーションの最後にイベントを発生させる必要があります。

そこで、いろいろいじった後、ストーリーボードと同じペースで実行されるタイマーを作成しました。

問題は、30 秒を超えるとタイマーとストーリーボードが同期しなくなり、インジケーターとアーム スイープが間に合わなくなることです (メトロノームには適していません!!)。

アニメーションの完了イベントをキャッチし、それをトリガーとしてタイマーを停止および再起動しようとしていましたが、2 つを完全に同期させるために思いついたのはこれだけでした。

同期のずれは、ストーリーボードのずれと、タイマーが .start で呼び出される前に行で begin でストーリーボードが呼び出されるという事実によって引き起こされます。時間。

私の質問は、アニメーションの完了イベントにバインドしようとすると、決して発生しません。私は、オートリバース (つまり、各反復の間に) に関係なく、均等な発射を完了したという印象を受けました。そうではありませんか?

2 つのことを同期させる別の (もっと狡猾な) 方法を考えられる人はいますか?

最後に、ストーリーボードからメソッドを起動できるかどうかを確認しました (これにより、作業が非常に楽になりますが、これは実行できないようです)。

何か提案があれば、私は貴重ではありません。これを完成させたいだけです!!

重要な最後のポイントとして、メトロノームの実行中に bpm を調整できます。これは、オンザフライ (ボタンをマウスで押す) でミリ秒の長さを計算し、現在の速度と新しい速度の差によってストーリーボードをスケーリングすることによって実現されます。明らかに、インジケーターを実行するタイマーは同時に変更する必要があります (間隔を使用)。

以下のコードは、これまでの私のプロジェクトからのものです (XAML ではなく、C# だけです)。

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Controls;
using System.Windows.Threading;    

namespace MetronomeLibrary
{    
    public partial class MetronomeLarge
    {
        private bool Running;

        //Speed and time signature
        private int _bpm = 60;
        private int _beats = 4;
        private int _beatUnit = 4;
        private int _currentBeat = 1;
        private readonly int _baseSpeed = 60000 / 60;
        private readonly DispatcherTimer BeatTimer = new DispatcherTimer();

        private Storyboard storyboard = new Storyboard();

        public MetronomeLarge()
        {
            InitializeComponent();

            NumericDisplay.Value = BPM;

            BeatTimer.Tick += new EventHandler(TimerTick);

            SetUpAnimation();    
            SetUpIndicators(); 
        }

        public int Beats
        {
            get
            {
                return _beats;
            }
            set
            {
                _beats = value;
                SetUpIndicators();
            }
        }

        public int BPM
        {
            get
            {
                return _bpm;
            }
            set
            {
                _bpm = value;
                //Scale the story board here
                SetSpeedRatio();
            }
        }

        public int BeatUnit
        {
            get
            {
                return _beatUnit;
            }
            set
            {
                _beatUnit = value;
            }
        }

        private void SetSpeedRatio()
        {
            //divide the new speed (bpm by the old speed to get the new ratio)
            float newMilliseconds = (60000 / BPM);
            float newRatio = _baseSpeed / newMilliseconds;
            storyboard.SetSpeedRatio(newRatio);

            //Set the beat timer according to the beattype (standard is quarter beats for one sweep of the metronome
            BeatTimer.Interval = TimeSpan.FromMilliseconds(newMilliseconds);
        }

        private void TimerTick(object sender, EventArgs e)
        {
            MetronomeBeat(_currentBeat);

            _currentBeat++;

            if (_currentBeat > Beats)
            {
                _currentBeat = 1;
            }
        }

        private void MetronomeBeat(int Beat)
        {
                //turnoff all indicators
                TurnOffAllIndicators();

                //Find a control by name
                MetronomeLargeIndicator theIndicator = (MetronomeLargeIndicator)gridContainer.Children[Beat-1];

                //illuminate the control
                theIndicator.TurnOn();
                theIndicator.PlaySound();    
        }

        private void TurnOffAllIndicators()
        {

            for (int i = 0; i <= gridContainer.Children.Count-1; i++)
            {
                MetronomeLargeIndicator theIndicator = (MetronomeLargeIndicator)gridContainer.Children[i];
                theIndicator.TurnOff();
            }
        }

        private void SetUpIndicators()
        {
            gridContainer.Children.Clear();
            gridContainer.ColumnDefinitions.Clear();

            for (int i = 1; i <= _beats; i++)
            {
                MetronomeLargeIndicator theNewIndicator = new MetronomeLargeIndicator();

                ColumnDefinition newCol = new ColumnDefinition() { Width = GridLength.Auto };
                gridContainer.ColumnDefinitions.Add(newCol);
                gridContainer.Children.Add(theNewIndicator);
                theNewIndicator.Name = "Indicator" + i.ToString();
                Grid.SetColumn(theNewIndicator, i - 1);
            }
        }   

        private void DisplayOverlay_MouseDown(object sender, MouseButtonEventArgs e)
        {
            ToggleAnimation();
        }

        private void ToggleAnimation()
        {
            if (Running)
            {
                //stop the animation
                ((Storyboard)Resources["Storyboard"]).Stop() ;
                BeatTimer.Stop();
            }
            else
            {
                //start the animation
                BeatTimer.Start();
                ((Storyboard)Resources["Storyboard"]).Begin();
                SetSpeedRatio();                 
            }

            Running = !Running;
        }


        private void ButtonIncrement_Click(object sender, RoutedEventArgs e)
        {
            NumericDisplay.Value++;
            BPM = NumericDisplay.Value;
        }

        private void ButtonDecrement_Click(object sender, RoutedEventArgs e)
        {
            NumericDisplay.Value--;
            BPM = NumericDisplay.Value;
        }

        private void ButtonIncrement_MouseEnter(object sender, MouseEventArgs e)
        {
            ImageBrush theBrush = new ImageBrush() 
            { 
                ImageSource = new BitmapImage(new 
                    Uri(@"pack://application:,,,/MetronomeLibrary;component/Images/pad-metronome-increment-button-over.png")) 
            };
            ButtonIncrement.Background = theBrush;
        }

        private void ButtonIncrement_MouseLeave(object sender, MouseEventArgs e)
        {
            ImageBrush theBrush = new ImageBrush() 
            { 
                ImageSource = new BitmapImage(new 
                    Uri(@"pack://application:,,,/MetronomeLibrary;component/Images/pad-metronome-increment-button.png")) 
            };
            ButtonIncrement.Background = theBrush;
        }

        private void ButtonDecrement_MouseEnter(object sender, MouseEventArgs e)
        {
            ImageBrush theBrush = new ImageBrush() 
            { 
                ImageSource = new BitmapImage(new 
                    Uri(@"pack://application:,,,/MetronomeLibrary;component/Images/pad-metronome-decrement-button-over.png")) 
            };
            ButtonDecrement.Background = theBrush;
        }

        private void ButtonDecrement_MouseLeave(object sender, MouseEventArgs e)
        {
            ImageBrush theBrush = new ImageBrush() 
            { 
                ImageSource = new BitmapImage(new 
                    Uri(@"pack://application:,,,/MetronomeLibrary;component/Images/pad-metronome-decrement-button.png")) 
            };
            ButtonDecrement.Background = theBrush;
        }

        private void SweepComplete(object sender, EventArgs e)
        {
            BeatTimer.Stop();
            BeatTimer.Start();
        }

        private void SetUpAnimation()
        {
            NameScope.SetNameScope(this, new NameScope());
            RegisterName(Arm.Name, Arm);

            DoubleAnimation animationRotation = new DoubleAnimation()
            {
                From = -17,
                To = 17,
                Duration = new Duration(TimeSpan.FromMilliseconds(NumericDisplay.Milliseconds)),
                RepeatBehavior = RepeatBehavior.Forever,
                AccelerationRatio = 0.3,
                DecelerationRatio = 0.3,
                AutoReverse = true,                 
            };

            Timeline.SetDesiredFrameRate(animationRotation, 90);

            MetronomeFlash.Opacity = 0;

            DoubleAnimation opacityAnimation = new DoubleAnimation()
            {
                From = 1.0,
                To = 0.0,
                AccelerationRatio = 1,
                BeginTime = TimeSpan.FromMilliseconds(NumericDisplay.Milliseconds - 0.5),
                Duration = new Duration(TimeSpan.FromMilliseconds(100)),
            };

            Timeline.SetDesiredFrameRate(opacityAnimation, 10);

            storyboard.Duration = new Duration(TimeSpan.FromMilliseconds(NumericDisplay.Milliseconds * 2));
            storyboard.RepeatBehavior = RepeatBehavior.Forever;
            Storyboard.SetTarget(animationRotation, Arm);
            Storyboard.SetTargetProperty(animationRotation, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));
            Storyboard.SetTarget(opacityAnimation, MetronomeFlash);
            Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("Opacity"));    
            storyboard.Children.Add(animationRotation);
            storyboard.Children.Add(opacityAnimation);

            Resources.Add("Storyboard", storyboard);    
        }
    }
}
4

4 に答える 4

2

これは、WPF アニメーションでは簡単に実装できない場合があります。代わりに、良い方法はゲーム ループです。ちょっとした調査で、これに関する多くのリソースが見つかるはずです。最初に思いついたのはhttp://www.nuclex.org/articles/3-basics/5-how-a-game-loop-worksでした。

ゲーム ループでは、次の基本手順のいずれかに従います。

  • 最後のフレームからの経過時間を計算します。
  • ディスプレイを適切に移動します。

また

  • 現在時刻を計算します。
  • ディスプレイを適切に配置します。

ゲーム ループの利点は、(使用するタイミングの種類に応じて) タイミングがわずかにずれることがありますが、すべての表示が同じ量だけずれることです。

実用上ドリフトしないシステムクロックで時刻を計算することにより、クロックのドリフトを防ぐことができます。タイマーは、システム クロックによって実行されないため、ドリフトします。

于 2012-07-07T15:03:06.933 に答える
1

時刻同期は、想像以上に広い分野です。

スケジューリング/タイマーの問題で有名なQuartz.NETをご覧になることをお勧めします。

ストーリーボードは論理ツリーの一部ではないため、WPF アニメーションの同期はトリッキーです。したがって、ストーリーボードには何もバインドできません。
そのため、動的/変数のストーリーボードを XAML で定義することはできません。C# で行う必要があります。

2 つのストーリーボードを作成することをお勧めします。1 つは左側の目盛り用、もう 1 つは右側です。
各アニメーションの間に、計算を実行する/UI の別の部分を更新するメソッドをTask起動しますが、タイミングが台無しにならないように個別に実行します (計算の数マイクロ秒は、30 秒後にかなりの時間を補います)。UI を更新するには、から
を使用する必要があることに注意してください。Application.Current.DispatcherTask

最後に、少なくともタスクが開始された順序で実行されるようにTaskフラグを設定します。 これは にヒントを与えるだけで、それらが順番に実行されることを保証するものではないため、完全な保証のために代わりにキューイング システムを使用することをお勧めします。TaskCreationOptions.PreferFairness
TaskScheduler

HTH、

バブ。

于 2012-07-06T13:31:14.847 に答える
0

タイマーをアニメーションと同期させるのは難しいと思います-メッセージに基づくディスパッチャーベースのタイマーです-少し時間がスキップされることがあります。つまり、マウスで速くクリックすると、アニメーションタイマーもはディスパッチャベースであるため、簡単に同期がとれなくなります。

同期を中止して、タイマーに処理させることをお勧めします。通知でプロパティを更新し、メトロノームアームの位置をそれにバインドさせることはできませんか?加速/減速を取得するには、正弦関数または余弦関数を使用する必要があります。

于 2012-07-06T13:14:30.730 に答える
0

右スイング用と左スイング用の 2 つのアニメーションを試すことができます。それぞれのアニメーションが完了したら、他のアニメーションを開始し (キャンセル フラグを確認します)、インジケーターを更新します (次のアニメーションの開始を妨げないように、Dispatcher の BeginInvoke を介して可能性があります)。

于 2012-07-02T18:24:17.880 に答える