<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code Shogun &#187; shake</title>
	<atom:link href="http://www.codeshogun.com/blog/tag/shake/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codeshogun.com/blog</link>
	<description>Mobile App Dev</description>
	<lastBuildDate>Wed, 02 Jun 2010 01:45:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to detect shake motion in Android &#8211; part I</title>
		<link>http://www.codeshogun.com/blog/2009/04/17/how-to-detect-shake-motion-in-android-part-i/</link>
		<comments>http://www.codeshogun.com/blog/2009/04/17/how-to-detect-shake-motion-in-android-part-i/#comments</comments>
		<pubDate>Sat, 18 Apr 2009 01:57:34 +0000</pubDate>
		<dc:creator>Android Shogun</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[motion]]></category>
		<category><![CDATA[shake]]></category>

		<guid isPermaLink="false">http://www.codeshogun.com/blog/?p=104</guid>
		<description><![CDATA[Gesture/Motion-based detection is fast becoming an integral part of many mobile applications. One neat usage of it is to shake to erease.  The first one I&#8217;ve encountered is SketchUp from the early days of jailbreak iPhone.  Then there are shake to delete done-items on your todo/shopping list, etc. There are many approaches to detect shake [...]]]></description>
			<content:encoded><![CDATA[<p>Gesture/Motion-based detection is fast becoming an integral part of many mobile applications.</p>
<p>One neat usage of it is to shake to erease.  The first one I&#8217;ve encountered is SketchUp from the early days of jailbreak iPhone.  Then there are shake to delete done-items on your todo/shopping list, etc.</p>
<p>There are many approaches to detect shake motion, one is from <a href="http://www.clingmarks.com/?p=25" target="_blank">ClingMarks</a>.  Basically it calculates the total delta (changes) in all 3 axises: x, y and z between the current even time and the previous one, and divide by the time elapsed to find its psudo-velocity.  (I call it psudo because it&#8217;s not actually a velocity in any direction; it&#8217;s more like a mixed-up velocity from 3 axises).  If this velocity is greater than a pre-defined threshold, it&#8217;s been viewed as a shake motion.</p>
<p>From the code point of view, you need to implement the SensorListener:<br />
<code><br />
public class ShakeActivity extends Activity implements SensorListener<br />
</code></p>
<p>You will need to acquire a SensorManager:<br />
<code><br />
sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);<br />
</code></p>
<p>And register this sensor with desired flags:<br />
<code><br />
ensorMgr.registerListener(this,<br />
SensorManager.SENSOR_ACCELEROMETER,<br />
SensorManager.SENSOR_DELAY_GAME);<br />
</code></p>
<p>In your onSensorChange() method, you determine whether it&#8217;s a shake or not:<br />
<code><br />
public void onSensorChanged(int sensor, float[] values) {<br />
if (sensor == SensorManager.SENSOR_ACCELEROMETER) {<br />
long curTime = System.currentTimeMillis();<br />
// only allow one update every 100ms.<br />
if ((curTime - lastUpdate) &gt; 100) {<br />
long diffTime = (curTime - lastUpdate);<br />
lastUpdate = curTime;</code></p>
<p>x = values[SensorManager.DATA_X];<br />
y = values[SensorManager.DATA_Y];<br />
z = values[SensorManager.DATA_Z];</p>
<p>float speed = Math.abs(x+y+z &#8211; last_x &#8211; last_y &#8211; last_z) / diffTime * 10000;</p>
<p>if (speed &gt; SHAKE_THRESHOLD) {<br />
Log.d(&#8220;sensor&#8221;, &#8220;shake detected w/ speed: &#8221; + speed);<br />
Toast.makeText(this, &#8220;shake detected w/ speed: &#8221; + speed, Toast.LENGTH_SHORT).show();<br />
}<br />
last_x = x;<br />
last_y = y;<br />
last_z = z;<br />
}<br />
}<br />
}</p>
<p>The shake threshold is defined as:<br />
<code><br />
private static final int SHAKE_THRESHOLD = 800;<br />
</code></p>
<p>This is pretty straightforward.  So if your app just simply needs a shake motion detection, w/o needing to know on which direction it&#8217;s shaken, this code is good enough.  However, there are situations where a non-shake motion would be detected as shake due to the fact that the formula is not actually the correct or should I say a precise formula for determining the shake motion.  I call this a coarse-grain approach.</p>
<p>In my next post, I will show you a more fine-grain approach, something I learned from iPhone SDK programming <img src='http://www.codeshogun.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>You can download this sample <a href="http://codeshogun.com/apps/ShakeSample.zip">here</a></p>
<p>Happy Coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codeshogun.com/blog/2009/04/17/how-to-detect-shake-motion-in-android-part-i/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

