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

Slides:



Advertisements
Similar presentations
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Advertisements

Graphics with Canvas, SurfaceView, and multitouch processing (panning and multitouch zoom) GraphicsWithCanvas_2012.pptx.
Cosc 5/4730 Input Keyboard, touch, and Accelerometer.
ANDROID – GESTURES L. Grewe. What and why  a great way to interact with applications on mobile devices.  With a touch screen, users can easily tap,
Sensors.  Hardware devices that take measurements of the physical environment  Some examples  3-axis Accelerometer  3-axis Magnetic field sensor 
Touch & Gestures.  MotionEvents  Touch Handling  Gestures.
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
More Java: Encapsulation, Getters, Setters, Anonymous Class 1 CS300.
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
Introduction to Smartphone Sensors
Android sensors.
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
1 CGS1060 Mobile UIs Copyright 2012 by Janson Industries.
CS378 - Mobile Computing Sensing and Sensors. Sensors "I should have paid more attention in Physics 41" Most devices have built in sensors to measure.
CS378 - Mobile Computing Sensing and Sensors. Sensors "I should have paid more attention in Physics 41" Most devices have built in sensors to measure.
Android Sensors & Async Callbacks Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
PROG Mobile Java Application Development PROG Mobile Java Application Development Event Handling Creating Menus.
Cosc 5/4730 Input Keyboard, touch, and Accelerometer.
Mobile Application Development Selected Topics – CPIT 490
Lecture # 9 Hardware Sensor. Topics 2  SensorManger & Sensor  SensorEvent & SensorEventListener  Example Application.
Sensing. Possible sensors on devices – Documented in SensorEvent class Accelerometer (m/s 2 ) – acceleration in x, y, z axes Magnetic Field (micro Tesla)
Doodlz App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Linear Layout, Screen Support, and Events. Linear Layout Supports 2 orientations: 1.Horizontal 2.Vertical I often get confused with how each orientation.
1 CMSC 628: Introduction to Mobile Computing Nilanjan Banerjee Introduction to Mobile Computing University of Maryland Baltimore County
BlackBerry Event Handling. Overview Introduction Typical Application Model Event Listeners Responding to UI Events Touch Screen Events Touch Screen Gestures.
Sensors – Part I SE 395/595.
CS378 - Mobile Computing Sensing and Sensors Part 2.
Resources and RelativeLayouts. Resources Android Resources We’ve already talked about the different types of Android Resources DirectoryResource Type.
Mobile Programming Lecture 12 HierarchyViewer, Linkify, Gestures, and Version Control.
Android Boot Camp Demo Application – Part 1. Development Environment Set Up Download and install Java Development Kit (JDK) Download and unzip Android.
Android Using Menus Notes are based on: The Busy Coder's Guide to Android Development by Mark L. Murphy Copyright © CommonsWare, LLC. ISBN:
Video Games list lab 6  At the end of this lab you will be expected to know:  What Views, View Groups, Layouts, and Widgets are and how they relate to.
Android View Stuff. TextViews Display text Display images???
Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.
CS378 - Mobile Computing More UI - Part 2. Special Menus Two special application menus – options menu – context menu Options menu replaced by action bar.
Sensors Nasrullah Khan Niazi. Using Device Sensors The Android SDK provides access to raw data from sensors on the device.The sensors,and their precision.
Sensors For Mobile Phones  Ambient Light Sensor  Proximity Sensor  GPS Receiver Sensor  Gyroscope Sensor  Barometer Sensor  Accelerometer Sensor.
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors Date: Feb 11, 2016.
Events. Slide 2©SoftMoore Consulting Events Events are generated when a user interacts with the view objects of an application. Examples –button clicked–
CHAPTER 7 TouchGestures. Chapter objectives: To code the detection of and response to touch gestures. Patterns of common touches to create touch gestures.
CPE 490/590 – Smartphone Development
CHAPTER 8 Sensors and Camera. Chapter objectives: Understand Motion Sensors, Environmental Sensors and Positional Sensors Learn how to acquire measurement.
User Interaction Radan Ganchev Astea Solutions. Content Basic input events Gestures Drag and drop Sensors.
CS371m - Mobile Computing Sensing and Sensors.
The Doodlz app enables you to paint by dragging one or more fingers across the screen. The app provides options for setting the drawing color.
Android Android Sensors Android Sensors: – Accelerometer – Gravity sensor – Linear Acceleration sensor – Magnetic Field sensor – Orientation.
Sensors in Android.
Vijay Kumar Kolagani Dr. Yingcai Xiao
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors
Google VR (gvr) CardBoard and DayDream With OpenGL
TUTORIAL ON MULTITOUCH AND SWIPE GESTURES
Applets.
Android Bluetooth Game Controllers
Linear Layout, Screen Support, and Events
Android – Event Handling
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors.
IF statements.
Sensors, maps and fragments:
Vijay Kumar Kolagani Dr. Yingcai Xiao
CS499 – Mobile Application Development
Android 16: Input Events Kirk Scott.
Mobile Computing With Android ACST 4550 Android Logs and Gestures
CS371m - Mobile Computing Sensing and Sensors.
Vijay Kumar Kolagani Dr. Yingcai Xiao
Android Developer Fundamentals V2
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors.
Mobile Programming Sensors in Android.
Interactive Graphics in Android
Mobile Programming Gestures in Android.
Game Programming Algorithms and Techniques
Presentation transcript:

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.

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

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 – html for a full list. html

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 boolean onTouch(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 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…

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: – 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.

“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) android.hardware.SensorManager private SensorManager myManager; private Sensor accSensor; private List 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 &