WPF 动画宽度和高度,代码在后面(放大)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12551823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Animate width and Height with code behind (Zoom in)
提问by Robben_Ford_Fan_boy
I am able to animate the movement of a Border:
我能够为边界的运动设置动画:
private void MoveTo(Border target, double newX, double newY)
{
Vector offset = VisualTreeHelper.GetOffset(target);
var top = offset.Y;
var left = offset.X;
TranslateTransform trans = new TranslateTransform();
target.RenderTransform = trans;
DoubleAnimation anim1 = new DoubleAnimation(0, newY - top, TimeSpan.FromMilliseconds(500));
DoubleAnimation anim2 = new DoubleAnimation(0, newX - left, TimeSpan.FromMilliseconds(500));
trans.BeginAnimation(TranslateTransform.YProperty, anim1);
trans.BeginAnimation(TranslateTransform.XProperty, anim2);
}
But I would like to be able to animate an increase in Height and Width as well as position to give the impression of enlarging an image(which is contained in a Border in my case and example above)).
但是我希望能够对高度和宽度以及位置的增加进行动画处理,以给人一种放大图像的印象(在我的案例和上面的示例中,它包含在边框中))。
Is this spossible with code behind?
后面的代码可以这样做吗?
OK, I tried the scale transform but it does not appear to do anything - do I need a storyboard?
好的,我尝试了比例变换,但它似乎没有做任何事情 - 我需要故事板吗?
private void Zoom(Border target)
{
TranslateTransform trans = new TranslateTransform();
target.RenderTransform = trans;
DoubleAnimation anim1 = new DoubleAnimation(1, 2, TimeSpan.FromMilliseconds(1000));
DoubleAnimation anim2 = new DoubleAnimation(1, 2, TimeSpan.FromMilliseconds(1000));
trans.BeginAnimation(ScaleTransform.ScaleXProperty, anim1);
trans.BeginAnimation(ScaleTransform.ScaleYProperty, anim2);
}
采纳答案by S3ddi9
Use ScaleTransform, no need for Height& Widthanimations, the ScaleTransformwill affect your Border's VisualTreeso the inner image will be stretched as well.
使用ScaleTransform,不需要Height&Width动画,这ScaleTransform将影响您的边框,VisualTree因此内部图像也会被拉伸。
private void Zoom(Border target)
{
ScaleTransform trans = new ScaleTransform();
target.RenderTransform = trans;
// if you use the same animation for X & Y you don't need anim1, anim2
DoubleAnimation anim = new DoubleAnimation(1, 2, TimeSpan.FromMilliseconds(1000));
trans.BeginAnimation(ScaleTransform.ScaleXProperty, anim);
trans.BeginAnimation(ScaleTransform.ScaleYProperty, anim);
}
回答by Sebastian Dymel
It is better to use scale transform for zooming, but if you insist on animating W/H you can do it, since these are normal DP you can animate them using standard DoubleAnimation/DoubleAnimationUsingKeyFrames Something like
最好使用缩放变换进行缩放,但是如果您坚持为 W/H 设置动画,则可以这样做,因为这些是普通的 DP,您可以使用标准的 DoubleAnimation/DoubleAnimationUsingKeyFrames 为它们设置动画
DoubleAnimation doubleAnimation = new DoubleAnimation(100, 200, new Duration(TimeSpan.FromMilliseconds(500)));
this.BeginAnimation(FrameworkElement.WidthProperty, doubleAnimation);

