0
我需要一個大文本的標籤,如需要捏縮放功能的文章,我已經寫了一個ZoomableScrollview在IOS和Windows,但不是在Android中正常工作。請參閱下面的代碼。xamarin形式的標籤在滾動視圖捏和縮放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中我能夠在一定程度上實現縮放,但滾動不順暢,但我妥協了。現在問題在於標籤中的文字被剝離了。有人請幫助我,我的應用程序本身是閱讀哪些是他基本功能不工作。 在此先感謝