机械手臂控制动画
前言
同过已有动画,将动画通过代码提取他的帧序列图片, 利用关键帧的播放实现对工程操作流程的控制 。二、使用步骤
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;
}
}
}