Improve GalleryView
This commit is contained in:
1 parent
dff16b3834
commit
ffd122c105
29 files changed
+1952
-1791
No files matched your search
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright 2016 Hippo Seven
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.hippo.util;
|
||||
|
||||
import android.view.InputDevice;
|
||||
import android.view.InputEvent;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
// From http://developer.android.com/training/game-controllers/controller-input.html#dpad
|
||||
public class Dpad {
|
||||
|
||||
public static final int INVALID = -1;
|
||||
public static final int UP = 0;
|
||||
public static final int LEFT = 1;
|
||||
public static final int RIGHT = 2;
|
||||
public static final int DOWN = 3;
|
||||
public static final int CENTER = 4;
|
||||
|
||||
public static int getDirectionPressed(InputEvent event) {
|
||||
if (!isDpadDevice(event)) {
|
||||
return INVALID;
|
||||
}
|
||||
|
||||
int directionPressed = INVALID;
|
||||
|
||||
// If the input event is a MotionEvent, check its hat axis values.
|
||||
if (event instanceof MotionEvent) {
|
||||
|
||||
// Use the hat axis value to find the D-pad direction
|
||||
MotionEvent motionEvent = (MotionEvent) event;
|
||||
float xaxis = motionEvent.getAxisValue(MotionEvent.AXIS_HAT_X);
|
||||
float yaxis = motionEvent.getAxisValue(MotionEvent.AXIS_HAT_Y);
|
||||
|
||||
// Check if the AXIS_HAT_X value is -1 or 1, and set the D-pad
|
||||
// LEFT and RIGHT direction accordingly.
|
||||
if (Float.compare(xaxis, -1.0f) == 0) {
|
||||
directionPressed = Dpad.LEFT;
|
||||
} else if (Float.compare(xaxis, 1.0f) == 0) {
|
||||
directionPressed = Dpad.RIGHT;
|
||||
}
|
||||
// Check if the AXIS_HAT_Y value is -1 or 1, and set the D-pad
|
||||
// UP and DOWN direction accordingly.
|
||||
else if (Float.compare(yaxis, -1.0f) == 0) {
|
||||
directionPressed = Dpad.UP;
|
||||
} else if (Float.compare(yaxis, 1.0f) == 0) {
|
||||
directionPressed = Dpad.DOWN;
|
||||
}
|
||||
}
|
||||
|
||||
// If the input event is a KeyEvent, check its key code.
|
||||
else if (event instanceof KeyEvent) {
|
||||
|
||||
// Use the key code to find the D-pad direction.
|
||||
KeyEvent keyEvent = (KeyEvent) event;
|
||||
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
|
||||
directionPressed = Dpad.LEFT;
|
||||
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
|
||||
directionPressed = Dpad.RIGHT;
|
||||
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
|
||||
directionPressed = Dpad.UP;
|
||||
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
|
||||
directionPressed = Dpad.DOWN;
|
||||
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
|
||||
directionPressed = Dpad.CENTER;
|
||||
}
|
||||
}
|
||||
return directionPressed;
|
||||
}
|
||||
|
||||
public static boolean isDpadDevice(InputEvent event) {
|
||||
// Check that input comes from a device with directional pads.
|
||||
if ((event.getSource() & InputDevice.SOURCE_DPAD)
|
||||
!= InputDevice.SOURCE_DPAD) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user