260 likes | 375 Views
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.
E N D
Cosc5/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.
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.
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"
View.OnKeyListener • Implement the View.OnKeyListener • And override • public booleanonKey(View v, intkeyCode, KeyEvent event) • Note, that there is not a char field here. You get the KeyCode as a parameter or event.getKeyCode()
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.
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.
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.
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.
View.OnTouchListener • Implement the View.OnTouchListener • And override • public booleanonTouch(View v, MotionEvent event) • return true if event consumed, false otherwise. • the event has all the information about the touch event.
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.
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 booleanonTouch(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…
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.
SimpleOnGestureListener() • Extend (only the ones you want) or implement the following methods: • booleanonDown(MotionEvente) • Notified when a tap occurs with the down MotionEvent that triggered it. • booleanonFling(MotionEvente1, 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(MotionEvente) • Notified when a long press occurs with the initial on down MotionEvent that trigged it. • booleanonScroll(MotionEvente1, MotionEvent e2, float distanceX, float distanceY) • Notified when a scroll occurs with the initial on down MotionEvent and the current move MotionEvent. • void onShowPress(MotionEvente) • The user has performed a down MotionEvent and not performed a move or up yet. • booleanonSingleTapUp(MotionEvente) • Notified when a tap occurs with the up MotionEvent that triggered it.
“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 }
Touch, Gesture, and swipes. • To see how all the code for touch, gestures, and swipes works together • see the associated code with the lecture.
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
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;
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.)
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.
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.
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
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”.
And lastly… • Don’t forget to unregister the listener when you are done (free memory and save battery life) • myManager.unregisterListener(mySensorListener);
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"
Q A &