xamarin在滚动视图中形成标签捏和缩放android

时间:2016-05-18 03:02:27

标签: xamarin.android scrollview xamarin.forms

我需要一个带有大文本的标签,就像需要捏缩放功能的文章一样,我已经编写了一个ZoomableScrollview,它可以在IOS和Windows中正常工作,但不能在android中工作。请参阅下面的代码。

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渲染器

 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渲染器

 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中我能够在某种程度上实现缩放,但滚动不顺畅,但我妥协了。现在的问题是文本在标签中被剥离了。有人请帮助我,我的应用程序本身是阅读,这是他的基本功能无法正常工作。 提前致谢

1 个答案:

答案 0 :(得分:0)

我明白了。基本上android标签没有行限制为100,我必须在渲染器中重写它

 label.SetMaxLines(4000);