/** Gets the number of pointers in the current gesture */ public int getPointerCount() { return mDetector.getPointerCount(); }
/** Gets the number of pointers in the current gesture */ public int getPointerCount() { return mDetector.getPointerCount(); }
/** Gets the Y component of the translation */ public float getTranslationY() { return calcAverage(mDetector.getCurrentY(), mDetector.getPointerCount()) - calcAverage(mDetector.getStartY(), mDetector.getPointerCount()); }
/** Gets the X component of the translation */ public float getTranslationX() { return calcAverage(mDetector.getCurrentX(), mDetector.getPointerCount()) - calcAverage(mDetector.getStartX(), mDetector.getPointerCount()); }
/** Gets the X coordinate of the pivot point */ public float getPivotX() { return calcAverage(mDetector.getStartX(), mDetector.getPointerCount()); }
/** Gets the Y coordinate of the pivot point */ public float getPivotY() { return calcAverage(mDetector.getStartY(), mDetector.getPointerCount()); }
@Test public void testRotation() { when(mMultiPointerGestureDetector.getPointerCount()).thenReturn(0); assertEquals(0, mGestureDetector.getRotation(), 0); when(mMultiPointerGestureDetector.getPointerCount()).thenReturn(1); assertEquals(0, mGestureDetector.getRotation(), 0); when(mMultiPointerGestureDetector.getPointerCount()).thenReturn(2); assertEquals((float)-Math.PI/2, mGestureDetector.getRotation(), 1e-6); }
@Test public void testScale() { when(mMultiPointerGestureDetector.getPointerCount()).thenReturn(0); assertEquals(1, mGestureDetector.getScale(), 0); when(mMultiPointerGestureDetector.getPointerCount()).thenReturn(1); assertEquals(1, mGestureDetector.getScale(), 0); when(mMultiPointerGestureDetector.getPointerCount()).thenReturn(2); assertEquals(0.1f, mGestureDetector.getScale(), 1e-6); }
@Before public void setup() { mListener = mock(TransformGestureDetector.Listener.class); mMultiPointerGestureDetector = mock(MultiPointerGestureDetector.class); when(mMultiPointerGestureDetector.getPointerCount()).thenReturn(0); when(mMultiPointerGestureDetector.getStartX()).thenReturn(new float[] {100f, 200f}); when(mMultiPointerGestureDetector.getStartY()).thenReturn(new float[] {500f, 600f}); when(mMultiPointerGestureDetector.getCurrentX()).thenReturn(new float[] {10f, 20f}); when(mMultiPointerGestureDetector.getCurrentY()).thenReturn(new float[] {50f, 40f}); mGestureDetector = new TransformGestureDetector(mMultiPointerGestureDetector); mGestureDetector.setListener(mListener); }
/** Gets the scale */ public float getScale() { if (mDetector.getPointerCount() < 2) { return 1; } else { float startDeltaX = mDetector.getStartX()[1] - mDetector.getStartX()[0]; float startDeltaY = mDetector.getStartY()[1] - mDetector.getStartY()[0]; float currentDeltaX = mDetector.getCurrentX()[1] - mDetector.getCurrentX()[0]; float currentDeltaY = mDetector.getCurrentY()[1] - mDetector.getCurrentY()[0]; float startDist = (float) Math.hypot(startDeltaX, startDeltaY); float currentDist = (float) Math.hypot(currentDeltaX, currentDeltaY); return currentDist / startDist; } }
/** Gets the X component of the translation */ public float getTranslationX() { return calcAverage(mDetector.getCurrentX(), mDetector.getPointerCount()) - calcAverage(mDetector.getStartX(), mDetector.getPointerCount()); }
/** Gets the Y component of the translation */ public float getTranslationY() { return calcAverage(mDetector.getCurrentY(), mDetector.getPointerCount()) - calcAverage(mDetector.getStartY(), mDetector.getPointerCount()); }
/** Gets the rotation in radians */ public float getRotation() { if (mDetector.getPointerCount() < 2) { return 0; } else { float startDeltaX = mDetector.getStartX()[1] - mDetector.getStartX()[0]; float startDeltaY = mDetector.getStartY()[1] - mDetector.getStartY()[0]; float currentDeltaX = mDetector.getCurrentX()[1] - mDetector.getCurrentX()[0]; float currentDeltaY = mDetector.getCurrentY()[1] - mDetector.getCurrentY()[0]; float startAngle = (float) Math.atan2(startDeltaY, startDeltaX); float currentAngle = (float) Math.atan2(currentDeltaY, currentDeltaX); return currentAngle - startAngle; } } }
/** Gets the Y coordinate of the pivot point */ public float getPivotY() { return calcAverage(mDetector.getStartY(), mDetector.getPointerCount()); }
/** Gets the X coordinate of the pivot point */ public float getPivotX() { return calcAverage(mDetector.getStartX(), mDetector.getPointerCount()); }
@Test public void testPivot() { when(mMultiPointerGestureDetector.getPointerCount()).thenReturn(0); assertEquals(0, mGestureDetector.getPivotX(), 0); assertEquals(0, mGestureDetector.getPivotY(), 0); when(mMultiPointerGestureDetector.getPointerCount()).thenReturn(1); assertEquals(100, mGestureDetector.getPivotX(), 0); assertEquals(500, mGestureDetector.getPivotY(), 0); when(mMultiPointerGestureDetector.getPointerCount()).thenReturn(2); assertEquals(150, mGestureDetector.getPivotX(), 0); assertEquals(550, mGestureDetector.getPivotY(), 0); }
@Test public void testTranslation() { when(mMultiPointerGestureDetector.getPointerCount()).thenReturn(0); assertEquals(0, mGestureDetector.getTranslationX(), 0); assertEquals(0, mGestureDetector.getTranslationY(), 0); when(mMultiPointerGestureDetector.getPointerCount()).thenReturn(1); assertEquals(-90, mGestureDetector.getTranslationX(), 0); assertEquals(-450, mGestureDetector.getTranslationY(), 0); when(mMultiPointerGestureDetector.getPointerCount()).thenReturn(2); assertEquals(-135, mGestureDetector.getTranslationX(), 0); assertEquals(-505, mGestureDetector.getTranslationY(), 0); }
/** Gets the scale */ public float getScale() { if (mDetector.getPointerCount() < 2) { return 1; } else { float startDeltaX = mDetector.getStartX()[1] - mDetector.getStartX()[0]; float startDeltaY = mDetector.getStartY()[1] - mDetector.getStartY()[0]; float currentDeltaX = mDetector.getCurrentX()[1] - mDetector.getCurrentX()[0]; float currentDeltaY = mDetector.getCurrentY()[1] - mDetector.getCurrentY()[0]; float startDist = (float) Math.hypot(startDeltaX, startDeltaY); float currentDist = (float) Math.hypot(currentDeltaX, currentDeltaY); return currentDist / startDist; } }
/** Gets the rotation in radians */ public float getRotation() { if (mDetector.getPointerCount() < 2) { return 0; } else { float startDeltaX = mDetector.getStartX()[1] - mDetector.getStartX()[0]; float startDeltaY = mDetector.getStartY()[1] - mDetector.getStartY()[0]; float currentDeltaX = mDetector.getCurrentX()[1] - mDetector.getCurrentX()[0]; float currentDeltaY = mDetector.getCurrentY()[1] - mDetector.getCurrentY()[0]; float startAngle = (float) Math.atan2(startDeltaY, startDeltaX); float currentAngle = (float) Math.atan2(currentDeltaY, currentDeltaX); return currentAngle - startAngle; } } }