Last active
August 2, 2017 00:11
-
-
Save rdelrosario/c29df1dcc7061f9d7b8a929e62f3990d to your computer and use it in GitHub Desktop.
Zoomable ScrollView Android Renderer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Android.App; | |
using Android.Content; | |
using Android.OS; | |
using Android.Runtime; | |
using Android.Views; | |
using Android.Widget; | |
using Xamarin.Forms.Platform.Android; | |
using static Android.Views.ScaleGestureDetector; | |
using Android.Views.Animations; | |
using Xamarin.Forms; | |
using ZoomableApp.Droid.Renderers; | |
[assembly: ExportRenderer(typeof(Xamarin.Forms.ScrollView), typeof(ZoomScrollViewRenderer))] | |
namespace ZoomableApp.Droid.Renderers | |
{ | |
public class ZoomScrollViewRenderer : ScrollViewRenderer, IOnScaleGestureListener | |
{ | |
private float mScale = 1f; | |
private ScaleGestureDetector mScaleDetector; | |
protected override void OnElementChanged(VisualElementChangedEventArgs e) | |
{ | |
base.OnElementChanged(e); | |
mScaleDetector = new ScaleGestureDetector(Context, this); | |
} | |
public override bool DispatchTouchEvent(MotionEvent e) | |
{ | |
base.DispatchTouchEvent(e); | |
return mScaleDetector.OnTouchEvent(e); | |
} | |
public bool OnScale(ScaleGestureDetector detector) | |
{ | |
float scale = 1 - detector.ScaleFactor; | |
float prevScale = mScale; | |
mScale += scale; | |
if (mScale < 0.5f) // Minimum scale condition: | |
mScale = 0.5f; | |
if (mScale > 1f) // Maximum scale condition: | |
mScale = 1f; | |
ScaleAnimation scaleAnimation = new ScaleAnimation(1f / prevScale, 1f / mScale, 1f / prevScale, 1f / mScale, detector.FocusX, detector.FocusY); | |
scaleAnimation.Duration =0; | |
scaleAnimation.FillAfter =true; | |
StartAnimation(scaleAnimation); | |
return true; | |
} | |
public bool OnScaleBegin(ScaleGestureDetector detector) | |
{ | |
return true; | |
} | |
public void OnScaleEnd(ScaleGestureDetector detector) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment