Search in sources :

Example 66 with MotionEvent

use of android.view.MotionEvent in project fresco by facebook.

the class MultiPointerGestureDetectorTest method testSinglePointer.

@Test
public void testSinglePointer() {
    MotionEvent event1 = obtainMotionEvent(1000, 1000, MotionEvent.ACTION_DOWN, 0, 100f, 300f);
    MotionEvent event2 = obtainMotionEvent(1000, 1010, MotionEvent.ACTION_MOVE, 0, 150f, 350f);
    MotionEvent event3 = obtainMotionEvent(1000, 1020, MotionEvent.ACTION_MOVE, 0, 200f, 400f);
    MotionEvent event4 = obtainMotionEvent(1000, 1030, MotionEvent.ACTION_UP, 0, 200f, 400f);
    InOrder inOrder = inOrder(mListener);
    mGestureDetector.onTouchEvent(event1);
    mGestureDetector.onTouchEvent(event2);
    assertTrue(mGestureDetector.isGestureInProgress());
    assertEquals(1, mGestureDetector.getPointerCount());
    assertEquals(100f, mGestureDetector.getStartX()[0], 0);
    assertEquals(300f, mGestureDetector.getStartY()[0], 0);
    assertEquals(150f, mGestureDetector.getCurrentX()[0], 0);
    assertEquals(350f, mGestureDetector.getCurrentY()[0], 0);
    inOrder.verify(mListener).onGestureBegin(mGestureDetector);
    inOrder.verify(mListener).onGestureUpdate(mGestureDetector);
    mGestureDetector.onTouchEvent(event3);
    assertTrue(mGestureDetector.isGestureInProgress());
    assertEquals(1, mGestureDetector.getPointerCount());
    assertEquals(100f, mGestureDetector.getStartX()[0], 0);
    assertEquals(300f, mGestureDetector.getStartY()[0], 0);
    assertEquals(200f, mGestureDetector.getCurrentX()[0], 0);
    assertEquals(400f, mGestureDetector.getCurrentY()[0], 0);
    inOrder.verify(mListener).onGestureUpdate(mGestureDetector);
    mGestureDetector.onTouchEvent(event4);
    assertFalse(mGestureDetector.isGestureInProgress());
    assertEquals(0, mGestureDetector.getPointerCount());
    inOrder.verify(mListener).onGestureEnd(mGestureDetector);
    inOrder.verifyNoMoreInteractions();
    event1.recycle();
    event2.recycle();
    event3.recycle();
    event4.recycle();
}
Also used : InOrder(org.mockito.InOrder) MotionEventTestUtils.obtainMotionEvent(com.facebook.samples.gestures.MotionEventTestUtils.obtainMotionEvent) MotionEvent(android.view.MotionEvent) ShadowMotionEvent(robolectric3.shadows.ShadowMotionEvent) Test(org.junit.Test)

Example 67 with MotionEvent

use of android.view.MotionEvent in project fresco by facebook.

the class TransformGestureDetectorTest method testOnTouchEvent.

@Test
public void testOnTouchEvent() {
    MotionEvent motionEvent = mock(MotionEvent.class);
    mGestureDetector.onTouchEvent(motionEvent);
    verify(mMultiPointerGestureDetector).onTouchEvent(motionEvent);
}
Also used : MotionEvent(android.view.MotionEvent) Test(org.junit.Test)

Example 68 with MotionEvent

use of android.view.MotionEvent in project FBReaderJ by geometer.

the class FileChooserActivity method initGestureDetector.

private void initGestureDetector() {
    mListviewFilesGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {

        private Object getData(float x, float y) {
            int i = getSubViewId(x, y);
            if (i >= 0)
                return mViewFiles.getItemAtPosition(mViewFiles.getFirstVisiblePosition() + i);
            return null;
        }

        // getSubView()
        /**
             * Gets {@link IFileDataModel} from {@code e}.
             * 
             * @param e
             *            {@link MotionEvent}.
             * @return the data model, or {@code null} if not available.
             */
        private IFileDataModel getDataModel(MotionEvent e) {
            Object o = getData(e.getX(), e.getY());
            return o instanceof IFileDataModel ? (IFileDataModel) o : null;
        }

        // getDataModel()
        private int getSubViewId(float x, float y) {
            Rect r = new Rect();
            for (int i = 0; i < mViewFiles.getChildCount(); i++) {
                mViewFiles.getChildAt(i).getHitRect(r);
                if (r.contains((int) x, (int) y))
                    return i;
            }
            return -1;
        }

        // getSubViewId()
        @Override
        public void onLongPress(MotionEvent e) {
        // do nothing
        }

        // onLongPress()
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            // do nothing
            return false;
        }

        // onSingleTapConfirmed()
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            if (mDoubleTapToChooseFiles) {
                if (mIsMultiSelection)
                    return false;
                IFileDataModel data = getDataModel(e);
                if (data == null)
                    return false;
                if (data.getFile().isDirectory() && IFileProvider.FilterMode.FilesOnly.equals(mFileProvider.getFilterMode()))
                    return false;
                if (mIsSaveDialog) {
                    if (data.getFile().isFile()) {
                        mTxtSaveas.setText(data.getFile().getName());
                        doCheckSaveasFilenameAndFinish(data.getFile().getName());
                    } else
                        return false;
                } else
                    doFinish(data.getFile());
            } else // double tap to choose files
            {
                // do nothing
                return false;
            }
            return true;
        }

        // onDoubleTap()
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            // 10 is too short :-D
            final int _max_y_distance = 19;
            final int _min_x_distance = 80;
            final int _min_x_velocity = 200;
            if (Math.abs(e1.getY() - e2.getY()) < _max_y_distance && Math.abs(e1.getX() - e2.getX()) > _min_x_distance && Math.abs(velocityX) > _min_x_velocity) {
                Object o = getData(e1.getX(), e1.getY());
                if (o instanceof IFileDataModel) {
                    ((IFileDataModel) o).setTobeDeleted(true);
                    mFileAdapter.notifyDataSetChanged();
                    doDeleteFile((IFileDataModel) o);
                }
            }
            /*
                 * always return false to let the default handler draw the item
                 * properly
                 */
            return false;
        }
    });
// mListviewFilesGestureDetector
}
Also used : Rect(android.graphics.Rect) GestureDetector(android.view.GestureDetector) MotionEvent(android.view.MotionEvent)

Example 69 with MotionEvent

use of android.view.MotionEvent in project cardslib by gabrielemariotti.

the class NativeShadowFragment method init_card_animation_shadow.

/**
     * This method builds a card with an animation
     */
private void init_card_animation_shadow() {
    //Create a Card
    Card card = new Card(getActivity());
    //Create a CardHeader
    CardHeader header = new CardHeader(getActivity());
    //Set the header title
    header.setTitle(getString(R.string.demo_header_basetitle));
    card.addCardHeader(header);
    //Set card in the cardView
    final CardViewNative cardView = (CardViewNative) getActivity().findViewById(R.id.carddemo_shadow_animation);
    cardView.setCard(card);
    cardView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch(motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if (Build.VERSION.SDK_INT >= Constants.API_L) {
                        cardView.animate().setDuration(100).scaleX(1.1f).scaleY(1.1f).translationZ(10);
                    } else {
                        cardView.animate().setDuration(100).scaleX(1.1f).scaleY(1.1f);
                    }
                    return true;
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                    if (Build.VERSION.SDK_INT >= Constants.API_L) {
                        cardView.animate().setDuration(100).scaleX(1).scaleY(1).translationZ(0);
                    } else {
                        cardView.animate().setDuration(100).scaleX(1).scaleY(1);
                    }
                    return true;
            }
            return false;
        }
    });
}
Also used : CardHeader(it.gmariotti.cardslib.library.internal.CardHeader) ScrollView(android.widget.ScrollView) View(android.view.View) Card(it.gmariotti.cardslib.library.internal.Card) CardViewNative(it.gmariotti.cardslib.library.view.CardViewNative) MotionEvent(android.view.MotionEvent)

Example 70 with MotionEvent

use of android.view.MotionEvent in project fresco by facebook.

the class GestureDetectorTest method testOnClick_ToFar.

@Test
public void testOnClick_ToFar() {
    float s = mScaledTouchSlop;
    long T0 = 1000;
    long T1 = T0;
    MotionEvent event1 = obtain(T0, T1, ACTION_DOWN, 100.f, 100.f, 0);
    mGestureDetector.onTouchEvent(event1);
    assertEquals(true, mGestureDetector.mIsCapturingGesture);
    assertEquals(true, mGestureDetector.mIsClickCandidate);
    assertEquals(event1.getEventTime(), mGestureDetector.mActionDownTime);
    assertEquals(event1.getX(), mGestureDetector.mActionDownX, 0f);
    assertEquals(event1.getY(), mGestureDetector.mActionDownY, 0f);
    long T2 = T0 + mLongPressTimeout * 1 / 3;
    MotionEvent event2 = obtain(T0, T2, ACTION_MOVE, 100.f + s * 0.5f, 100.f - s * 0.5f, 0);
    mGestureDetector.onTouchEvent(event2);
    assertEquals(true, mGestureDetector.mIsCapturingGesture);
    assertEquals(true, mGestureDetector.mIsClickCandidate);
    assertEquals(event1.getEventTime(), mGestureDetector.mActionDownTime);
    assertEquals(event1.getX(), mGestureDetector.mActionDownX, 0f);
    assertEquals(event1.getY(), mGestureDetector.mActionDownY, 0f);
    // maximum allowed distance is `s` px, but here we went `s * 1.1` px away from down point
    long T3 = T0 + mLongPressTimeout * 2 / 3;
    MotionEvent event3 = obtain(T0, T3, ACTION_MOVE, 100.f + s * 1.1f, 100.f - s * 0.5f, 0);
    mGestureDetector.onTouchEvent(event3);
    assertEquals(true, mGestureDetector.mIsCapturingGesture);
    assertEquals(false, mGestureDetector.mIsClickCandidate);
    assertEquals(event1.getEventTime(), mGestureDetector.mActionDownTime);
    assertEquals(event1.getX(), mGestureDetector.mActionDownX, 0f);
    assertEquals(event1.getY(), mGestureDetector.mActionDownY, 0f);
    long T4 = T0 + mLongPressTimeout;
    MotionEvent event4 = obtain(T0, T4, ACTION_UP, 100.f + s, 100.f - s, 0);
    mGestureDetector.onTouchEvent(event4);
    assertEquals(false, mGestureDetector.mIsCapturingGesture);
    assertEquals(false, mGestureDetector.mIsClickCandidate);
    assertEquals(event1.getEventTime(), mGestureDetector.mActionDownTime);
    assertEquals(event1.getX(), mGestureDetector.mActionDownX, 0f);
    assertEquals(event1.getY(), mGestureDetector.mActionDownY, 0f);
    verifyNoMoreInteractions(mClickListener);
    event1.recycle();
    event2.recycle();
    event3.recycle();
    event4.recycle();
}
Also used : MotionEvent(android.view.MotionEvent) Test(org.junit.Test)

Aggregations

MotionEvent (android.view.MotionEvent)717 View (android.view.View)255 TextView (android.widget.TextView)113 ImageView (android.widget.ImageView)91 Point (android.graphics.Point)63 OnTouchListener (android.view.View.OnTouchListener)48 GestureDetector (android.view.GestureDetector)47 Paint (android.graphics.Paint)40 ViewGroup (android.view.ViewGroup)36 Test (org.junit.Test)35 AdapterView (android.widget.AdapterView)34 RecyclerView (android.support.v7.widget.RecyclerView)33 ListView (android.widget.ListView)33 SuppressLint (android.annotation.SuppressLint)31 Intent (android.content.Intent)30 Instrumentation (android.app.Instrumentation)27 AbsListView (android.widget.AbsListView)27 OnClickListener (android.view.View.OnClickListener)24 Rect (android.graphics.Rect)22 Handler (android.os.Handler)20