简体   繁体   English

如何逐个像素地移动图像?

[英]How to move an image pixel by pixel?

So I have 2 Image 's I want the first Image to move towards the other Image I have both X,Y coordinates, how should this be done if I want it moving pixel by pixel towards the target Image ? 所以我有2个Image我希望第一个Image移向另一个Image我有X,Y坐标,如果我想让它逐像素地朝目标Image移动,应该怎么做?

Bare in mind, I'm using Windows-Universal I've tried DoubleAnimation but my knowledge in that stuff is really bad, I don't know where to start. 请记住,我正在使用Windows-Universal我尝试过DoubleAnimation但我对这些东西的了解非常糟糕,我不知道从哪里开始。 The Image would sometimes have to move diagonally (across) rather than moving right then up. Image有时必须沿对角线(横向)移动而不是向右移动然后向上移动。

How should I do this? 我该怎么做?

This is what I have so far: 这是我到目前为止:

    private void MoveToTarget_Start()
    {
        moveToTimer = new DispatcherTimer();
        moveToTimer.Tick += MoveToTarget_Tick;
        moveToTimer.Interval = new TimeSpan(0, 0, 0, 0, 1);
        moveToTimer.Start();
    }

    void MoveToTarget_Tick(object sender, object e)
    {


    }

First, you need to know how many pixels you need to move. 首先,您需要知道需要移动的像素数。 For that, we can try to retrieve the absolute position of each element and compare them (there may be a more straightforward way, I just don't know how): 为此,我们可以尝试检索每个元素的绝对位置并进行比较(可能有一种更直接的方式,我只是不知道如何):

private Point GetAbsolutePosition(UIElement element)
{
   var ttv = element.TransformToVisual(Window.Current.Content);
   return ttv.TransformPoint(new Point(0, 0));
}

(taken from this answer ) (取自这个答案

From there, we retrieve the point of each element and compute the difference: 从那里,我们检索每个元素的点并计算差异:

var position1 = GetAbsolutePosition(Image1);
var position2 = GetAbsolutePosition(Image2);

var offsetX = position2.X - position1.X;
var offsetY = position2.Y - position1.Y;

Now, we now of how many pixels we'll have to move on each axis. 现在,我们现在需要在每个轴上移动多少像素。 We add the TranslateTransform for the element (it may be better to do that beforehand directly from the XAML): 我们为元素添加了TranslateTransform (事先直接从XAML中执行此操作可能更好):

var translateTransform = new TranslateTransform();
image1.RenderTransform = translateTransform;

Finally, we create the animations, and target the TranslateTransform . 最后,我们创建动画,并以TranslateTransform目标。 Then we group them in a Storyboard, and start it: 然后我们将它们分组在一个Storyboard中,然后启动它:

var animationX = new DoubleAnimation()
{
    From = 0,
    To = offsetX,
    Duration = TimeSpan.FromSeconds(2)
};

var animationY = new DoubleAnimation()
{
    From = 0,
    To = offsetY,
    Duration = TimeSpan.FromSeconds(2)
};

Storyboard.SetTarget(animationX, translateTransform);
Storyboard.SetTargetProperty(animationX, "X");

Storyboard.SetTarget(animationY, translateTransform);
Storyboard.SetTargetProperty(animationY, "Y");

var storyboard = new Storyboard();

storyboard.Children.Add(animationX);
storyboard.Children.Add(animationY);

storyboard.Begin();

To be honest the best idea is to use DoubleAnimation and Storyboard classes. 说实话,最好的想法是使用DoubleAnimation和Storyboard类。 I would set background as Canvas then You can animate it throught Canvas.SetLeft and Canvas.SetTop properties. 我将背景设置为Canvas然后您可以通过Canvas.SetLeft和Canvas.SetTop属性为其设置动画。

First You should create DoubleAnimationC class 首先你应该创建DoubleAnimationC类

DoubleAnimation da = new DoubleAnimation()
{
  SpeedRatio = 3.0,
  AutoReverse = false,
  From = 0 
  To = 100
  BeginTime = TimeSpan.FromSeconds(x),
};


Storyboard.SetTarget((Timeline)doubleAnimation, YOUR IMAGE);
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(Canvas.Top)"));

Of course change those properties as You like and now we must create StoryBoard class which will contains our animation 当然,按照您的喜好更改这些属性,现在我们必须创建包含动画的StoryBoard类

Storyboard sb = new Storyboard();
sb.Children.Add(da);
sb.Start();

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM