WPF动画宽度和高度code后面(放大) [英] WPF Animate width and Height with code behind (Zoom in)

查看:767
本文介绍了WPF动画宽度和高度code后面(放大)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够动画边框的动作:

 私人无效通过MoveTo(边境目标,双下一页末,双newy指定)
{
    矢量偏移= VisualTreeHelper.GetOffset(目标);
    VAR顶部= offset.Y;
    VAR左= offset.X;
    TranslateTransform反=新TranslateTransform();
    target.RenderTransform =反;
    DoubleAnimation是anim1 =新DoubleAnimation是(0,newy指定 - 顶部,TimeSpan.FromMilliseconds(500));
    DoubleAnimation是anim2 =新DoubleAnimation是(0,下一页末 - 左,TimeSpan.FromMilliseconds(500));
    trans.BeginAnimation(TranslateTransform.YProperty,anim1);
    trans.BeginAnimation(TranslateTransform.XProperty,anim2);
}

不过,我想能够和动画的高度和宽度的增加作为位置给扩大(其中包含在边界在我的情况和上面的例子))图像的IM pression。

这是spossible与code后面?


OK,我试过尺度变换,但它似乎并没有做任何事情 - 做我需要一个故事板

 私人无效变焦(边界目标)
    {
        TranslateTransform反=新TranslateTransform();
        target.RenderTransform =反;
        DoubleAnimation是anim1 =新DoubleAnimation是(1,2,TimeSpan.FromMilliseconds(1000));
        DoubleAnimation是anim2 =新DoubleAnimation是(1,2,TimeSpan.FromMilliseconds(1000));
        trans.BeginAnimation(ScaleTransform.ScaleXProperty,anim1);
        trans.BeginAnimation(ScaleTransform.ScaleYProperty,anim2);
    }


解决方案

使用 ScaleTransform ,不需要高度 &安培; 宽度动画,在 ScaleTransform 会影响你的边框的的VisualTree 等等内部图像将被拉长为好。

 私人无效变焦(边界目标)
    {
        ScaleTransform反=新ScaleTransform();
        target.RenderTransform =反;
        //如果你使用X'放大器相同的动画; y您不需要anim1,anim2
        DoubleAnimation是阿尼姆=新DoubleAnimation是(1,2,TimeSpan.FromMilliseconds(1000));
        trans.BeginAnimation(ScaleTransform.ScaleXProperty,阿尼姆);
        trans.BeginAnimation(ScaleTransform.ScaleYProperty,阿尼姆);    }

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);
    }

解决方案

Use ScaleTransform, no need for Height & Width animations, the ScaleTransform will affect your Border's VisualTree so the inner image will be stretched as well.

    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);

    }

这篇关于WPF动画宽度和高度code后面(放大)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆