Top Banner
Cosc 5/4730 Input Keyboard, touch, and Accelerometer
26

Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

Cosc 5/4730

InputKeyboard, touch, and Accelerometer

Page 2: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

View

• For most input, you add a custom listener to a View class– EditView has its own already for keyboards, since

it accepts input.• Unlike blackberries where you add listeners to

the “Screen”, android add listener to a View (widgets) and they only work for that widget– The exception is the Sensor, which is for the

device.

Page 3: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

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.

Page 4: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

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"

Page 5: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

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()

Page 6: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

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.

Page 7: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

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.

Page 8: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

View Overrides

• When extending a View class you also can override several more

• 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.

Page 9: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

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.

Page 10: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

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.

Page 11: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

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.

Page 12: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

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…

Page 13: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

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.

Page 14: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

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.

Page 15: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

“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 }

Page 16: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

Touch, Gesture, and swipes.

• To see how all the code for touch, gestures, and swipes works together– see the associated code with the lecture.

Page 17: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

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, (like the blackberry accelerometer)

– PRESSURE• a pressure sensor

– PROXIMITY • an proximity sensor

– TEMPERATURE• A temperature sensor

Page 18: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

Android Sensor

• packages are – android.hardware.Sensor;– android.hardware.SensorEvent;– android.hardware.SensorEventListener;– android.hardware.SensorManager; (Easter egg)

private SensorManager myManager;private Sensor accSensor;private List<Sensor> sensors;SensorEventListener mySensorListener;

Page 19: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

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.)

Page 20: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

Orientation (2)

• Example:sensors = myManager.getSensorList(Sensor.TYPE_ORIENTATION);if(sensors.size() > 0)

accSensor = sensors.get(0);

• ORaccSensor = 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.

Page 21: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

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.

Page 22: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

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

Page 23: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

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”.

Page 24: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

And lastly…

• Don’t forget to unregister the listener when you are done (free memory and save battery life)– myManager.unregisterListener(mySensorListener);

Page 25: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

Screen: portrait and landscape

• Remember when using the sensors that you screen may change from landscape to portrait and vis versa.– In the XML you can set the landscape, portrait to

prevent “screen flipping” for each activity• android:screenOrientation="portrait”• android:screenOrientation=“landscape"

Page 26: Cosc 5/4730 Input Keyboard, touch, and Accelerometer.

QA&