【问题标题】:How do I change an image's position (x and y) dynamically at runtime in Xamarin.Forms?如何在运行时在 Xamarin.Forms 中动态更改图像的位置(x 和 y)?
【发布时间】:2019-03-16 18:42:21
【问题描述】:

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

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

很遗憾,我找不到这方面的代码示例。

如何动态更改图像的位置(在后面的代码中)?

【问题讨论】:

    标签: c# image xamarin xamarin.forms position


    【解决方案1】:

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

    【讨论】:

    • TranslateTo 效果很好。但是当我第二次尝试移动它时。它返回到原来的位置并从那里开始位置
    • 已解决以供将来参考我将坐标保存在 TouchActionType.ReleasedTouchActionType.Moved(就我而言)上,我在 xDif 和 yDif 上添加了保存的值。 X 或 Y 坐标可通过'object'.TranslationX or .TranslationY 获取
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    相关资源
    最近更新 更多