Report your Multitouch operation : Touch « User Event « Android






Report your Multitouch operation

   
package app.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;

public class Test extends Activity implements OnTouchListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.layout1);
        layout1.setOnTouchListener(this);
    }

  public boolean onTouch(View v, MotionEvent event) {
    String myTag = v.getTag().toString();
    Log.v(myTag, describeEvent(event));
    logAction(event);
    if( "true".equals(myTag.substring(0, 4))) {
      return true;
    }
    else {
      return false;
    }
  }

  protected static String describeEvent(MotionEvent event) {
    StringBuilder result = new StringBuilder(500);
    result.append("Action: ").append(event.getAction()).append("\n");
    int numPointers = event.getPointerCount();
    result.append("Number of pointers: ").append(numPointers).append("\n");
        int ptrIdx = 0;
    while (ptrIdx < numPointers) {
        int ptrId = event.getPointerId(ptrIdx);
            result.append("Pointer Index: ").append(ptrIdx);
            result.append(", Pointer Id: ").append(ptrId).append("\n");
            result.append("   Location: ").append(event.getX(ptrIdx));
            result.append(" x ").append(event.getY(ptrIdx)).append("\n");
            result.append("   Pressure: ").append(event.getPressure(ptrIdx));
            result.append("   Size: ").append(event.getSize(ptrIdx)).append("\n");

            ptrIdx++;
        }
        result.append("Downtime: ").append(event.getDownTime()).append("ms\n");
        result.append("Event time: ").append(event.getEventTime()).append("ms");
        result.append("  Elapsed: ");
        result.append(event.getEventTime()-event.getDownTime());
        result.append(" ms\n");
        return result.toString();
    }

  private void logAction(MotionEvent event) {
    int action = event.getAction();
    int ptrIndex = (action & MotionEvent.ACTION_POINTER_ID_MASK) >>> MotionEvent.ACTION_POINTER_ID_SHIFT;
    action = action & MotionEvent.ACTION_MASK;
    if(action == 5 || action == 6)
      action = action - 5;
    int ptrId = event.getPointerId(ptrIndex);
    Log.v("Action", "Pointer index: " + ptrIndex);
    Log.v("Action", "Pointer Id: " + ptrId);
    Log.v("Action", "True action value: " + action);
  }
}

//main.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout1"  android:tag="trueLayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"  android:layout_height="wrap_content"
    android:layout_weight="1" >

    <TextView android:text="Touch fingers on the screen and look at LogCat"
    android:id="@+id/message"  android:tag="trueText"
    android:layout_width="wrap_content"  android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />
</RelativeLayout>

   
    
    
  








Related examples in the same category

1.Touch screen
2.Single Touch Test
3.Testing the Multitouch API
4.Demonstrates the handling of touch screen and trackball events to implement a simple painting app.
5.Demonstrates splitting touch events across multiple views within a view group.
6.Using your finger to draw