Cosc 5/4730

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

description

Cosc 5/4730. Input Keyboard, touch, and Accelerometer . 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 - PowerPoint PPT Presentation

Transcript of Cosc 5/4730

Page 1: Cosc  5/4730

Cosc 5/4730

InputKeyboard, touch, and Accelerometer

Page 2: Cosc  5/4730

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.

Page 3: Cosc  5/4730

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

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

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

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

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

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

Page 9: Cosc  5/4730

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

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

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

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

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

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

“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

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

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?

Page 18: Cosc  5/4730

Full screen gestures

• Implement OnGuestureListener and OnDoubleTapListenver (if you want)– Using the support v4 library as well.private GestureDetectorCompat mDetector;

• GestureDetector.OnGestureListenermDetector = new GestureDetectorCompat(this,this);• Set the gesture detector as the double tap

listener.mDetector.setOnDoubleTapListener(this);

Page 19: Cosc  5/4730

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.

Page 20: Cosc  5/4730

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.

Page 21: Cosc  5/4730

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

Page 22: Cosc  5/4730

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 23: Cosc  5/4730

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 24: Cosc  5/4730

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 25: Cosc  5/4730

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 26: Cosc  5/4730

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 27: Cosc  5/4730

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 28: Cosc  5/4730

And lastly…

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

Page 29: Cosc  5/4730

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"

Page 30: Cosc  5/4730

QA&