Wednesday, January 15, 2014

VelocityTracker and VelocityTrackerCompat

The post "VelocityTracker, track the velocity of touch events" for one touch point tracking using VelocityTracker. Together with android.support.v4.view.VelocityTrackerCompat (Helper for accessing features in VelocityTracker introduced after API level 4 in a backwards compatible fashion), we can track velocity for multi-touch tracking on post-HONEYCOMB device.






package com.example.androidvelocitytracker;

import android.os.Build;
import android.os.Bundle;
import android.os.Build.VERSION_CODES;
import android.support.v4.view.VelocityTrackerCompat;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {

 TextView textAvtion, textVelocityX0, textVelocityY0,
  textVelocityX1, textVelocityY1;
 TextView textVersion;
 VelocityTracker velocityTracker = null;
 
 static SparseArray<String> arrayVC = new SparseArray<String>();
    static {
        arrayVC.append(VERSION_CODES.BASE, "The original, first, version of Android.");
        arrayVC.append(VERSION_CODES.BASE_1_1, "First Android update, officially called 1.1");
        arrayVC.append(VERSION_CODES.CUPCAKE, "Android 1.5");
        arrayVC.append(VERSION_CODES.CUR_DEVELOPMENT, "Magic version number...");
        arrayVC.append(VERSION_CODES.DONUT, "Android 1.6");
        arrayVC.append(VERSION_CODES.ECLAIR, "Android 2.0");
        arrayVC.append(VERSION_CODES.ECLAIR_0_1, "Android 2.0.1");
        arrayVC.append(VERSION_CODES.ECLAIR_MR1, "Android 2.1");
        arrayVC.append(VERSION_CODES.FROYO, "Android 2.2");
        arrayVC.append(VERSION_CODES.GINGERBREAD,"Android 2.3");
        arrayVC.append(VERSION_CODES.GINGERBREAD_MR1, "Android 2.3.3");
        arrayVC.append(VERSION_CODES.HONEYCOMB, "Android 3.0");
        arrayVC.append(VERSION_CODES.HONEYCOMB_MR1, "Android 3.1");
        arrayVC.append(VERSION_CODES.HONEYCOMB_MR2, "Android 3.2");
        arrayVC.append(VERSION_CODES.ICE_CREAM_SANDWICH,"Android 4.0");
        arrayVC.append(VERSION_CODES.ICE_CREAM_SANDWICH_MR1,"Android 4.0.3");
        arrayVC.append(VERSION_CODES.JELLY_BEAN, "Android 4.1");
        arrayVC.append(VERSION_CODES.JELLY_BEAN_MR1, "Android 4.2");
        arrayVC.append(VERSION_CODES.JELLY_BEAN_MR2, "Android 4.3");
        arrayVC.append(VERSION_CODES.KITKAT, "Android 4.4");
    }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  textAvtion = (TextView) findViewById(R.id.action);
  textVelocityX0 = (TextView) findViewById(R.id.velocityx0);
  textVelocityY0 = (TextView) findViewById(R.id.velocityy0);
  textVelocityX1 = (TextView) findViewById(R.id.velocityx1);
  textVelocityY1 = (TextView) findViewById(R.id.velocityy1);
  
  textVersion = (TextView)findViewById(R.id.version);
  int version = Build.VERSION.SDK_INT;
  String BuildVersion = arrayVC.get(version, "unknown");
  textVersion.setText(BuildVersion);
 }

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

  switch (action) {
  case MotionEvent.ACTION_DOWN:
   if (velocityTracker == null) {
    velocityTracker = VelocityTracker.obtain();
   } else {
    velocityTracker.clear();
   }
   velocityTracker.addMovement(event);
   
   if(pointerId == 0){
    textVelocityX0.setText("X-velocity (pixel/s): 0");
    textVelocityY0.setText("Y-velocity (pixel/s): 0");
   }else if(pointerId == 1){
    textVelocityX1.setText("X-velocity (pixel/s): 0");
    textVelocityY1.setText("Y-velocity (pixel/s): 0");
   }
   
   break;
  case MotionEvent.ACTION_MOVE:
   velocityTracker.addMovement(event);
   velocityTracker.computeCurrentVelocity(1000); 
   //1000 provides pixels per second
   
   float xVelocity = VelocityTrackerCompat.getXVelocity(
     velocityTracker, pointerId);
   
   float yVelocity = VelocityTrackerCompat.getYVelocity(
     velocityTracker, pointerId);
   
   if(pointerId == 0){
    textVelocityX0.setText("X-velocity (pixel/s): " + xVelocity);
    textVelocityY0.setText("Y-velocity (pixel/s): " + yVelocity);
   }else if(pointerId == 1){
    textVelocityX1.setText("X-velocity (pixel/s): " + xVelocity);
    textVelocityY1.setText("Y-velocity (pixel/s): " + yVelocity);
   }
   
   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/version"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

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

 <TextView
        android:text="pointer 0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/velocityx0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/velocityy0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
 <TextView
        android:text="pointer 1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/velocityx1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/velocityy1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

No comments:

Post a Comment