C# Xamarin图像裁剪
我试图通过使用图像在Xamarin表单中创建自己的“滑块”。我有下面的两张图片,上面的另一张图片是“手柄”。当我移动控制柄时,我希望在控制柄移动时裁剪空图像和填充图像(同时保持图像的纵横比)。当手柄滑动时,我可以通过在相应的图像上设置C# Xamarin图像裁剪,c#,image,xamarin,xamarin.forms,C#,Image,Xamarin,Xamarin.forms,我试图通过使用图像在Xamarin表单中创建自己的“滑块”。我有下面的两张图片,上面的另一张图片是“手柄”。当我移动控制柄时,我希望在控制柄移动时裁剪空图像和填充图像(同时保持图像的纵横比)。当手柄滑动时,我可以通过在相应的图像上设置X和Width值来获得工作版本,但我无法使纵横比保持不变……因为图像已调整大小,但未剪切。将Aspect属性更改为Aspect.AspectFill时,裁剪行为非常奇怪。值得一提的是,我正在通过LayoutBounds属性设置大小和位置 长话短说,我的问题是如何
X
和Width
值来获得工作版本,但我无法使纵横比保持不变……因为图像已调整大小,但未剪切。将Aspect
属性更改为Aspect.AspectFill
时,裁剪行为非常奇怪。值得一提的是,我正在通过LayoutBounds
属性设置大小和位置
长话短说,我的问题是如何在飞行中裁剪图像(从任何方向)
内容所在页面的XAML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="MyProject.Views.PageView"
Title="{Binding Title}">
<AbsoluteLayout>
<AbsoluteLayout x:Name="WidgetContent" />
</AbsoluteLayout>
</ContentPage>
此函数用于处理移动手柄以及尝试更新填充/空尺寸的位置:
private void UpdateHandlePosition(Xamarin.Forms.Rectangle position, Xamarin.Forms.Point touchPoint)
{
GraphWidget graphWidget = (GraphWidget)this.Widget;
double newX = position.Left + touchPoint.X;
double fillWidth = touchPoint.X + this.HandleDown.Width;
if (newX + this.HandleDown.Width > position.Right)
{
newX = position.Right - this.HandleDown.Width;
fillWidth = position.Width;
}
else if (newX < position.Left)
{
newX = position.Left;
fillWidth = 0.1;
}
this.HandleDown.XPosition = newX;
// HERE is my current attempt to resize the image
ImageViewModel fillImage = this.Images.ElementAt(1);
fillImage.Width = fillWidth;
}
private void UpdateHandlePosition(Xamarin.Forms.Rectangle位置,Xamarin.Forms.Point接触点)
{
GraphWidget GraphWidget=(GraphWidget)this.Widget;
double newX=位置.左+接触点.X;
double fillWidth=接触点.X+this.HandleDown.Width;
如果(newX+this.HandleDown.Width>position.Right)
{
newX=position.Right-this.HandleDown.Width;
fillWidth=位置。宽度;
}
else if(newX<位置左)
{
newX=位置。左;
填充宽度=0.1;
}
this.HandleDown.XPosition=newX;
//下面是我当前调整图像大小的尝试
ImageViewModel fillImage=this.Images.ElementAt(1);
fillImage.Width=fillWidth;
}
Xamarin.Forms还是Xamrin.iOS/Android?显示一些代码:)它是Xamarin.Forms。我会更新标签的。你也能显示一些代码吗?XMAL和/或图像布局背后的代码:)我会使用#xamarin和#xamarin.forms,因为我认为大多数回答者都在筛选#xamarin。视图是在绝对布局
中动态构建的。我只是将图像添加到该布局中,并为源
和布局边界
属性设置绑定。我还使用了AbsoluteLayoutFlags.None
,这样我就可以完全控制绝对布局中的位置和大小。代码可以显示1000多个单词。请把代码的两个重要部分加上好吗?它帮助人们重现你的问题。
private void UpdateHandlePosition(Xamarin.Forms.Rectangle position, Xamarin.Forms.Point touchPoint)
{
GraphWidget graphWidget = (GraphWidget)this.Widget;
double newX = position.Left + touchPoint.X;
double fillWidth = touchPoint.X + this.HandleDown.Width;
if (newX + this.HandleDown.Width > position.Right)
{
newX = position.Right - this.HandleDown.Width;
fillWidth = position.Width;
}
else if (newX < position.Left)
{
newX = position.Left;
fillWidth = 0.1;
}
this.HandleDown.XPosition = newX;
// HERE is my current attempt to resize the image
ImageViewModel fillImage = this.Images.ElementAt(1);
fillImage.Width = fillWidth;
}