Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用xamarin跨平台在图像中进行缩放和平移_C#_Xaml_Xamarin.forms - Fatal编程技术网

C# 如何使用xamarin跨平台在图像中进行缩放和平移

C# 如何使用xamarin跨平台在图像中进行缩放和平移,c#,xaml,xamarin.forms,C#,Xaml,Xamarin.forms,我想在我的应用程序中缩放和平移一幅图像,但效果并不理想 我尝试了以下链接: 请帮帮我 这是我的XAML代码 <ContentPage.Content> <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" BackgroundColor="Yellow"> <Image Source="download" x:Name="img"&

我想在我的应用程序中缩放和平移一幅图像,但效果并不理想

我尝试了以下链接:

请帮帮我

这是我的XAML代码

<ContentPage.Content>
    <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" BackgroundColor="Yellow">
        <Image Source="download" x:Name="img">
            <Image.GestureRecognizers>
                <PanGestureRecognizer PanUpdated="OnPanUpdated"/>
                <PinchGestureRecognizer PinchUpdated="OnPinchUpdated"/>
                <TapGestureRecognizer Tapped="OnTapped"/>
            </Image.GestureRecognizers>
        </Image>
    </StackLayout>
</ContentPage.Content>

我的主页.cs

    private const double MIN_SCALE = 1;
    private const double MAX_SCALE = 4;
    private const double OVERSHOOT = 0.15;
    private double StartScale;
    private double LastX, LastY;
    private double StartX, StartY;

    public MainPage()
    {
        InitializeComponent();

        Scale = MIN_SCALE;
        TranslationX = TranslationY = 0;
        AnchorX = AnchorY = 0;
    }

    protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
    {
        Scale = MIN_SCALE;
        TranslationX = TranslationY = 0;
        AnchorX = AnchorY = 0;
        return base.OnMeasure(widthConstraint, heightConstraint);
    }

    private void OnTapped(object sender, EventArgs e)
    {
        if (Scale > MIN_SCALE)
        {
            this.ScaleTo(MIN_SCALE, 250, Easing.CubicInOut);
            this.TranslateTo(0, 0, 250, Easing.CubicInOut);
        }
        else
        {
            AnchorX = AnchorY = 0.5; //TODO tapped position
            this.ScaleTo(MAX_SCALE, 250, Easing.CubicInOut);
        }
    }

    private void OnPanUpdated(object sender, PanUpdatedEventArgs e)
    {
        if (Scale > MIN_SCALE)
            switch (e.StatusType)
            {
                case GestureStatus.Started:
                    StartX = (1 - AnchorX) * Width;
                    StartY = (1 - AnchorY) * Height;
                    break;
                case GestureStatus.Running:
                    TranslationX = Clamp(LastX + e.TotalX * Scale, -Width / 2, Width / 2);
                    TranslationY = Clamp(LastY + e.TotalY * Scale, -Height / 2, Height / 2);
                    break;
            }

        //switch (e.StatusType)
        //{
        //    case GestureStatus.Started:
        //        StartX = (1 - AnchorX) * Width;
        //        StartY = (1 - AnchorY) * Height;
        //        break;
        //    case GestureStatus.Running:
        //        AnchorX = Clamp(1 - (StartX + e.TotalX) / Width, 0, 1);
        //        AnchorY = Clamp(1 - (StartY + e.TotalY) / Height, 0, 1);
        //        break;
        //}

}

    private void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e)
    {
        switch (e.Status)
        {
            case GestureStatus.Started:
                StartScale = Scale;
                AnchorX = e.ScaleOrigin.X;
                AnchorY = e.ScaleOrigin.Y;
                break;
            case GestureStatus.Running:
                double current = Scale + (e.Scale - 1) * StartScale;
                Scale = Clamp(current, MIN_SCALE * (1 - OVERSHOOT), MAX_SCALE * (1 + OVERSHOOT));
                break;
            case GestureStatus.Completed:
                if (Scale > MAX_SCALE)
                    this.ScaleTo(MAX_SCALE, 250, Easing.SpringOut);
                else if (Scale < MIN_SCALE)
                    this.ScaleTo(MIN_SCALE, 250, Easing.SpringOut);
                break;
        }
    }

    protected override void OnSizeAllocated(double width, double height)
    {
        base.OnSizeAllocated(width, height); //must be called

        img.TranslateTo(0, 0, 0, Easing.Linear);
    }

    private T Clamp<T>(T value, T minimum, T maximum) where T : IComparable
    {
        if (value.CompareTo(minimum) < 0)
            return minimum;
        else if (value.CompareTo(maximum) > 0)
            return maximum;
        else
            return value;
    }

}
private const double MIN_SCALE=1;
私人常数双最大刻度=4;
专用常数双超调=0.15;
私人双星;
私人双拉斯特,拉斯特;
私人双星;
公共主页()
{
初始化组件();
刻度=最小刻度;
TranslationX=TranslationY=0;
AnchorX=AnchorY=0;
}
测量时受保护的覆盖大小请求(双宽度约束、双高度约束)
{
刻度=最小刻度;
TranslationX=TranslationY=0;
AnchorX=AnchorY=0;
返回base.OnMeasure(宽度约束、高度约束);
}
私有void OnTapped(对象发送方,事件参数e)
{
如果(比例>最小比例)
{
这个.ScaleTo(最小刻度,250,放松.CubicInOut);
这个.TranslateTo(0,0250,Easing.CubicInOut);
}
其他的
{
AnchorX=AnchorY=0.5;//TODO分接位置
此.ScaleTo(最大刻度,250,宽松.CubicInOut);
}
}
私有void OnPanUpdated(对象发送方,PanUpdatedEventArgs e)
{
如果(比例>最小比例)
开关(如状态类型)
{
案例手势状态。已启动:
StartX=(1-锚点)*宽度;
星形=(1-锚定)*高度;
打破
案例手势状态。正在运行:
平移X=夹具(LastX+e.TotalX*刻度,-宽度/2,宽度/2);
平移Y=夹具(最后一次+e.Total*刻度,-高度/2,高度/2);
打破
}
//开关(如状态类型)
//{
//案例手势状态。已启动:
//StartX=(1-锚点)*宽度;
//星形=(1-锚定)*高度;
//中断;
//案例手势状态。正在运行:
//锚具=夹具(1-(StartX+e.TotalX)/宽度,0,1);
//锚定=夹具(1-(起点+e.TotalY)/高度,0,1);
//中断;
//}
}
PinchUpdated上的私有void(对象发送方,PinchEstureUpdatedEventArgs e)
{
开关(如状态)
{
案例手势状态。已启动:
StartScale=比例;
AnchorX=e.ScaleOrigin.X;
AnchorY=e.ScaleOrigin.Y;
打破
案例手势状态。正在运行:
双电流=刻度+(e.刻度-1)*标准刻度;
刻度=钳位(电流、最小刻度*(1-超调)、最大刻度*(1+超调));
打破
案例管理状态。已完成:
如果(比例>最大比例)
这个.ScaleTo(最大刻度,250,放松.SpringOut);
else if(刻度<最小刻度)
这个.ScaleTo(MIN_SCALE,250,Easing.SpringOut);
打破
}
}
IzealLocated上的受保护覆盖空心(双倍宽度,双倍高度)
{
base.OnSizeAllocated(宽度、高度);//必须调用
图像平移(0,0,0,线性);
}
专用T形夹(T值,T最小值,T最大值),其中T:I可比较
{
如果(值比较到(最小值)<0)
返回最小值;
如果(值比较(最大值)>0)
返回最大值;
其他的
返回值;
}
}
当我缩放图像时,图像位置如下所示


图像手势不能很好地工作。双击图像时,放大和缩小都能很好地工作。

平移图像后,图像的位置改变是什么意思。有点模糊。@Paul Kertscher缩放图像时,图像的位置改变了