转换为 and/or 坐标系如何在 Xamarin Forms 中工作
How does Translate To and/or the Coordinate System work in Xamarin Forms
单击图像时,会出现一个下拉菜单 window,用户可以从众多选项中选择一个来放置图像。然而,第二张图片似乎偏离了大约 35,而第三张图片似乎低了大约 70。我正在使用 Image.TranslateTo。坐标系在 Xamarin Forms 中是如何工作的,有没有更好的方法将图像放在坐标系的特定位置?
关于Translation方法,里面有3个参数。
image.TranslateTo (TranslationX, TranslationY, millisecond);
- TranslationX : 获取或设置元素的 X 平移增量。
- TranslationY : 获取或设置元素的 Y 平移增量。
- millisecond : 完成动画需要多长时间(毫秒)。
也就是说参数TranslationX
和TranslationY
只是表示View
的Offset,而不是coordinate
。
如果我有如下 xaml 代码:
<Image Source="icon.png" BackgroundColor="Accent" HorizontalOptions="Center" VerticalOptions="Center">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" NumberOfTapsRequired="1"/>
</Image.GestureRecognizers>
</Image>
<Image x:Name="imageone" Source="imageone.png" BackgroundColor="Aqua" HorizontalOptions="Center" WidthRequest="50" HeightRequest="50"/>
<Image x:Name="imagetwo" Source="imagetwo.png" BackgroundColor="Beige" HeightRequest="50" />
<Image x:Name="imagethree" Source="imagethree.png" BackgroundColor="BurlyWood" HeightRequest="50" />
屏幕显示:(为了更好地解释,我将 HeightRequest
和 BackgroundColor
设置为 Image
。)
你会看到上面的代码,imageone/imagetwo/imagethree
的height
都是50
。我将在 运行 之后首先获得 imageone
的 X
。它是 180
。然后我将如下编写 TapGestureRecognizer_Tapped
方法的代码来翻译 imageone
和 imagetwo
.
private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
Console.WriteLine("Tap" + imageone.X +"---"+ imageone.Y);
imageone.TranslateTo(-180, 0, 1000);
imagetwo.TranslateTo(0, 100, 1000);
}
效果如下:
而 Console
的打印日志是:
14:34:36.621 I/mono-stdout( 4499): Tap180---290.714285714286
14:34:41.171 I/mono-stdout( 4499): Tap180---290.714285714286
然后你会发现有两个结论:
首先 , TranslationX 和 TranslationY 只是表示 View
的 Offset
.
其次,翻译方法不会修改View
的coordinate
。
单击图像时,会出现一个下拉菜单 window,用户可以从众多选项中选择一个来放置图像。然而,第二张图片似乎偏离了大约 35,而第三张图片似乎低了大约 70。我正在使用 Image.TranslateTo。坐标系在 Xamarin Forms 中是如何工作的,有没有更好的方法将图像放在坐标系的特定位置?
关于Translation方法,里面有3个参数。
image.TranslateTo (TranslationX, TranslationY, millisecond);
- TranslationX : 获取或设置元素的 X 平移增量。
- TranslationY : 获取或设置元素的 Y 平移增量。
- millisecond : 完成动画需要多长时间(毫秒)。
也就是说参数TranslationX
和TranslationY
只是表示View
的Offset,而不是coordinate
。
如果我有如下 xaml 代码:
<Image Source="icon.png" BackgroundColor="Accent" HorizontalOptions="Center" VerticalOptions="Center">
<Image.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" NumberOfTapsRequired="1"/>
</Image.GestureRecognizers>
</Image>
<Image x:Name="imageone" Source="imageone.png" BackgroundColor="Aqua" HorizontalOptions="Center" WidthRequest="50" HeightRequest="50"/>
<Image x:Name="imagetwo" Source="imagetwo.png" BackgroundColor="Beige" HeightRequest="50" />
<Image x:Name="imagethree" Source="imagethree.png" BackgroundColor="BurlyWood" HeightRequest="50" />
屏幕显示:(为了更好地解释,我将 HeightRequest
和 BackgroundColor
设置为 Image
。)
你会看到上面的代码,imageone/imagetwo/imagethree
的height
都是50
。我将在 运行 之后首先获得 imageone
的 X
。它是 180
。然后我将如下编写 TapGestureRecognizer_Tapped
方法的代码来翻译 imageone
和 imagetwo
.
private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
Console.WriteLine("Tap" + imageone.X +"---"+ imageone.Y);
imageone.TranslateTo(-180, 0, 1000);
imagetwo.TranslateTo(0, 100, 1000);
}
效果如下:
而 Console
的打印日志是:
14:34:36.621 I/mono-stdout( 4499): Tap180---290.714285714286
14:34:41.171 I/mono-stdout( 4499): Tap180---290.714285714286
然后你会发现有两个结论:
首先 , TranslationX 和 TranslationY 只是表示 View
的 Offset
.
其次,翻译方法不会修改View
的coordinate
。