机械手臂控制动画

该博客介绍了如何通过提取动画帧序列图片,利用关键帧播放技术,实现机械手臂操作流程的控制。在前端代码中,利用WPF的Image控件和Storyboard进行图片切换,模拟动画效果。在后台代码中,定义了不同按钮触发不同阶段的动画,如取片、放入反应腔等,实现了机械手臂的全程操作流程动画展示。
摘要由CSDN通过智能技术生成

机械手臂控制动画


前言

同过已有动画,将动画通过代码提取他的帧序列图片, 利用关键帧的播放实现对工程操作流程的控制 。

二、使用步骤

1.I前台代码

代码如下(示例):

<Window x:Class="MechanicalArm.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        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"
        xmlns:local="clr-namespace:MechanicalArm"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Image x:Name="picture1" Stretch="Uniform" Source="C:\Users\lf\Desktop\MechanicalArm\MechanicalArm\image\1.jpg" Visibility="Visible" Width="500" Margin="146,0"/>
        <Image x:Name="picture2" Stretch="Uniform" Source="C:\Users\lf\Desktop\MechanicalArm\MechanicalArm\image\36.jpg" Visibility="Collapsed"  Height="450" Width="500"></Image>
        <Rectangle Name="RectDis" Stretch="Uniform" Width="500" Margin="146,0" Visibility="Collapsed"/>
        <Button Content="取片" HorizontalAlignment="Left" Margin="44,49,0,0" VerticalAlignment="Top" Width="75" Click="GetPiece_Button" Height="25"/>
        <Button Content="反应操作流程流程" HorizontalAlignment="Left" Margin="44,183,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="0.507,-0.228" Click="SetPiece_Button"/>
        <Button Content="放入反应腔" HorizontalAlignment="Left" Margin="44,112,0,0" VerticalAlignment="Top" Width="75" Click="FlowPath_Button"/>
        <Button Content="归位" HorizontalAlignment="Left" Margin="44,268,0,0" VerticalAlignment="Top" Width="75" Click="Home_Button"/>
    </Grid>
</Window>

2.后台代码

代码如下(示例):

using System;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;

namespace MechanicalArm
{

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 对图片序列帧进行处理实现动画播放
        /// </summary>
        /// <param name="a">序列帧的起始值</param>
        /// <param name="n">序列帧的结束值</param>
        public void DrawPricture(int a, int n)
        {
            Storyboard _sbDisappear = new Storyboard();
            string fileName = AppDomain.CurrentDomain.BaseDirectory;
            string path1 = new DirectoryInfo("../..").FullName;                                     //当前应用程序路径的上级目录
            string path2 = "image";
            string newPath = Path.Combine(path1, path2);                                            //链接字符串获取图片所在目录
            fileName = newPath;
            for (int i = a; i < n; i++)
            {
                newPath = fileName + "\\" + i.ToString() + ".jpg";
                if (System.IO.File.Exists(newPath))
                {
                    ObjectAnimationUsingKeyFrames oauf = new ObjectAnimationUsingKeyFrames();             //ObjectAnimationUsingKeyFrames 可以对指定 Duration 内的一组 KeyFrames 中的 Object 属性值进行动画处理
                    ImageBrush imgBrush = new ImageBrush();
                    BitmapImage bmpImge = new BitmapImage(new Uri(newPath));
                    imgBrush.ImageSource = bmpImge;                                                       //读取图片文件
                    DiscreteObjectKeyFrame dokf = new DiscreteObjectKeyFrame();                           //DiscreteObjectKeyFrame 通过使用离散内插,可以在前一个关键帧的 Object 值及其自己的 Value 之间进行动画处理。                
                    dokf.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(i * 40));               
                                                                                                         
                    dokf.Value = imgBrush;
                    oauf.KeyFrames.Add(dokf);
                    Storyboard.SetTargetProperty(oauf, new PropertyPath("(Rectangle.Fill)"));             //把动画应用到窗体的Rectangle中
                    Storyboard.SetTarget(oauf, RectDis);                                                  
                    _sbDisappear.Children.Add(oauf);
                    _sbDisappear.Begin();                                                                 
                }
            }
        }

        private void GetPiece_Button(object sender, RoutedEventArgs e)               //取片按钮
        {
            RectDis.Visibility = Visibility.Visible;
            DrawPricture(1, 8);                                                     //读取第一到第八章图片序列帧     
        }

        private void FlowPath_Button(object sender, RoutedEventArgs e)              //放入反应腔按钮
        {
            RectDis.Visibility = Visibility.Visible;
            DrawPricture(8, 17);
        }

        private void SetPiece_Button(object sender, RoutedEventArgs e)              //机械手臂全程动画流程动画
        {
            RectDis.Visibility = Visibility.Visible;
            DrawPricture(1, 79);                                                    //读取第一到第七十八章图片序列帧  
        }

        private void Home_Button(object sender, RoutedEventArgs e)                  //归位
        {
            RectDis.Visibility = Visibility.Collapsed;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值