TranslateTransform3D tt3d = new TranslateTransform3D(new Vector3D(0, 0, 0)); DoubleAnimation da = new DoubleAnimation(-4, new Duration(TimeSpan.FromSeconds(1))); tt3d.BeginAnimation(TranslateTransform3D.OffsetYProperty, da); 我们使用一个0矢量创建一个平移。动画在经过一段时间后会改变平移;所以,我们不需要把该矢量设置为其它非0值。实际动画使用一个值和一个时限(TimeSpan)值。在这种情况中,我想每秒移动-4单位。BeginAnimation调用指示,我想沿哪个轴移动这-4单位-在此是沿着Y轴。实际实现的是,把字母往下移动4个单位,这将耗费一秒钟。
double oldX = double.Parse(str[1]); double newX = (_CurrGuess.Length + 1) * -2.5; TranslateTransform3D tt3d2 = new TranslateTransform3D(new Vector3D(0, 0, 0)); da = new DoubleAnimation(newX - oldX, new Duration(TimeSpan.FromSeconds(1))); tt3d2.BeginAnimation(TranslateTransform3D.OffsetXProperty, da); 这里的执行非常相似于上面情况,只是要确定要移动多少单位和沿什么轴移动。因此,让我们继续讨论更令人感爱好的旋转变换:
RotateTransform3D myRotateTransform = new RotateTransform3D( new AxisAngleRotation3D(new Vector3D(1, 0, 0), 1)); DoubleAnimation myAnimation = new DoubleAnimation(); myAnimation.From = 0; myAnimation.To = 360; myAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(1000)); myAnimation.RepeatBehavior = new RepeatBehavior(1); myRotateTransform.Rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, myAnimation); 我们沿一个轴实现旋转。上面的矢量指示,它位于X轴上。AxisAngleRotation3D构造函数的第二个参数是一个角度值(为1)。该动画每次运动1度,这很轻易理解。我使动画从第0帧(相应于0度)移动到第360帧(相应于360度)。我还想使其它一切都在一秒内完成。
现在,最后一步是把所有变换施加到hitgeo对象:
(hitgeo.Transform as Transform3DGroup).Children.Insert(1, myRotateTransform); (hitgeo.Transform as Transform3DGroup).Children.Add(tt3d); (hitgeo.Transform as Transform3DGroup).Children.Add(tt3d2); 代码看上去有点希奇,但是为了实现多种变换,你必须把字母的Transform属性赋值为一个Transform3DGroup(其实,这是相应于一组变换)。我是在创建字母时实现这一点的,我想保留对字母施加的原始变换并且再添加上一些新的变换。
FormattedText ft = new FormattedText(text, new CultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface(new FontFamily("Arial"), FontStyles.Normal, FontWeights.Normal, new FontStretch()), 24D, Brushes.Black); 注重,这里的text变量中存储着我想编写的文本。我选择使用黑色Arial,24pt字体。这基本上确定了一个文本框,并且能够相应于文本大小来调整该文本框大小。我可以使用这个尺寸来决定我想如何缩放我的按钮的尺寸。你会在ButtonFactory的实现代码中看到类似如下的内容: