Example usage for android.view MotionEvent getAxisValue

List of usage examples for android.view MotionEvent getAxisValue

Introduction

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

Prototype

public final float getAxisValue(int axis) 

Source Link

Document

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

Usage

From source file:org.mariotaku.twidere.view.ExtendedRecyclerView.java

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
    final LayoutManager lm = getLayoutManager();
    if (lm == null) {
        return false;
    }//from   w w  w .ja v  a  2 s. c  o  m
    if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
        if (event.getAction() == MotionEventCompat.ACTION_SCROLL) {
            final float vScroll, hScroll;
            if (lm.canScrollVertically()) {
                vScroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
                if (!mMouseScrollDirectionDecider.isVerticalAvailable()) {
                    mMouseScrollDirectionDecider.guessDirection(event);
                }
            } else {
                vScroll = 0f;
            }
            if (lm.canScrollHorizontally()) {
                hScroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
                if (!mMouseScrollDirectionDecider.isHorizontalAvailable()) {
                    mMouseScrollDirectionDecider.guessDirection(event);
                }
            } else {
                hScroll = 0f;
            }
            if ((vScroll != 0 || hScroll != 0)) {
                final float scrollFactor = getScrollFactorBackport();
                float horizontalDirection = mMouseScrollDirectionDecider.getHorizontalDirection();
                float verticalDirection = mMouseScrollDirectionDecider.getVerticalDirection();
                final float hFactor = scrollFactor * (horizontalDirection != 0 ? horizontalDirection : -1);
                final float vFactor = scrollFactor * (verticalDirection != 0 ? verticalDirection : -1);
                scrollBy((int) (hScroll * hFactor), (int) (vScroll * vFactor));
            }
        }
    }
    return false;
}

From source file:com.afayear.android.client.view.TabPageIndicator.java

@Override
public boolean onGenericMotionEvent(final MotionEvent event) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1)
        return false;
    if (mTabProvider == null)
        return false;
    if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_SCROLL: {
            final float vscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
            if (vscroll < 0) {
                if (mCurrentItem + 1 < mTabProvider.getCount()) {
                    setCurrentItem(mCurrentItem + 1);
                }//w w w . jav  a2  s .c  om
            } else if (vscroll > 0) {
                if (mCurrentItem - 1 >= 0) {
                    setCurrentItem(mCurrentItem - 1);
                }
            }
        }
        }
    }
    return true;
}

From source file:com.example.android.visualgamecontroller.FullscreenActivity.java

/**
 * Get centered position for axis input by considering flat area.
 * /*from ww w  .  j  a v a2s  .c o m*/
 * @param event
 * @param device
 * @param axis
 * @return
 */
private float getCenteredAxis(MotionEvent event, InputDevice device, int axis) {
    InputDevice.MotionRange range = device.getMotionRange(axis, event.getSource());

    // A joystick at rest does not always report an absolute position of
    // (0,0). Use the getFlat() method to determine the range of values
    // bounding the joystick axis center.
    if (range != null) {
        float flat = range.getFlat();
        float value = event.getAxisValue(axis);

        // Ignore axis values that are within the 'flat' region of the
        // joystick axis center.
        if (Math.abs(value) > flat) {
            return value;
        }
    }
    return 0;
}

From source file:com.fishstix.dosboxfree.DBGLSurfaceView.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
private void processJoystickInput(MotionEvent event, int historyPos) {
    float hatX = 0.0f;
    InputDevice.MotionRange range = event.getDevice().getMotionRange(MotionEvent.AXIS_HAT_X, event.getSource());
    if (range != null) {
        if (historyPos >= 0) {
            hatX = InputDeviceState.ProcessAxis(range,
                    event.getHistoricalAxisValue(MotionEvent.AXIS_HAT_X, historyPos));
        } else {//from   www  .j a v a  2s  .c  o m
            hatX = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_HAT_X));
        }
    }

    float hatY = 0.0f;
    range = event.getDevice().getMotionRange(MotionEvent.AXIS_HAT_Y, event.getSource());
    if (range != null) {
        if (historyPos >= 0) {
            hatY = InputDeviceState.ProcessAxis(range,
                    event.getHistoricalAxisValue(MotionEvent.AXIS_HAT_Y, historyPos));
        } else {
            hatY = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_HAT_Y));
        }
    }

    float joyX = 0.0f;
    range = event.getDevice().getMotionRange(MotionEvent.AXIS_X, event.getSource());
    if (range != null) {
        if (historyPos >= 0) {
            joyX = InputDeviceState.ProcessAxis(range,
                    event.getHistoricalAxisValue(MotionEvent.AXIS_X, historyPos));
        } else {
            joyX = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_X));
        }
    }

    float joyY = 0.0f;
    range = event.getDevice().getMotionRange(MotionEvent.AXIS_Y, event.getSource());
    if (range != null) {
        if (historyPos >= 0) {
            joyY = InputDeviceState.ProcessAxis(range,
                    event.getHistoricalAxisValue(MotionEvent.AXIS_Y, historyPos));
        } else {
            joyY = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_Y));
        }
    }

    float joy2X = 0.0f;
    range = event.getDevice().getMotionRange(MotionEvent.AXIS_Z, event.getSource());
    if (range != null) {
        if (historyPos >= 0) {
            joy2X = InputDeviceState.ProcessAxis(range,
                    event.getHistoricalAxisValue(MotionEvent.AXIS_Z, historyPos));
        } else {
            joy2X = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_Z));
        }
    }

    float joy2Y = 0.0f;
    range = event.getDevice().getMotionRange(MotionEvent.AXIS_RZ, event.getSource());
    if (range != null) {
        if (historyPos >= 0) {
            joy2Y = InputDeviceState.ProcessAxis(range,
                    event.getHistoricalAxisValue(MotionEvent.AXIS_RZ, historyPos));
        } else {
            joy2Y = InputDeviceState.ProcessAxis(range, event.getAxisValue(MotionEvent.AXIS_RZ));
        }
    }

    if (mAnalogStickPref == 0) {
        mMouseThread.setCoord(
                (int) ((Math.abs(joyX * 32.0f) > DEADZONE) ? (-joyX * 32.0f * mMouseSensitivityX) : 0),
                (int) ((Math.abs(joyY * 32.0f) > DEADZONE) ? (-joyY * 32.0f * mMouseSensitivityY) : 0));
        DosBoxControl.nativeJoystick((int) ((joy2X * 256.0f) + mJoyCenterX),
                (int) ((joy2Y * 256.0f) + mJoyCenterY), ACTION_MOVE, -1);
    } else {
        mMouseThread.setCoord(
                (int) ((Math.abs(joy2X * 32.0f) > DEADZONE) ? (-joy2X * 32.0f * mMouseSensitivityX) : 0),
                (int) ((Math.abs(joy2Y * 32.0f) > DEADZONE) ? (-joy2Y * 32.0f * mMouseSensitivityY) : 0));
        DosBoxControl.nativeJoystick((int) ((joyX * 256.0f) + mJoyCenterX),
                (int) ((joyY * 256.0f) + mJoyCenterY), ACTION_MOVE, -1);
    }

    // Handle all other keyevents
    int value = 0;
    int tKeyCode = MAP_NONE;

    if (hatX < 0) {
        value = customMap.get(KeyEvent.KEYCODE_DPAD_LEFT);
        if (value > 0) {
            // found a valid mapping
            tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT));
            if (tKeyCode > MAP_NONE) {
                DosBoxControl.sendNativeKey(tKeyCode, true, mModifierCtrl, mModifierAlt, mModifierShift);
            }
        }
        hatXlast = hatX;
    } else if (hatX > 0) {
        value = customMap.get(KeyEvent.KEYCODE_DPAD_RIGHT);
        if (value > 0) {
            // found a valid mapping
            tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT));
            if (tKeyCode > MAP_NONE) {
                DosBoxControl.sendNativeKey(tKeyCode, true, mModifierCtrl, mModifierAlt, mModifierShift);
            }
        }
        hatXlast = hatX;
    } else {
        // released
        if (hatX != hatXlast) {
            if (hatXlast < 0) {
                value = customMap.get(KeyEvent.KEYCODE_DPAD_LEFT);
                if (value > 0) {
                    // found a valid mapping
                    tKeyCode = getMappedKeyCode(value,
                            new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_LEFT));
                    if (tKeyCode > MAP_NONE) {
                        DosBoxControl.sendNativeKey(tKeyCode, false, mModifierCtrl, mModifierAlt,
                                mModifierShift);
                    }
                }
            } else if (hatXlast > 0) {
                value = customMap.get(KeyEvent.KEYCODE_DPAD_RIGHT);
                if (value > 0) {
                    // found a valid mapping
                    tKeyCode = getMappedKeyCode(value,
                            new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_RIGHT));
                    if (tKeyCode > MAP_NONE) {
                        DosBoxControl.sendNativeKey(tKeyCode, false, mModifierCtrl, mModifierAlt,
                                mModifierShift);
                    }
                }
            }
        }
        hatXlast = hatX;
    }
    if (hatY < 0) {
        value = customMap.get(KeyEvent.KEYCODE_DPAD_UP);
        if (value > 0) {
            // found a valid mapping
            tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_UP));
            if (tKeyCode > MAP_NONE) {
                DosBoxControl.sendNativeKey(tKeyCode, true, mModifierCtrl, mModifierAlt, mModifierShift);
            }
        }
        hatYlast = hatY;
    } else if (hatY > 0) {
        value = customMap.get(KeyEvent.KEYCODE_DPAD_DOWN);
        if (value > 0) {
            // found a valid mapping
            tKeyCode = getMappedKeyCode(value, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_DOWN));
            if (tKeyCode > MAP_NONE) {
                DosBoxControl.sendNativeKey(tKeyCode, true, mModifierCtrl, mModifierAlt, mModifierShift);
            }
        }
        hatYlast = hatY;
    } else {
        // released
        if (hatY != hatYlast) {
            if (hatYlast < 0) {
                value = customMap.get(KeyEvent.KEYCODE_DPAD_UP);
                if (value > 0) {
                    // found a valid mapping
                    tKeyCode = getMappedKeyCode(value,
                            new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_UP));
                    if (tKeyCode > MAP_NONE) {
                        DosBoxControl.sendNativeKey(tKeyCode, false, mModifierCtrl, mModifierAlt,
                                mModifierShift);
                    }
                }
            } else if (hatYlast > 0) {
                value = customMap.get(KeyEvent.KEYCODE_DPAD_DOWN);
                if (value > 0) {
                    // found a valid mapping
                    tKeyCode = getMappedKeyCode(value,
                            new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_DOWN));
                    if (tKeyCode > MAP_NONE) {
                        DosBoxControl.sendNativeKey(tKeyCode, false, mModifierCtrl, mModifierAlt,
                                mModifierShift);
                    }
                }
            }
        }
        hatYlast = hatY;
    }
}

From source file:com.android.launcher2.PagedView.java

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
    if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_SCROLL: {
            // Handle mouse (or ext. device) by shifting the page depending on the scroll
            final float vscroll;
            final float hscroll;
            if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
                vscroll = 0;/*from w  ww. jav  a  2  s .c  o  m*/
                hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
            } else {
                vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
                hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
            }
            if (hscroll != 0 || vscroll != 0) {
                if (hscroll > 0 || vscroll > 0) {
                    scrollRight();
                } else {
                    scrollLeft();
                }
                return true;
            }
        }
        }
    }
    return super.onGenericMotionEvent(event);
}

From source file:com.hippo.widget.BothScrollView.java

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
    if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_SCROLL: {
            if (!mIsBeingDragged) {
                if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
                    final float hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
                    if (hscroll != 0) {
                        final int delta = (int) (hscroll * getHorizontalScrollFactor());
                        final int range = getHorizontalScrollRange();
                        int oldScrollX = getScrollX();
                        int newScrollX = oldScrollX + delta;
                        if (newScrollX < 0) {
                            newScrollX = 0;
                        } else if (newScrollX > range) {
                            newScrollX = range;
                        }/* www  .j ava2  s.  c o  m*/
                        if (newScrollX != oldScrollX) {
                            super.scrollTo(newScrollX, getScrollY());
                            return true;
                        }
                    }
                } else {
                    final float vscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
                    if (vscroll != 0) {
                        final int delta = (int) (vscroll * getVerticalScrollFactor());
                        final int range = getVerticalScrollRange();
                        int oldScrollY = getScrollY();
                        int newScrollY = oldScrollY - delta;
                        if (newScrollY < 0) {
                            newScrollY = 0;
                        } else if (newScrollY > range) {
                            newScrollY = range;
                        }
                        if (newScrollY != oldScrollY) {
                            super.scrollTo(getScrollX(), newScrollY);
                            return true;
                        }
                    }
                }
            }
        }
        }
    }
    return super.onGenericMotionEvent(event);
}

From source file:cc.flydev.launcher.Page.java

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
    if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_SCROLL: {
            // Handle mouse (or ext. device) by shifting the page depending on the scroll
            final float vscroll;
            final float hscroll;
            if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
                vscroll = 0;//from   ww w . j  ava2s .  c  om
                hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
            } else {
                vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
                hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
            }
            if (hscroll != 0 || vscroll != 0) {
                boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
                        : (hscroll > 0 || vscroll > 0);
                if (isForwardScroll) {
                    scrollRight();
                } else {
                    scrollLeft();
                }
                return true;
            }
        }
        }
    }
    return super.onGenericMotionEvent(event);
}

From source file:com.n2hsu.launcher.Page.java

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
    if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_SCROLL: {
            // Handle mouse (or ext. device) by shifting the page depending
            // on the scroll
            final float vscroll;
            final float hscroll;
            if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
                vscroll = 0;//  w  w  w  .  ja  v  a2 s.c o m
                hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
            } else {
                vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
                hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
            }
            if (hscroll != 0 || vscroll != 0) {
                boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0)
                        : (hscroll > 0 || vscroll > 0);
                if (isForwardScroll) {
                    scrollRight();
                } else {
                    scrollLeft();
                }
                return true;
            }
        }
        }
    }
    return super.onGenericMotionEvent(event);
}

From source file:com.amazon.appstream.fireclient.FireClientActivity.java

/**
 * A "generic motion event" includes joystick and mouse motion
 * when the mouse button isn't down. In our simple sample, we're
 * not handling the joystick, but this is where any such code
 * would live./*from  www .  j av a 2 s  .c  o  m*/
 *
 * This will only ever be called in HONEYCOMB_MR1 (12) or later, so I'm marking the
 * function using \@TargetApi to allow it to call the super.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override
public boolean dispatchGenericMotionEvent(MotionEvent event) {
    if (event.getSource() == InputDevice.SOURCE_MOUSE) {
        event.getPointerCoords(0, mCoordHolder);
        switch (event.getAction()) {
        case MotionEvent.ACTION_HOVER_MOVE:
            AppStreamInterface.mouseEvent((int) mCoordHolder.x, (int) mCoordHolder.y, 0);
            break;

        default:
            return super.dispatchGenericMotionEvent(event);
        }
        return true;
    } else if (event.getSource() == InputDevice.SOURCE_JOYSTICK) {
        Log.v(TAG, "Joystick event:" + event.toString());

        if (!mInitializedJoystick) {
            mInitializedJoystick = true;

            InputDevice joystick = InputDevice.getDevice(event.getDeviceId());
            InputDevice.MotionRange lThumbX = joystick.getMotionRange(MotionEvent.AXIS_X,
                    InputDevice.SOURCE_JOYSTICK);
            InputDevice.MotionRange lThumbY = joystick.getMotionRange(MotionEvent.AXIS_Y,
                    InputDevice.SOURCE_JOYSTICK);
            InputDevice.MotionRange rThumbX = joystick.getMotionRange(MotionEvent.AXIS_Z);
            InputDevice.MotionRange rThumbY = joystick.getMotionRange(MotionEvent.AXIS_RZ);
            InputDevice.MotionRange rTrigger = joystick.getMotionRange(MotionEvent.AXIS_GAS);
            if (rTrigger == null) {
                rTrigger = joystick.getMotionRange(MotionEvent.AXIS_RTRIGGER);
                mRTrigger = MotionEvent.AXIS_RTRIGGER;
            }

            InputDevice.MotionRange lTrigger = joystick.getMotionRange(MotionEvent.AXIS_BRAKE);
            if (lTrigger == null) {
                lTrigger = joystick.getMotionRange(MotionEvent.AXIS_LTRIGGER);
                mLTrigger = MotionEvent.AXIS_LTRIGGER;
            }

            List<InputDevice.MotionRange> ranges = joystick.getMotionRanges();

            InputDevice.MotionRange dPad = null;
            String name = joystick.getName();

            /*
            The Amazon Fire Game Controller follows the NVidia standard of sending
            AXIS_HAT_X/AXIS_HAT_Y results when the user hits the D-Pad. Only if we
            return false from dispatchGenericMotionEvent() will it then send
            DPAD keycodes.
                    
            But the most popular Android joystick on the market at this time, the
            Nyko Playpad Pro, returns AXIS_HAT_X/AXIS_HAT_Y results when the left
            analog stick hits its extremes, meaning that the analog stick will
            generate DPAD keys if we return false. The Nyko generates DPAD keys
            directly for the DPAD controller.
                    
            So we have two incompatible standards fighting with each other. Probably
            the safest thing to do would be to ask the user to press on their DPAD
            and see what messages we get, but that is beyond the scope of this
            example code.
            */
            if (name.equals("Amazon Fire Game Controler")) {
                for (int i = 0; i < ranges.size(); ++i) {
                    InputDevice.MotionRange range = ranges.get(i);
                    int axis = range.getAxis();

                    if (axis == MotionEvent.AXIS_HAT_X || axis == MotionEvent.AXIS_HAT_Y) {
                        dPad = ranges.get(i);
                        break;
                    }
                }
            }

            JoystickHelper.joystickDeadZones(lTrigger, rTrigger, lThumbX, lThumbY, rThumbX, rThumbY, dPad);
        }

        float lThumbX = event.getAxisValue(MotionEvent.AXIS_X);
        float lThumbY = -event.getAxisValue(MotionEvent.AXIS_Y);
        float rThumbX = event.getAxisValue(MotionEvent.AXIS_Z);
        float rThumbY = -event.getAxisValue(MotionEvent.AXIS_RZ);
        float lTrigger = event.getAxisValue(mLTrigger);
        float rTrigger = event.getAxisValue(mRTrigger);
        float hatX = event.getAxisValue(MotionEvent.AXIS_HAT_X);
        float hatY = event.getAxisValue(MotionEvent.AXIS_HAT_Y);

        JoystickHelper.setJoystickState(lTrigger, rTrigger, lThumbX, lThumbY, rThumbX, rThumbY, hatX, hatY);
        return true;
    }

    return super.dispatchGenericMotionEvent(event);
}

From source file:com.glview.widget.AbsListView.java

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
    if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_SCROLL: {
            if (mTouchMode == TOUCH_MODE_REST) {
                final float vscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
                if (vscroll != 0) {
                    final int delta = (int) (vscroll * getVerticalScrollFactor());
                    if (!trackMotionScroll(delta, delta)) {
                        return true;
                    }//from   w  ww. j  av  a2  s. c om
                }
            }
        }
        }
    }
    return super.onGenericMotionEvent(event);
}