簡體   English   中英

如何在運行時在 Xamarin.Forms 中動態更改圖像的位置(x 和 y)?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM