在Xamarin.Forms中,我如何在运行时动态地改变图像的位置(x和y)?

问题描述 投票:2回答:1

我正在使用Xamarin.Forms,并希望动态地改变图像的位置(在代码后面)。

我已经通过在一个.xaml文件中定义静态加载图像。然后我想根据用户的运行时间值动态地更新图像的位置(水平和垂直)。图像应该根据用户的输入值进行移动。

不幸的是,我无法找到这样的代码示例。

我怎样才能动态地改变图像的位置(在代码后面)?

c# image xamarin xamarin.forms position
1个回答
2
投票

你可以使用TranslationX和TranslationY属性来操作元素的X和Y坐标。

要制作动画,你可以使用TranslateTo方法。

public static System.Threading.Tasks.Task<bool> TranslateTo (this Xamarin.Forms.VisualElement view, double x, double y, uint length = 250, Xamarin.Forms.Easing easing = null);

Where:

  • 视图 - 坛坛罐罐的观点。

  • x - 最终翻译向量的X分量。

  • y - 最终翻译向量的Y分量。

  • 长度 - 动画的持续时间,单位为毫秒。

  • 缓和 - 动画的缓和。

例如何进行动画翻译。

await image.TranslateTo (-100, 0, 1000);    // Move image left
await image.TranslateTo (-100, -100, 1000); // Move image up
await image.TranslateTo (100, 100, 2000);   // Move image diagonally down and right
await image.TranslateTo (0, 100, 1000);     // Move image left
await image.TranslateTo (0, 0, 1000);       // Move image up
© www.soinside.com 2019 - 2024. All rights reserved.