Xamarin在滚动视图中捏标签并缩放android [英] xamarin forms label in a scroll view pinch and zoom android

查看:123
本文介绍了Xamarin在滚动视图中捏标签并缩放android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个带有大文本的标签,例如需要捏紧缩放功能的文章,因为我写了一个ZoomableScrollview,在iOS和Windows上都可以正常工作,但在android上却不行.请参见下面的代码.

I need a label with large text like a article which needs pinch to zoom capability for that I have written a ZoomableScrollview which works fine in IOS and Windows but not in android. Please see the code below.

PCL中的代码

public class ZoomableScrollView:ScrollView
{
    public static readonly BindableProperty MinimumZoomScaleProperty = BindableProperty.Create("MinimumZoomScale", typeof(float), typeof(ZoomableScrollView), default(float));

    public float MinimumZoomScale
    {
        get { return (float)GetValue(MinimumZoomScaleProperty); }
        set { SetValue(MinimumZoomScaleProperty, value); }
    }
    public static readonly BindableProperty MaximumZoomScaleProperty = BindableProperty.Create("MaximumZoomScale", typeof(float), typeof(ZoomableScrollView), default(float));

    public float MaximumZoomScale
    {
        get { return (float)GetValue(MaximumZoomScaleProperty); }
        set { SetValue(MaximumZoomScaleProperty, value); }
    }
}

IOS渲染器

public class ZoomableScrollViewRenderer : ScrollViewRenderer
{
    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);
        if (e.NewElement == null)
            return;

        if (e.OldElement == null)
        {
            ZoomableScrollView zsv = Element as ZoomableScrollView;
            this.MinimumZoomScale = zsv.MinimumZoomScale;
            this.MaximumZoomScale = zsv.MaximumZoomScale;
            this.ViewForZoomingInScrollView += (UIScrollView sv) => { return this.Subviews[0]; };
        }
    }
}

Windows渲染器

Windows renderer

 public class ZoomableScrollViewRenderer:ScrollViewRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<ScrollView> e)
    {
        base.OnElementChanged(e);
        if (e.NewElement == null)
            return;

        if (e.OldElement == null)
        {
            ZoomableScrollView zsv = Element as ZoomableScrollView;
            this.Control.ZoomMode = Windows.UI.Xaml.Controls.ZoomMode.Enabled;
            this.Control.MinZoomFactor = zsv.MinimumZoomScale;
            this.Control.MaxZoomFactor = zsv.MaximumZoomScale;
        }
    }
}

Android渲染器

Android renderer

 public class ZoomableScrollViewRenderer:ScrollViewRenderer
{
    float originalDistanceX, currentdistanceX, originalDistanceY, currentdistanceY;
    bool IsPinching = false;
    double currentScale;
    TeluguLabel lbl;
    ScrollView svMain, svSub;

    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);
        svMain = ((ScrollView)e.NewElement);
        lbl = svMain.Content as TeluguLabel;
        svSub = new ScrollView();
        svSub.Orientation = ScrollOrientation.Horizontal;
        svSub.Content = lbl;
        svMain.Content = svSub;
        lbl.AnchorX = 0;
        lbl.AnchorY = 0;

    }
    public override bool OnTouchEvent(MotionEvent e)
    {
        if (e.PointerCount > 1)
        {
            IsPinching = true;
            currentScale = lbl.Scale;
            originalDistanceX = Math.Abs(e.GetX(0) - e.GetX(1));
            originalDistanceY = Math.Abs(e.GetY(0) - e.GetY(1));
        }
        else
        {
            IsPinching = false;
        }
        return base.OnTouchEvent(e);
    }
    public override bool DispatchTouchEvent(Android.Views.MotionEvent e)
    {
        switch (e.Action)
        {
            case MotionEventActions.Down:
                this.Parent.RequestDisallowInterceptTouchEvent(true);
                break;
            case MotionEventActions.Move:
                if(IsPinching && e.PointerCount > 1)
                {
                    currentdistanceX = Math.Abs(e.GetX(0) - e.GetX(1));
                    currentdistanceY = Math.Abs(e.GetY(0) - e.GetY(1));
                    if (originalDistanceX < currentdistanceX || originalDistanceY < currentdistanceY)
                        lbl.Scale = currentScale + 0.01;
                    else if (originalDistanceX > currentdistanceX || originalDistanceY > currentdistanceY)
                        lbl.Scale = currentScale - 0.01;
                }
                break;
            case MotionEventActions.Up:
                this.Parent.RequestDisallowInterceptTouchEvent(false);
                break;
        }
        return base.DispatchTouchEvent(e);
    }


}

在android中,我能够达到一定程度的缩放,但是滚动并不顺畅,但是我为此做出了妥协.现在的问题是标签中的文本被剥离了.有人请帮助我,我的应用程序本身就是供阅读的,这是他的基本功能无法正常工作的原因. 预先感谢

in android I was able to achieve zoom to some extent but the scrolling in not smooth, but I compromised for it. Now the problem is the text is getting stripped in the label. Somebody please help me my app itself is for reading which is he basic feature not working. Thanks in advance

推荐答案

我明白了.基本上android标签的行数限制为100,我必须在渲染器中将其覆盖

I got it. Basically android label has no of lines limit of 100, i have to override it in the renderer

 label.SetMaxLines(4000);

这篇关于Xamarin在滚动视图中捏标签并缩放android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆