Saturday, December 28, 2013

VelocityTracker, track the velocity of touch events

It's a example of using Android's VelocityTracker. It's a helper for tracking the velocity of touch events, for implementing flinging and other such gestures. Use obtain() to retrieve a new instance of the class when you are going to begin tracking. Put the motion events you receive into it with addMovement(MotionEvent). When you want to determine the velocity call computeCurrentVelocity(int) and then call getXVelocity(int) and getYVelocity(int) to retrieve the velocity for each pointer id.


VelocityTracker
Example of using Android's VelocityTracker

package com.example.androidvelocitytracker;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {

 TextView textAvtion, textVelocityX, textVelocityY, 
  textMaxVelocityX, textMaxVelocityY;
 VelocityTracker velocityTracker = null;
 
 float maxXVelocity;
 float maxYVelocity;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  textAvtion = (TextView) findViewById(R.id.action);
  textVelocityX = (TextView) findViewById(R.id.velocityx);
  textVelocityY = (TextView) findViewById(R.id.velocityy);
  textMaxVelocityX = (TextView) findViewById(R.id.maxvelocityx);
  textMaxVelocityY = (TextView) findViewById(R.id.maxvelocityy);
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  
  int action = event.getActionMasked();

  switch (action) {
  case MotionEvent.ACTION_DOWN:
   if (velocityTracker == null) {
    velocityTracker = VelocityTracker.obtain();
   } else {
    velocityTracker.clear();
   }
   velocityTracker.addMovement(event);
   maxXVelocity = 0; 
   maxYVelocity = 0;
   
   textVelocityX.setText("X-velocity (pixel/s): 0");
   textVelocityY.setText("Y-velocity (pixel/s): 0");
   textMaxVelocityX.setText("max. X-velocity: 0");
   textMaxVelocityY.setText("max. Y-velocity: 0");
   
   break;
  case MotionEvent.ACTION_MOVE:
   velocityTracker.addMovement(event);
   velocityTracker.computeCurrentVelocity(1000); 
   //1000 provides pixels per second
   
   float xVelocity = velocityTracker.getXVelocity();
   float yVelocity = velocityTracker.getYVelocity();
   
   if(xVelocity > maxXVelocity){
    //max in right side
    maxXVelocity = xVelocity;
   }
   
   if(yVelocity > maxYVelocity){
    //Max in down side
    maxYVelocity = yVelocity;
   }
   
   textVelocityX.setText("X-velocity (pixel/s): " + xVelocity);
   textVelocityY.setText("Y-velocity (pixel/s): " + yVelocity);
   textMaxVelocityX.setText("max. X-velocity: " + maxXVelocity);
   textMaxVelocityY.setText("max. Y-velocity: " + maxYVelocity);

   break;
  case MotionEvent.ACTION_UP:
  case MotionEvent.ACTION_CANCEL:
   velocityTracker.recycle();
   break;
  }

  return true;
 }

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/action"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/velocityx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/maxvelocityx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/velocityy"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/maxvelocityy"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>



Next: VelocityTracker and VelocityTrackerCompat to track velocity for multi-touch case.

1 comment:

  1. "It's a example of using Android's VelocityTracker"

    And how do you use it? Just printing the values the VelocityTracker computes can hardly be called "usage". Without providing a clear example of how the VelocityTracker can be used in practice this post is useless.

    ReplyDelete