Download presentation
Presentation is loading. Please wait.
Published byBrice Palmer Modified over 9 years ago
1
Cosc 5/4730 Input Keyboard, touch, and Accelerometer
2
View For most input, you add a custom listener to a View class – EditView has a keyboard listener, since it accepts input. You add a listener to a View (widgets) and then it only work for that widget – You can’t have a listener for a “screen” Unless the view takes up the entire screen – The exception is the Sensor, which is for the device. – Most of the “activity” classes have builtin Touch and key listener. We’ll come back to this later.
3
View (2) There is a View class that you can extend and use to create custom Views For the purpose of this lecture, we’ll use an ImageView widget and add listeners – View and SurfaceView will be covered later on.
4
A Note View will need several attributed in order to work with these listeners In the xml you will need to add android:focusable="true" android:focusableInTouchMode="true" android:clickable="true"
5
View.OnKeyListener Implement the View.OnKeyListener And override – public boolean onKey(View v, int keyCode, KeyEvent event) – Note, that there is not a char field here. You get the KeyCode as a parameter or event.getKeyCode()
6
View.OnKeyListener (2) What key was pressed? – Use event.getMatch(char[] chars) Pass it an array of characters you want to test against. If it matches, then returns that character – Else returns ‘\0’ – Use keycode == KeyEvent. Constants Example: KeyEvent.KEYCODE_0 for Zero Gives you access to any key that was pushed: – KEYCODE_CAMERA, KEYCODE_DPAD_LEFT – KEYCODE_ENDCALL, KEYCODE_VOLUME_DOWN, etc – http://developer.android.com/reference/android/view/KeyEvent. html for a full list. http://developer.android.com/reference/android/view/KeyEvent. html
7
ViewOnKeyListener (3) Add the listener to the view ImageView in our case iv.setOnKeyListener(new myKeyListener()); When the ImageView has focus, then the keylistener will be called for any key events.
8
View Overrides When extending a View class you also can override several more – You also override them in an activity as well. onKeyDown(int, KeyEvent) – Called when a new key event occurs. onKeyUp(int, KeyEvent) – Called when a key up event occurs. onTrackballEvent(MotionEvent) – Called when a trackball motion event occurs. There is also a touchEvent
9
Touch Events. There is an OnTouchListener But you can also use the OnClickListener and OnLongClickListener as well. – Normally associated with buttons. – Not that OnTouchListeners appear to be call first, so if you don’t consume the event, then the click and/or LongClick are called.
10
View.OnTouchListener Implement the View.OnTouchListener And override – public boolean onTouch(View v, MotionEvent event) return true if event consumed, false otherwise. – the event has all the information about the touch event.
11
View.OnTouchListener (2) MotionEvent – getX(), getY() returns the X, Y location of the touch in the Widget – Not the position on the screen. – getRawX(), getRawY() returns the original raw X and Y coordinate, which is the position on the screen. – getAction() Return the kind of action being performed – one of either ACTION_DOWN, ACTION_MOVE, ACTION_UP, or ACTION_CANCEL.
12
Gesture events. There is a GestureDetector and Gesture.SimpleOnGestureListener() – From everything I’ve seen, you declare a OnTouchListener, that then calls a SimpleOnGestureListener with the Gestures you are interested in. example: public boolean onTouch(View v, MotionEvent event) { if (myGestureDetector.onTouchEvent(event)) return true; //gesture detector consumed the event int action = event.getAction(); //check for touch ACTION_MOVE, etc…
13
SimpleOnGestureListener() On previous slide the myGestureDetector extends the class, instead of implement, since I was looking for only onFling event for swipes There are two interfaces, you can implement to use a “real” listener – GestureDetector.OnDoubleTapListener The listener that is used to notify when a double-tap or a confirmed single-tap occur. – GestureDetector.OnGestureListener The listener that is used to notify when gestures occur.
14
SimpleOnGestureListener() Extend (only the ones you want) or implement the following methods: – boolean onDown(MotionEvent e) Notified when a tap occurs with the down MotionEvent that triggered it. – boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) Notified of a fling event when it occurs with the initial on down MotionEvent and the matching up MotionEvent. – void onLongPress(MotionEvent e) Notified when a long press occurs with the initial on down MotionEvent that trigged it. – boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) Notified when a scroll occurs with the initial on down MotionEvent and the current move MotionEvent. – void onShowPress(MotionEvent e) The user has performed a down MotionEvent and not performed a move or up yet. – boolean onSingleTapUp(MotionEvent e) Notified when a tap occurs with the up MotionEvent that triggered it.
15
“Swipe” event using onFling you can do the math to figure out a swipe event across the view Example for Right Swipe: float dX = e2.getX()-e1.getX(); if (Math.abs(dY)<SWIPE_MAX_OFF_PATH && Math.abs(velocityX)>=SWIPE_THRESHOLD_VELOCITY && Math.abs(dX)>=SWIPE_MIN_DISTANCE ) { if (dX>0) { //Right Swipe }
16
Touch, Gesture, and swipes. To see how all the code for touch, gestures, and swipes works together – see the associated code with the lecture.
17
Touch and keys for the “screen” An activity has onTouchEvent(MotionEvent event) and key methods built in. You can over ride them. – Note, you will need to be careful about do this because of the other widgets maybe need them. Example: If you consume the key events, then how does the EditText work?
18
Full screen gestures Implement OnGuestureListener and OnDoubleTapListenver (if you want) – Using the support v4 library as well. private GestureDetectorCompat mDetector; GestureDetector.OnGestureListener mDetector = new GestureDetectorCompat(this,this); Set the gesture detector as the double tap listener. mDetector.setOnDoubleTapListener(this);
19
Full screen gestures(2) We still have to use the onTouchEvent method. @Override public boolean onTouchEvent(MotionEvent event){ this.mDetector.onTouchEvent(event); // Be sure to call the superclass implementation return super.onTouchEvent(event); } Now you can detect and deal with gestures that are across multiple widgets, such as Swipes/Flings.
20
Example code Input goes though most of the input using widgets Input2 sets them for the “screen”. – The code is less in depth and just toasts and logs results.
21
Sensor(s) Android is built with ability to handle many sensors – ACCELEROMETER accelerometer sensor, which is the acceleration movement of the phone. – GYROSCOPE a gyroscope sensor – LIGHT a light sensor – MAGNETIC_FIELD a magnetic field sensor. – ORIENTATION Orientation is space – PRESSURE a pressure sensor – PROXIMITY an proximity sensor – TEMPERATURE A temperature sensor
22
Android Sensor packages are – android.hardware.Sensor; – android.hardware.SensorEvent; – android.hardware.SensorEventListener; – android.hardware.SensorManager; (Easter egg) android.hardware.SensorManager private SensorManager myManager; private Sensor accSensor; private List sensors; SensorEventListener mySensorListener;
23
Orientation First we need to get the a Sensor Manager for the sensors myManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); Now we have two methods to get the Accelerometer – GetDefaultSensor, which may return a sensor could be a composite sensor – Or to get the raw sensor, get a list of the Sensors for that TYPE and then choose one (normally the first one.)
24
Orientation (2) Example: sensors = myManager.getSensorList(Sensor.TYPE_ ORIENTATION ); if(sensors.size() > 0) accSensor = sensors.get(0); OR accSensor = myManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); – A Note: Google states this sensor type exists for legacy reasons please use getRotationMatrix() in conjunction with remapCoordinateSystem() and getOrientation() to compute these values instead. This option is shown in the pitchroll2 example on the handout page.
25
Orientation (2) Create a call back listener for the Sensor. – You can use the same listener for more then one sensor. – SensorEventListener – Override the two methods public void onAccuracyChanged(Sensor sensor, int accuracy) – Called when the accuracy changes. public void onSensorChanged(SensorEvent event) – Called when the Sensor data changes.
26
SensorEvent The data is a SensorEvent is based on the Sensor TYPE (ORIENTATION, ACELEROMETER, etc), – values[] contains the data – Sensor.TYPE_ORIENTATION: All values are angles in degrees. values[0]: Azimuth, angle between the magnetic north direction and the Y axis, around the Z axis (0 to 359). 0=North, 90=East, 180=South, 270=West values[1]: Pitch, rotation around X axis (-180 to 180), with positive values when the z-axis moves toward the y-axis. values[2]: Roll, rotation around Y axis (-90 to 90), with positive values when the x-axis moves toward the z-axis. Important note: For historical reasons the roll angle is positive in the clockwise direction (mathematically speaking, it should be positive in the counter-clockwise direction). – Sensor.TYPE_ACCELEROMETER: All values are in SI units (m/s^2) and measure the acceleration applied to the phone minus the force of gravity. values[0]: Acceleration minus Gx on the x-axis values[1]: Acceleration minus Gy on the y-axis values[2]: Acceleration minus Gz on the z-axis
27
Orientation (3) Lastly add the listener – myManager.registerListener(mySensorListener, accSensor, SensorManager.SENSOR_DELAY_GAME); Where AccSensor is the Sensor SENSOR_DELAY_GAME is a suitable time interval for games, which SENSOR_DELAY_NORMAL is for applications, and SENSOR_DELAY_UI is for “screen flipping”.
28
And lastly… Don’t forget to unregister the listener when you are done (free memory and save battery life) – myManager.unregisterListener(mySensorListener);
29
Screen: portrait and landscape Remember when using the sensors that you screen may change from landscape to portrait and vise versa. – In the XML you can set the landscape, portrait to prevent “screen flipping” for each activity android:screenOrientation="portrait” android:screenOrientation=“landscape"
30
Q A &
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.