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

gmol1639  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(116)

我正在使用Xamarin.Forms,希望动态更改图像的位置(在代码后面)。
我最初通过在.xaml文件中定义静态加载图像。然后我想根据用户的运行时间值动态更新图像的位置(水平和垂直)。图像应根据用户输入值移动。
不幸的是,我找不到这方面的代码示例。
如何动态地改变图像的位置(在代码后面)?

gab6jxml

gab6jxml1#

可以使用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

holgip5t

holgip5t2#

translationX = 20; translationY = 20;

相关问题