Code example for MotionEvent

Methods: getActiongetXoffsetLocationsetAction

0
        int buttonRange = iconWidth + ICON_SPACING;
        return (mNonIndicatorButtonCount + ((baselineX - x) / buttonRange));
    } 
 
    private void dispatchRelativeTouchEvent(View view, MotionEvent event) {
        event.offsetLocation(-view.getLeft(), -view.getTop());
        view.dispatchTouchEvent(event);
        event.offsetLocation(view.getLeft(), view.getTop());
    } 
 
    @Override 
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (!onFilterTouchEventForSecurity(event)) return false;
 
        int action = event.getAction();
        if (!isEnabled()) return false; 
 
        double x = (double) event.getX();
        int width = getWidth();
        if (width == 0) return false; // the event is sent before onMeasure()
        if (x > width) x = width;
 
        int index = getTouchViewIndex((int) x, width);
 
        // Cancel the previous target if we moved out of it 
        if ((mSelectedIndex != -1) && (index != mSelectedIndex)) {
            View p = getChildAt(mSelectedIndex);
 
            int oldAction = event.getAction();
            event.setAction(MotionEvent.ACTION_CANCEL);
            dispatchRelativeTouchEvent(p, event);
            event.setAction(oldAction);
 
            if (p instanceof AbstractIndicatorButton) {
                AbstractIndicatorButton b = (AbstractIndicatorButton) p;
                b.dismissPopup();
            } 
        } 
 
        // Send event to the target 
        View v = getChildAt(index);
        if (v == null) return true;
 
        // Change MOVE to DOWN if this is a new target 
        if (mSelectedIndex != index && action == MotionEvent.ACTION_MOVE) {
            event.setAction(MotionEvent.ACTION_DOWN);
        } 
        dispatchRelativeTouchEvent(v, event);
        mSelectedIndex = index;
        return true; 
    }