Example usage for android.view MotionEvent getX

List of usage examples for android.view MotionEvent getX

Introduction

In this page you can find the example usage for android.view MotionEvent getX.

Prototype

public final float getX() 

Source Link

Document

#getX(int) for the first pointer index (may be an arbitrary pointer identifier).

Usage

From source file:Main.java

public static float getX(MotionEvent event, int index) {
    float value;/*from   w  ww.ja  v a 2s. co  m*/
    try {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ECLAIR) {
            if (index == 0) {
                value = event.getX();
            } else {
                value = 0;
            }
        } else {
            value = ((Float) (event.getClass().getMethod("getX", new Class[] { int.class }).invoke(event,
                    new Object[] { Integer.valueOf(index) }))).floatValue();
        }
    } catch (IllegalArgumentException e) {
        throw new RuntimeException(e);
    } catch (SecurityException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
    return value;
}

From source file:Main.java

/**
 * get coordinate in parent's coordinate system
 * /*from   w  ww.j  ava 2  s.  c  o  m*/
 * @param view target view
 * @param location an array of two integers in which to hold the coordinates
 */
public static void getCoordinateInParent(View view, MotionEvent event, int[] location) {
    if (view == null || event == null || view.getParent() == null || location.length < 2) {
        return;
    }
    int parentLocation[] = new int[2];
    ((View) view.getParent()).getLocationOnScreen(parentLocation);
    if (parentLocation != null && parentLocation.length > 1) {
        location[0] = (int) event.getX() - parentLocation[0];
        location[1] = (int) event.getY() - parentLocation[1];
    }
}

From source file:Main.java

public static MotionEvent hoverMotionEventAtPosition(View view, int action, int xPercent, int yPercent) {
    MotionEvent ev = motionEventAtPosition(view, action, xPercent, yPercent);

    MotionEvent.PointerProperties[] pointerProperties = new MotionEvent.PointerProperties[1];
    pointerProperties[0] = new MotionEvent.PointerProperties();

    MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[1];
    pointerCoords[0] = new MotionEvent.PointerCoords();
    pointerCoords[0].x = ev.getX();
    pointerCoords[0].y = ev.getY();//  w  ww .java2  s  .c  o m

    return MotionEvent.obtain(ev.getDownTime(), ev.getEventTime(), ev.getAction(), 1, pointerProperties,
            pointerCoords, ev.getMetaState(), 0, ev.getXPrecision(), ev.getYPrecision(), ev.getDeviceId(),
            ev.getEdgeFlags(), InputDevice.SOURCE_CLASS_POINTER, ev.getFlags());
}

From source file:Main.java

public static void addTouchFeedback(final ImageView view) {
    view.setOnTouchListener(new View.OnTouchListener() {
        private Rect rect;

        @Override//from  w w  w. ja v  a2 s . co m
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                view.setColorFilter(Color.argb(50, 0, 0, 0));
                rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                view.setColorFilter(Color.argb(0, 0, 0, 0));
            }
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) {
                    view.setColorFilter(Color.argb(0, 0, 0, 0));
                }
            }
            return false;
        }
    });
}

From source file:Main.java

public static void handleAutoCloseKeyboard(boolean isAutoCloseKeyboard, View currentFocusView,
        MotionEvent motionEvent, Object dialogOrActivity) {
    if (isAutoCloseKeyboard && motionEvent.getAction() == MotionEvent.ACTION_DOWN && currentFocusView != null
            && (currentFocusView instanceof EditText) && dialogOrActivity != null) {
        int[] leftTop = { 0, 0 };
        currentFocusView.getLocationInWindow(leftTop);
        int left = leftTop[0];
        int top = leftTop[1];
        int bottom = top + currentFocusView.getHeight();
        int right = left + currentFocusView.getWidth();
        if (!(motionEvent.getX() > left && motionEvent.getX() < right && motionEvent.getY() > top
                && motionEvent.getY() < bottom)) {
            if (dialogOrActivity instanceof Dialog) {
                closeKeyboard((Dialog) dialogOrActivity);
            } else if (dialogOrActivity instanceof Activity) {
                closeKeyboard((Activity) dialogOrActivity);
            }//w  w  w.  ja  v a 2s.  c  o  m
        }
    }
}

From source file:android.support.test.testapp.VerticalViewPager.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    event.setLocation(event.getY() * getWidth() / getHeight(), event.getX() * getHeight() / getWidth());
    return super.onTouchEvent(event);
}

From source file:com.android.deskclock.VerticalViewPager.java

private boolean verticalDrag(MotionEvent ev) {
    final float x = ev.getX();
    final float y = ev.getY();
    ev.setLocation(y, x);// w  w  w  .j a  v a  2  s . c o m
    return super.onTouchEvent(ev);
}

From source file:cn.limc.androidcharts.event.SlipGestureDetector.java

private float calEventDistance(MotionEvent initalEvent, MotionEvent finalEvent) {
    float xSpan = initalEvent.getX() - finalEvent.getX();
    float ySpan = initalEvent.getY() - finalEvent.getY();
    return (float) Math.sqrt(xSpan * xSpan + ySpan * ySpan);
}

From source file:android.hawkencompanionapp.fragments.MechGuideFragment.java

@Override
public boolean onTouch(View v, MotionEvent event) {
    final float xPos = event.getX();

    if (xPos > mSwipeXPxOffset) { //Prevent the nav drawer from registering the event
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            mPrevXdp = xPos;// w ww .  j  a va2s.c  om
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (mPrevXdp < xPos) { //Right swipe
                switchTab("Right");
            } else { //Left swipe
                switchTab("Left");
            }
        }
        return true;
    }
    return false;
}

From source file:de.struckmeierfliesen.ds.wochenbericht.ClearableAutoCompleteTextView.java

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (getCompoundDrawables()[2] != null) {
        boolean tappedX = event.getX() > (getWidth() - getPaddingRight()
                - (int) (xD.getIntrinsicWidth() * DRAWABLE_MULTIPLIER));
        if (tappedX) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                setText("");
                if (textClearListener != null) {
                    textClearListener.didClearText();
                }//from   w ww  . j  ava 2  s.  co m
            }
            return true;
        }
    }
    if (l != null) {
        return l.onTouch(v, event);
    }
    return false;
}