<?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; view flipper</title>
	<atom:link href="http://www.codeshogun.com/blog/tag/view-flipper/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 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>
