[英]How to change the position or size of a view in Relative Layout at runtime in xamarin.forms
[英]How do I change an image's position (x and y) dynamically at runtime in Xamarin.Forms?
我正在使用 Xamarin.Forms 并希望动态更改图像的位置(在后面的代码中)。
我最初通过在 .xaml 文件中定义它来静态加载图像。 然后我想根据用户的运行时值动态更新图像的位置(水平和垂直)。 图像应根据用户输入值移动。
不幸的是,我无法为此找到代码示例。
如何动态更改图像的位置(在后面的代码中)?
您可以使用 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);
在哪里:
view - 要转换的视图。
x - 最终平移向量的 x 分量。
y - 最终平移向量的 y 分量。
length - 动画的持续时间(以毫秒为单位)。
easing - 动画的缓动。
示例如何为翻译设置动画:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.