Skip to content

Instantly share code, notes, and snippets.

@rdelrosario
Last active August 2, 2017 00:11
Show Gist options
  • Save rdelrosario/c29df1dcc7061f9d7b8a929e62f3990d to your computer and use it in GitHub Desktop.
Save rdelrosario/c29df1dcc7061f9d7b8a929e62f3990d to your computer and use it in GitHub Desktop.
Zoomable ScrollView Android Renderer
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