我正在使用Xamarin.Forms,并希望动态更改图像的位置(在代码隐藏中(。
我最初通过在 .xaml 文件中定义图像来静态加载图像。然后,我想根据用户的运行时值动态更新图像的位置(水平和垂直(。图像应根据用户输入值移动。
不幸的是,我找不到为此的代码示例。
如何动态更改图像的位置(在代码隐藏中(?
您可以使用 TranslationX 和 TranslationY 属性操作元素的 X 和 Y 坐标。
要制作动画,您可以使用方法翻译到:
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);
哪里:
视图- 坦斯板的视图。
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