<?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; motion</title>
	<atom:link href="http://www.codeshogun.com/blog/tag/motion/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>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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 motion, one [...]]]></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>8</slash:comments>
		</item>
		<item>
		<title>How to implement Swipe action in Android</title>
		<link>http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/</link>
		<comments>http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 02:06:12 +0000</pubDate>
		<dc:creator>Android Shogun</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[gesture]]></category>
		<category><![CDATA[motion]]></category>
		<category><![CDATA[swipe]]></category>
		<category><![CDATA[view flipper]]></category>

		<guid isPermaLink="false">http://www.codeshogun.com/blog/?p=97</guid>
		<description><![CDATA[To implement swipe action in Android, such as the one used in iPhone homescreen unlock, actually Android SDK provides native support for gestures.  Swipe is actually called Fling ^_^
You will need to extend SimpleOnGestureListener to implement your own handling on swipe/fling action:

class MyGestureDetector extends SimpleOnGestureListener {
        @Override
 [...]]]></description>
			<content:encoded><![CDATA[<p>To implement swipe action in Android, such as the one used in iPhone homescreen unlock, actually Android SDK provides native support for gestures.  Swipe is actually called Fling ^_^</p>
<p>You will need to extend SimpleOnGestureListener to implement your own handling on swipe/fling action:<br />
<code><br />
class MyGestureDetector extends SimpleOnGestureListener {<br />
        @Override<br />
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {<br />
        }<br />
</code></p>
<p>To determine if it&#8217;s a valid swipe, you will need to make sure the fling falls into an almost straight path, with at least certain length of touch duration, so it&#8217;s a continuous touch, and with certain velocity of course.</p>
<p>So let&#8217;s define some constants:<br />
<code><br />
private static final int SWIPE_MIN_DISTANCE = 120;<br />
private static final int SWIPE_MAX_OFF_PATH = 250;<br />
private static final int SWIPE_THRESHOLD_VELOCITY = 200;<br />
</code></p>
<p>The max off path is to make sure the fling still falls within a somewhat straight path.</p>
<p>If onFling() method, you can do this:<br />
<code><br />
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE &#038;&#038; Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {<br />
                	viewFlipper.setInAnimation(slideLeftIn);<br />
                    viewFlipper.setOutAnimation(slideLeftOut);<br />
                	viewFlipper.showNext();<br />
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE &#038;&#038; Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {<br />
                	viewFlipper.setInAnimation(slideRightIn);<br />
                    viewFlipper.setOutAnimation(slideRightOut);<br />
                	viewFlipper.showPrevious();<br />
                }<br />
</code></p>
<p>The viewFlipper is used to show how fling/swipe make the view animation/transition from one to the other.</p>
<p>At last, you need to make sure in your activity, you catch the gesture event by overriding onTouch() method:<br />
<code><br />
@Override<br />
    public boolean onTouchEvent(MotionEvent event) {<br />
        if (gestureDetector.onTouchEvent(event))<br />
	        return true;<br />
	    else<br />
	    	return false;<br />
    }<br />
</code></p>
<p>Here&#8217;s a screenshot of the view in transition:<br />
<div id="attachment_98" class="wp-caption aligncenter" style="width: 330px"><a href="http://www.codeshogun.com/blog/wp-content/uploads/2009/04/swipe.png"><img src="http://www.codeshogun.com/blog/wp-content/uploads/2009/04/swipe.png" alt="Android Swipe View Flipper in Action" title="swipe" width="320" height="480" class="size-full wp-image-98" /></a><p class="wp-caption-text">Android Swipe View Flipper in Action</p></div></p>
<p>The sample source is attached <a href="http://codeshogun.com/apps/SwipeSample.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
	</channel>
</rss>
