14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Absolute screen coordinates.<br />

int xPosRaw = (int)event.getRawX();<br />

int yPosRaw = (int)event.getRawY();<br />

Chapter 11: Advanced Android <strong>Development</strong><br />

The Motion Event parameter also includes the pressure being applied to the screen using getPressure,<br />

a method that returns a value usually between 0 (no pressure) and 1 (normal pressure).<br />

Depending on the calibration of the hardware, it’s possible to return values greater than 1.<br />

Finally, you can also determine the normalized size of the current contact area using the getSize<br />

method. This method returns a value between 0 and 1, where 0 suggests a very precise measurement<br />

and 1 indicates a possible “fat touch” event in which the user may not have intended to press anything.<br />

Tracking Movement<br />

Whenever the current touch contact position, pressure, or size changes, a new onTouchEvent is triggered<br />

with an ACTION_MOVE action.<br />

As well as the fi elds described previously, the Motion Event parameter can include historical values.<br />

This history represents all the movement events that have occurred between the previous onTouchEvent<br />

and this one, allowing Android to buffer rapid movement changes to provide fi ne-grained capture<br />

of movement data.<br />

You can fi nd the size of the history by calling getHistorySize, which returns the number of movement<br />

positions available for the current event. You can then obtain the times, pressures, sizes, and positions<br />

of each of the historical events, using a series of getHistorical* methods and passing in the position<br />

index, as shown in the following code snippet:<br />

int historySize = event.getHistorySize();<br />

for (int i = 0; i < historySize; i++) {<br />

long time = event.getHistoricalEventTime(i);<br />

float pressure = event.getHistoricalPressure(i);<br />

float x = event.getHistoricalX(i);<br />

float y = event.getHistoricalY(i);<br />

float size = event.getHistoricalSize(i);<br />

}<br />

// TODO: Do something with each point<br />

The normal pattern used for handling movement events is to process each of the historical events fi rst,<br />

followed by the current Motion Event values, as shown in the following code snippet:<br />

@Override<br />

public boolean onTouchEvent(MotionEvent event) {<br />

int action = event.getAction();<br />

switch (action) {<br />

case (MotionEvent.ACTION_MOVE)<br />

{<br />

int historySize = event.getHistorySize();<br />

395

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!