Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A first order approximation would be to sum up the local length of every tiny piece of movement detected :</p> <p>On <code>ACTION_DOWN</code></p> <pre><code>total = 0; xPrec = ev.getX(); yPrec = ev.getY(); </code></pre> <p>On <code>ACTION_MOVE</code></p> <pre><code>final float dx = ev.getX() - xPrec; final float dy = ev.getY() - yPrec; final float dl = sqrt(dx * dx + dy * dy); total += dl; xPrec = ev.getX(); yPrec = ev.getY(); </code></pre> <p>On <code>ACTION_UP</code> you can do whatever you want with <code>total</code> which contains the total approximated length of your path.</p> <p>If you read the official documentation about <code>MotionEvent</code> <a href="http://developer.android.com/reference/android/view/MotionEvent.html" rel="nofollow">http://developer.android.com/reference/android/view/MotionEvent.html</a> you will see a section entitled <strong>Batching</strong> which explains that one given motion event can batch together multiple movement samples. For the best first order approximation you need to consume all those samples using <code>getHistorySize</code>, <code>getHistoricalX</code>, <code>getHistoricalY</code>. Don't forget to process the most recent sample which stands in <code>getX</code> and <code>getY</code>.</p> <p>If you need a better approximation I suggest you to read about the problem of curve fitting <a href="http://en.wikipedia.org/wiki/Curve_fitting" rel="nofollow">http://en.wikipedia.org/wiki/Curve_fitting</a>, but as the frequency of touch events is quite fast you may not need to do so and get satisfied with a first order approximation.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload