2017/07/13

[C#] WPF Transform Animation

針對一些WPF的物件會常用到的變形寫法
使用的套件是using System.Windows.Media;



source code:
        
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;

namespace TransformSample
{
    public partial class MainWindow : Window
    {
        private int frame_count = 0;
        private double fps = 30.0;

        private DispatcherTimer timer_main;

        public MainWindow()
        {
            InitializeComponent();

            timer_main = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1.0 / fps) };
            timer_main.Tick += TimerMainTick;
            timer_main.Start();
            
        }

        private void TimerMainTick(object sender, EventArgs e)
        {
            frame_count++;
            label_count.Content = frame_count;

            TranslateObjectFunc(image_translate, frame_count);
            ScaleObjectFunc(image_sacle, frame_count);
            RotateObjectFunc(image_rotate, frame_count);
        }

        private void TranslateObjectFunc(Image image, double parament)
        {
            parament = 5.0 * parament % 50.0;
            TranslateTransform tt = new TranslateTransform(0, parament);
            image.RenderTransform = tt;
        }

        private void ScaleObjectFunc(Image image, double parament)
        {
            double ratio = 20.0;
            parament = (parament % ratio) / ratio;
            ScaleTransform st = new ScaleTransform(parament, parament);
            image.RenderTransformOrigin = new Point(0.5, 0.5); // object center
            image.RenderTransform = st;
        }

        private void RotateObjectFunc(Image image, double parament)
        {
            parament = parament % 360;
            RotateTransform rt = new RotateTransform(parament);
            image.RenderTransformOrigin = new Point(0.5, 0.5); // object center
            image.RenderTransform = rt;
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Escape)
            {
                Application.Current.Shutdown();
            }
        }
    }
}

end

沒有留言:

張貼留言