Add input/Multitouch/BasicMultitouch sample code.
This sample demonstrates the use of MotionEvent properties to track individual
touches across multiple touch events.
Change-Id: Idf2822b12bec1fa8f64ee2197238f39b329c3b2e
diff --git a/input/Multitouch/BasicMultitouch/AndroidManifest.xml b/input/Multitouch/BasicMultitouch/AndroidManifest.xml
new file mode 100644
index 0000000..d6709ef
--- /dev/null
+++ b/input/Multitouch/BasicMultitouch/AndroidManifest.xml
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright 2013 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.android.example.input.multitouch.basicMultitouch"
+ android:versionCode="1"
+ android:versionName="1.0" >
+
+ <uses-sdk
+ android:minSdkVersion="8"
+ android:targetSdkVersion="17" />
+
+ <application
+ android:allowBackup="true"
+ android:icon="@drawable/ic_launcher"
+ android:label="@string/app_name"
+ android:theme="@style/AppTheme" >
+ <activity
+ android:name="com.example.android.input.multitouch.basicMultitouch.MainActivity"
+ android:label="@string/app_name" >
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+
+ <category android:name="android.intent.category.LAUNCHER" />
+ </intent-filter>
+ </activity>
+ </application>
+
+</manifest>
\ No newline at end of file
diff --git a/input/Multitouch/BasicMultitouch/images/MultitouchSample.png b/input/Multitouch/BasicMultitouch/images/MultitouchSample.png
new file mode 100644
index 0000000..17447e9
--- /dev/null
+++ b/input/Multitouch/BasicMultitouch/images/MultitouchSample.png
Binary files differ
diff --git a/input/Multitouch/BasicMultitouch/multitouch.jd b/input/Multitouch/BasicMultitouch/multitouch.jd
new file mode 100644
index 0000000..d9108bb
--- /dev/null
+++ b/input/Multitouch/BasicMultitouch/multitouch.jd
@@ -0,0 +1,24 @@
+page.title=Multitouch Sample
+@jd:body
+
+<p>This samples demonstrates the use of <pre>MotionEvent</pre> properties to keep track of individual touches across multiple touch events.</p>
+
+<p>A <a href="http://developer.android.com/reference/android/view/MotionEvent.html">MotionEvent</a> describes a movement event.
+For touch events, they contain an action, X and Y coordinates and other relevant information. Multi-touch screens can report multiple movement traces at the same time,
+reporting all active touches within a single <pre>MotionEvent</pre>.</p>
+
+<p>Each pointer has a unique id that is assigned when it first goes down
+ (indicated by <pre><a href="/reference/android/view/MotionEvent.html#ACTION_DOWN">ACTION_DOWN</a></pre> or <pre><a href="/reference/android/view/MotionEvent.html#ACTION_POINTER_DOWN">ACTION_POINTER_DOWN</a></pre>). A pointer id
+ remains valid until the pointer eventually goes up (indicated by <pre><a href="/reference/android/view/MotionEvent.html#ACTION_UP">ACTION_UP</a></pre>
+ or <pre><a href="/reference/android/view/MotionEvent.html#ACTION_POINTER_UP">ACTION_POINTER_UP</a></pre>) or when the gesture is canceled (indicated by
+ <pre><a href="/reference/android/view/MotionEvent.html#ACTION_CANCEL">ACTION_CANCEL</a></pre>).
+ </p>
+
+<p>The sample shows how these pointer identifiers can be used over subsequent events to track individual fingers.
+This is illustrated by a custom View that responds to touch events. Coloured circles are drawn for each finger, showing its current and past position on the screen.
+This example shows the relationship between <pre>MotionEvent</pre> indices, pointer identifiers and <pre>MotionEvent</pre> actions.</p>
+
+<p>For more information, see the relevant <a href="http://android-developers.blogspot.com.au/2010/06/making-sense-of-multitouch.html">blog post</a> and
+<a href="http://developer.android.com/reference/android/view/MotionEvent.html"><pre>MotionEvent</pre></a> API documentation. </p>
+
+<p><img src="images/MultitouchSample.png" /></p>
diff --git a/input/Multitouch/BasicMultitouch/res/drawable-hdpi/ic_launcher.png b/input/Multitouch/BasicMultitouch/res/drawable-hdpi/ic_launcher.png
new file mode 100644
index 0000000..c49a01f
--- /dev/null
+++ b/input/Multitouch/BasicMultitouch/res/drawable-hdpi/ic_launcher.png
Binary files differ
diff --git a/input/Multitouch/BasicMultitouch/res/drawable-mdpi/ic_launcher.png b/input/Multitouch/BasicMultitouch/res/drawable-mdpi/ic_launcher.png
new file mode 100644
index 0000000..34d8c42
--- /dev/null
+++ b/input/Multitouch/BasicMultitouch/res/drawable-mdpi/ic_launcher.png
Binary files differ
diff --git a/input/Multitouch/BasicMultitouch/res/drawable-xhdpi/ic_launcher.png b/input/Multitouch/BasicMultitouch/res/drawable-xhdpi/ic_launcher.png
new file mode 100644
index 0000000..abb5f12
--- /dev/null
+++ b/input/Multitouch/BasicMultitouch/res/drawable-xhdpi/ic_launcher.png
Binary files differ
diff --git a/input/Multitouch/BasicMultitouch/res/drawable-xxhdpi/ic_launcher.png b/input/Multitouch/BasicMultitouch/res/drawable-xxhdpi/ic_launcher.png
new file mode 100644
index 0000000..ef649c1
--- /dev/null
+++ b/input/Multitouch/BasicMultitouch/res/drawable-xxhdpi/ic_launcher.png
Binary files differ
diff --git a/input/Multitouch/BasicMultitouch/res/layout/layout_mainactivity.xml b/input/Multitouch/BasicMultitouch/res/layout/layout_mainactivity.xml
new file mode 100644
index 0000000..a53e5f1
--- /dev/null
+++ b/input/Multitouch/BasicMultitouch/res/layout/layout_mainactivity.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent" >
+
+ <TextView
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_gravity="center"
+ android:padding="75dp"
+ android:text="@string/explanation" />
+
+ <com.example.android.input.multitouch.basicMultitouch.TouchDisplayView
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content" />
+
+</FrameLayout>
\ No newline at end of file
diff --git a/input/Multitouch/BasicMultitouch/res/values-v11/styles.xml b/input/Multitouch/BasicMultitouch/res/values-v11/styles.xml
new file mode 100644
index 0000000..9daaa26
--- /dev/null
+++ b/input/Multitouch/BasicMultitouch/res/values-v11/styles.xml
@@ -0,0 +1,27 @@
+<!--
+ Copyright 2013 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+
+ <resources>
+
+ <!--
+ Base application theme for API 11+. This theme completely replaces
+ AppBaseTheme from res/values/styles.xml on API 11+ devices.
+ -->
+ <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
+ <!-- API 11 theme customizations can go here. -->
+ </style>
+
+</resources>
\ No newline at end of file
diff --git a/input/Multitouch/BasicMultitouch/res/values-v14/styles.xml b/input/Multitouch/BasicMultitouch/res/values-v14/styles.xml
new file mode 100644
index 0000000..42ce193
--- /dev/null
+++ b/input/Multitouch/BasicMultitouch/res/values-v14/styles.xml
@@ -0,0 +1,28 @@
+<!--
+ Copyright 2013 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+
+ <resources>
+
+ <!--
+ Base application theme for API 14+. This theme completely replaces
+ AppBaseTheme from BOTH res/values/styles.xml and
+ res/values-v11/styles.xml on API 14+ devices.
+ -->
+ <style name="AppBaseTheme" parent="android:Theme.Holo.Light.NoActionBar">
+ <!-- API 14 theme customizations can go here. -->
+ </style>
+
+</resources>
\ No newline at end of file
diff --git a/input/Multitouch/BasicMultitouch/res/values/strings.xml b/input/Multitouch/BasicMultitouch/res/values/strings.xml
new file mode 100644
index 0000000..541639c
--- /dev/null
+++ b/input/Multitouch/BasicMultitouch/res/values/strings.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright 2013 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<resources>
+
+ <string name="app_name">BasicMultitouch</string>
+ <string name="explanation">This samples demonstrates the use of <b>MotionEvent</b> properties
+ to keep track of individual touches across multiple touch events.\n\n
+Touch the screen with multiple fingers to show that the pointer id
+(also represented by a colour) does not change as new touch events are received.</string>
+
+</resources>
\ No newline at end of file
diff --git a/input/Multitouch/BasicMultitouch/res/values/styles.xml b/input/Multitouch/BasicMultitouch/res/values/styles.xml
new file mode 100644
index 0000000..dedc633
--- /dev/null
+++ b/input/Multitouch/BasicMultitouch/res/values/styles.xml
@@ -0,0 +1,35 @@
+<!--
+ Copyright 2013 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+<resources>
+
+ <!--
+ Base application theme, dependent on API level. This theme is replaced
+ by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
+ -->
+ <style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar">
+ <!--
+ Theme customizations available in newer API levels can go in
+ res/values-vXX/styles.xml, while customizations related to
+ backward-compatibility can go here.
+ -->
+ </style>
+
+ <!-- Application theme. -->
+ <style name="AppTheme" parent="AppBaseTheme">
+ <!-- All customizations that are NOT specific to a particular API-level can go here. -->
+ </style>
+
+</resources>
\ No newline at end of file
diff --git a/input/Multitouch/BasicMultitouch/src/com/example/android/common/Pools.java b/input/Multitouch/BasicMultitouch/src/com/example/android/common/Pools.java
new file mode 100644
index 0000000..67c937b
--- /dev/null
+++ b/input/Multitouch/BasicMultitouch/src/com/example/android/common/Pools.java
@@ -0,0 +1,165 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.common;
+
+/**
+ * Helper class for crating pools of objects. An example use looks like this:
+ * <pre>
+ * public class MyPooledClass {
+ *
+ * private static final SynchronizedPool<MyPooledClass> sPool =
+ * new SynchronizedPool<MyPooledClass>(10);
+ *
+ * public static MyPooledClass obtain() {
+ * MyPooledClass instance = sPool.acquire();
+ * return (instance != null) ? instance : new MyPooledClass();
+ * }
+ *
+ * public void recycle() {
+ * // Clear state if needed.
+ * sPool.release(this);
+ * }
+ *
+ * . . .
+ * }
+ * </pre>
+ *
+ * @hide
+ */
+public final class Pools {
+
+ /**
+ * Interface for managing a pool of objects.
+ *
+ * @param <T> The pooled type.
+ */
+ public static interface Pool<T> {
+
+ /**
+ * @return An instance from the pool if such, null otherwise.
+ */
+ public T acquire();
+
+ /**
+ * Release an instance to the pool.
+ *
+ * @param instance The instance to release.
+ * @return Whether the instance was put in the pool.
+ *
+ * @throws IllegalStateException If the instance is already in the pool.
+ */
+ public boolean release(T instance);
+ }
+
+ private Pools() {
+ /* do nothing - hiding constructor */
+ }
+
+ /**
+ * Simple (non-synchronized) pool of objects.
+ *
+ * @param <T> The pooled type.
+ */
+ public static class SimplePool<T> implements Pool<T> {
+ private final Object[] mPool;
+
+ private int mPoolSize;
+
+ /**
+ * Creates a new instance.
+ *
+ * @param maxPoolSize The max pool size.
+ *
+ * @throws IllegalArgumentException If the max pool size is less than zero.
+ */
+ public SimplePool(int maxPoolSize) {
+ if (maxPoolSize <= 0) {
+ throw new IllegalArgumentException("The max pool size must be > 0");
+ }
+ mPool = new Object[maxPoolSize];
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public T acquire() {
+ if (mPoolSize > 0) {
+ final int lastPooledIndex = mPoolSize - 1;
+ T instance = (T) mPool[lastPooledIndex];
+ mPool[lastPooledIndex] = null;
+ mPoolSize--;
+ return instance;
+ }
+ return null;
+ }
+
+ @Override
+ public boolean release(T instance) {
+ if (isInPool(instance)) {
+ throw new IllegalStateException("Already in the pool!");
+ }
+ if (mPoolSize < mPool.length) {
+ mPool[mPoolSize] = instance;
+ mPoolSize++;
+ return true;
+ }
+ return false;
+ }
+
+ private boolean isInPool(T instance) {
+ for (int i = 0; i < mPoolSize; i++) {
+ if (mPool[i] == instance) {
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+
+ /**
+ * Synchronized) pool of objects.
+ *
+ * @param <T> The pooled type.
+ */
+ public static class SynchronizedPool<T> extends SimplePool<T> {
+ private final Object mLock = new Object();
+
+ /**
+ * Creates a new instance.
+ *
+ * @param maxPoolSize The max pool size.
+ *
+ * @throws IllegalArgumentException If the max pool size is less than zero.
+ */
+ public SynchronizedPool(int maxPoolSize) {
+ super(maxPoolSize);
+ }
+
+ @Override
+ public T acquire() {
+ synchronized (mLock) {
+ return super.acquire();
+ }
+ }
+
+ @Override
+ public boolean release(T element) {
+ synchronized (mLock) {
+ return super.release(element);
+ }
+ }
+ }
+}
diff --git a/input/Multitouch/BasicMultitouch/src/com/example/android/input/multitouch/basicMultitouch/MainActivity.java b/input/Multitouch/BasicMultitouch/src/com/example/android/input/multitouch/basicMultitouch/MainActivity.java
new file mode 100644
index 0000000..627ada5
--- /dev/null
+++ b/input/Multitouch/BasicMultitouch/src/com/example/android/input/multitouch/basicMultitouch/MainActivity.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.input.multitouch.basicMultitouch;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.view.MotionEvent;
+
+import com.android.example.input.multitouch.basicMultitouch.R;
+
+/**
+ * This is an example of keeping track of individual touches across multiple
+ * {@link MotionEvent}s.
+ * <p>
+ * This is illustrated by a View ({@link TouchDisplayView}) that responds to
+ * touch events and draws coloured circles for each pointer, stores the last
+ * positions of this pointer and draws them. This example shows the relationship
+ * between MotionEvent indices, pointer identifiers and actions.
+ *
+ * @see MotionEvent
+ */
+public class MainActivity extends Activity {
+ TouchDisplayView mView;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ setContentView(R.layout.layout_mainactivity);
+ }
+
+}
diff --git a/input/Multitouch/BasicMultitouch/src/com/example/android/input/multitouch/basicMultitouch/TouchDisplayView.java b/input/Multitouch/BasicMultitouch/src/com/example/android/input/multitouch/basicMultitouch/TouchDisplayView.java
new file mode 100644
index 0000000..55bcf8f
--- /dev/null
+++ b/input/Multitouch/BasicMultitouch/src/com/example/android/input/multitouch/basicMultitouch/TouchDisplayView.java
@@ -0,0 +1,401 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.input.multitouch.basicMultitouch;
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.graphics.PointF;
+import android.util.AttributeSet;
+import android.util.SparseArray;
+import android.view.MotionEvent;
+import android.view.View;
+
+import com.example.android.common.Pools.SimplePool;
+
+/**
+ * View that shows touch events and their history. This view demonstrates the
+ * use of {@link #onTouchEvent(MotionEvent)} and {@link MotionEvent}s to keep
+ * track of touch pointers across events.
+ */
+public class TouchDisplayView extends View {
+
+ // Hold data for active touch pointer IDs
+ private SparseArray<TouchHistory> mTouches;
+
+ // Is there an active touch?
+ private boolean mHasTouch = false;
+
+ /**
+ * Holds data related to a touch pointer, including its current position,
+ * pressure and historical positions. Objects are allocated through an
+ * object pool using {@link #obtain()} and {@link #recycle()} to reuse
+ * existing objects.
+ */
+ static final class TouchHistory {
+
+ // number of historical points to store
+ public static final int HISTORY_COUNT = 20;
+
+ public float x;
+ public float y;
+ public float pressure = 0f;
+ public String label = null;
+
+ // current position in history array
+ public int historyIndex = 0;
+ public int historyCount = 0;
+
+ // arrray of pointer position history
+ public PointF[] history = new PointF[HISTORY_COUNT];
+
+ private static final int MAX_POOL_SIZE = 10;
+ private static final SimplePool<TouchHistory> sPool =
+ new SimplePool<TouchHistory>(MAX_POOL_SIZE);
+
+ public static TouchHistory obtain(float x, float y, float pressure) {
+ TouchHistory data = sPool.acquire();
+ if (data == null) {
+ data = new TouchHistory();
+ }
+
+ data.setTouch(x, y, pressure);
+
+ return data;
+ }
+
+ public TouchHistory() {
+
+ // initialise history array
+ for (int i = 0; i < HISTORY_COUNT; i++) {
+ history[i] = new PointF();
+ }
+ }
+
+ public void setTouch(float x, float y, float pressure) {
+ this.x = x;
+ this.y = y;
+ this.pressure = pressure;
+ }
+
+ public void recycle() {
+ this.historyIndex = 0;
+ this.historyCount = 0;
+ sPool.release(this);
+ }
+
+ /**
+ * Add a point to its history. Overwrites oldest point if the maximum
+ * number of historical points is already stored.
+ *
+ * @param point
+ */
+ public void addHistory(float x, float y) {
+ PointF p = history[historyIndex];
+ p.x = x;
+ p.y = y;
+
+ historyIndex = (historyIndex + 1) % history.length;
+
+ if (historyCount < HISTORY_COUNT) {
+ historyCount++;
+ }
+ }
+
+ }
+
+ public TouchDisplayView(Context context, AttributeSet attrs) {
+ super(context, attrs);
+
+ // SparseArray for touch events, indexed by touch id
+ mTouches = new SparseArray<TouchDisplayView.TouchHistory>(10);
+
+ initialisePaint();
+ }
+
+ // BEGIN_INCLUDE(onTouchEvent)
+ @Override
+ public boolean onTouchEvent(MotionEvent event) {
+
+ final int action = event.getAction();
+
+ /*
+ * Switch on the action. The action is extracted from the event by
+ * applying the MotionEvent.ACTION_MASK. Alternatively a call to
+ * event.getActionMasked() would yield in the action as well.
+ */
+ switch (action & MotionEvent.ACTION_MASK) {
+
+ case MotionEvent.ACTION_DOWN: {
+ // first pressed gesture has started
+
+ /*
+ * Only one touch event is stored in the MotionEvent. Extract
+ * the pointer identifier of this touch from the first index
+ * within the MotionEvent object.
+ */
+ int id = event.getPointerId(0);
+
+ TouchHistory data = TouchHistory.obtain(event.getX(0), event.getY(0),
+ event.getPressure(0));
+ data.label = "id: " + 0;
+
+ /*
+ * Store the data under its pointer identifier. The pointer
+ * number stays consistent for the duration of a gesture,
+ * accounting for other pointers going up or down.
+ */
+ mTouches.put(id, data);
+
+ mHasTouch = true;
+
+ break;
+ }
+
+ case MotionEvent.ACTION_POINTER_DOWN: {
+ /*
+ * A non-primary pointer has gone down, after an event for the
+ * primary pointer (ACTION_DOWN) has already been received.
+ */
+
+ /*
+ * The MotionEvent object contains multiple pointers. Need to
+ * extract the index at which the data for this particular event
+ * is stored.
+ */
+ int index = event.getActionIndex();
+ int id = event.getPointerId(index);
+
+ TouchHistory data = TouchHistory.obtain(event.getX(index), event.getY(index),
+ event.getPressure(index));
+ data.label = "id: " + id;
+
+ /*
+ * Store the data under its pointer identifier. The index of
+ * this pointer can change over multiple events, but this
+ * pointer is always identified by the same identifier for this
+ * active gesture.
+ */
+ mTouches.put(id, data);
+
+ break;
+ }
+
+ case MotionEvent.ACTION_UP: {
+ /*
+ * Final pointer has gone up and has ended the last pressed
+ * gesture.
+ */
+
+ /*
+ * Extract the pointer identifier for the only event stored in
+ * the MotionEvent object and remove it from the list of active
+ * touches.
+ */
+ int id = event.getPointerId(0);
+ TouchHistory data = mTouches.get(id);
+ mTouches.remove(id);
+ data.recycle();
+
+ mHasTouch = false;
+
+ break;
+ }
+
+ case MotionEvent.ACTION_POINTER_UP: {
+ /*
+ * A non-primary pointer has gone up and other pointers are
+ * still active.
+ */
+
+ /*
+ * The MotionEvent object contains multiple pointers. Need to
+ * extract the index at which the data for this particular event
+ * is stored.
+ */
+ int index = event.getActionIndex();
+ int id = event.getPointerId(index);
+
+ TouchHistory data = mTouches.get(id);
+ mTouches.remove(id);
+ data.recycle();
+
+ break;
+ }
+
+ case MotionEvent.ACTION_MOVE: {
+ /*
+ * A change event happened during a pressed gesture. (Between
+ * ACTION_DOWN and ACTION_UP or ACTION_POINTER_DOWN and
+ * ACTION_POINTER_UP)
+ */
+
+ /*
+ * Loop through all active pointers contained within this event.
+ * Data for each pointer is stored in a MotionEvent at an index
+ * (starting from 0 up to the number of active pointers). This
+ * loop goes through each of these active pointers, extracts its
+ * data (position and pressure) and updates its stored data. A
+ * pointer is identified by its pointer number which stays
+ * constant across touch events as long as it remains active.
+ * This identifier is used to keep track of a pointer across
+ * events.
+ */
+ for (int index = 0; index < event.getPointerCount(); index++) {
+ // get pointer id for data stored at this index
+ int id = event.getPointerId(index);
+
+ // get the data stored externally about this pointer.
+ TouchHistory data = mTouches.get(id);
+
+ // add previous position to history and add new values
+ data.addHistory(data.x, data.y);
+ data.setTouch(event.getX(index), event.getY(index),
+ event.getPressure(index));
+
+ }
+
+ break;
+ }
+ }
+
+ // trigger redraw on UI thread
+ this.postInvalidate();
+
+ return true;
+ }
+
+ // END_INCLUDE(onTouchEvent)
+
+ @Override
+ protected void onDraw(Canvas canvas) {
+ super.onDraw(canvas);
+
+ // Canvas background color depends on whether there is an active touch
+ if (mHasTouch) {
+ canvas.drawColor(BACKGROUND_ACTIVE);
+ } else {
+ // draw inactive border
+ canvas.drawRect(mBorderWidth, mBorderWidth, getWidth() - mBorderWidth, getHeight()
+ - mBorderWidth, mBorderPaint);
+ }
+
+ // loop through all active touches and draw them
+ for (int i = 0; i < mTouches.size(); i++) {
+
+ // get the pointer id and associated data for this index
+ int id = mTouches.keyAt(i);
+ TouchHistory data = mTouches.valueAt(i);
+
+ // draw the data and its history to the canvas
+ drawCircle(canvas, id, data);
+ }
+ }
+
+ /*
+ * Below are only helper methods and variables required for drawing.
+ */
+
+ // radius of active touch circle in dp
+ private static final float CIRCLE_RADIUS_DP = 75f;
+ // radius of historical circle in dp
+ private static final float CIRCLE_HISTORICAL_RADIUS_DP = 7f;
+
+ // calculated radiuses in px
+ private float mCircleRadius;
+ private float mCircleHistoricalRadius;
+
+ private Paint mCirclePaint = new Paint();
+ private Paint mTextPaint = new Paint();
+
+ private static final int BACKGROUND_ACTIVE = Color.WHITE;
+
+ // inactive border
+ private static final float INACTIVE_BORDER_DP = 15f;
+ private static final int INACTIVE_BORDER_COLOR = 0xFFffd060;
+ private Paint mBorderPaint = new Paint();
+ private float mBorderWidth;
+
+ public final int[] COLORS = {
+ 0xFF33B5E5, 0xFFAA66CC, 0xFF99CC00, 0xFFFFBB33, 0xFFFF4444,
+ 0xFF0099CC, 0xFF9933CC, 0xFF669900, 0xFFFF8800, 0xFFCC0000
+ };
+
+ /**
+ * Sets up the required {@link Paint} objects for the screen density of this
+ * device.
+ */
+ private void initialisePaint() {
+
+ // Calculate radiuses in px from dp based on screen density
+ float density = getResources().getDisplayMetrics().density;
+ mCircleRadius = CIRCLE_RADIUS_DP * density;
+ mCircleHistoricalRadius = CIRCLE_HISTORICAL_RADIUS_DP * density;
+
+ // Setup text paint for circle label
+ mTextPaint.setTextSize(27f);
+ mTextPaint.setColor(Color.BLACK);
+
+ // Setup paint for inactive border
+ mBorderWidth = INACTIVE_BORDER_DP * density;
+ mBorderPaint.setStrokeWidth(mBorderWidth);
+ mBorderPaint.setColor(INACTIVE_BORDER_COLOR);
+ mBorderPaint.setStyle(Paint.Style.STROKE);
+
+ }
+
+ /**
+ * Draws the data encapsulated by a {@link TouchHistory} object to a canvas.
+ * A large circle indicates the current position held by the
+ * {@link TouchHistory} object, while a smaller circle is drawn for each
+ * entry in its history. The size of the large circle is scaled depending on
+ * its pressure, clamped to a maximum of <code>1.0</code>.
+ *
+ * @param canvas
+ * @param id
+ * @param data
+ */
+ protected void drawCircle(Canvas canvas, int id, TouchDisplayView.TouchHistory data) {
+ // select the color based on the id
+ int color = COLORS[id % COLORS.length];
+ mCirclePaint.setColor(color);
+
+ /*
+ * Draw the circle, size scaled to its pressure. Pressure is clamped to
+ * 1.0 max to ensure proper drawing. (Reported pressure values can
+ * exceed 1.0, depending on the calibration of the touch screen).
+ */
+ float pressure = Math.min(data.pressure, 1f);
+ float radius = pressure * mCircleRadius;
+
+ canvas.drawCircle(data.x, (data.y) - (radius / 2f), radius,
+ mCirclePaint);
+
+ // draw all historical points with a lower alpha value
+ mCirclePaint.setAlpha(125);
+ for (int j = 0; j < data.history.length && j < data.historyCount; j++) {
+ PointF p = data.history[j];
+ canvas.drawCircle(p.x, p.y, mCircleHistoricalRadius, mCirclePaint);
+ }
+
+ // draw its label next to the main circle
+ canvas.drawText(data.label, data.x + radius, data.y
+ - radius, mTextPaint);
+ }
+
+}