Forum Xamarin Xamarin.Android

Pinch zoom in view group

I've been trying lots of thing, mainly this example https://developer.android.com/training/gestures/scale.html to make a custom view group with pinch zoom, but i just can't get this working. I googled a lot, tried different thins but y can't get this zoom function. I debugged the app, and i know the listener is getting the touch event, and the view is calling onDraw event, but nothing happens, the view content doesn't get zoomed.

Custom View

namespace Zoom { class ZoomeableViewGroup : RelativeLayout { private ScaleGestureDetector mScaleDetector; private float mScaleFactor = 1f; public ZoomeableViewGroup(Context context) : base(context, null, 0) { mScaleDetector = new ScaleGestureDetector(context, new ScaleListener(this)); SetWillNotDraw(false); } public ZoomeableViewGroup (Context context, IAttributeSet attr) : base(context,attr) { mScaleDetector = new ScaleGestureDetector(context, new ScaleListener(this)); SetWillNotDraw(false); } public override bool OnTouchEvent(MotionEvent ev) { mScaleDetector.OnTouchEvent(ev); return true; } protected override void OnDraw(Canvas canvas) { base.OnDraw(canvas); canvas.Save(); canvas.Scale(mScaleFactor, mScaleFactor); canvas.Restore(); } private class ScaleListener : ScaleGestureDetector.SimpleOnScaleGestureListener { private readonly ZoomeableViewGroup _view; public ScaleListener(ZoomeableViewGroup view) { _view = view; } public override bool OnScale(ScaleGestureDetector detector) { _view.mScaleFactor *= detector.ScaleFactor; _view.mScaleFactor = Math.Max(0.1f, Math.Min(_view.mScaleFactor, 5.0f)); _view.Invalidate(); return true; } } } }

XML

<?xml version="1.0" encoding="utf-8"?> <Zoom.ZoomeableViewGroup xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:layout_width="wrap_content" > <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tblSaldos" android:layout_width="match_parent" android:layout_height="match_parent" android:shrinkColumns="0" android:stretchColumns="0"> <TableRow android:layout_height="wrap_content" android:layout_width="match_parent" android:gravity="center_horizontal"> <TextView android:text="Cliente" style="@style/TablaTitulo" android:gravity="center_horizontal" /> <TextView android:text="Saldo" style="@style/TablaTitulo" android:gravity="center_horizontal" /> </TableRow> </TableLayout> </Zoom.ZoomeableViewGroup>

Sign In or Register to comment.