summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorChih-Chung Chang <chihchung@google.com>2012-07-31 13:03:24 +0800
committerChih-Chung Chang <chihchung@google.com>2012-08-01 12:42:28 +0800
commiteea522754d385c30d1918ab27c52e09d631ac7be (patch)
tree3ce1f9ac8094578f4b044eb0caaef2a5ce8b56e6 /src
parent12087cb18047940abd3fbad2c8b20d5017e9225b (diff)
downloadandroid_packages_apps_Snap-eea522754d385c30d1918ab27c52e09d631ac7be.tar.gz
android_packages_apps_Snap-eea522754d385c30d1918ab27c52e09d631ac7be.tar.bz2
android_packages_apps_Snap-eea522754d385c30d1918ab27c52e09d631ac7be.zip
Manually transform the event coordinates on older platforms.
Change-Id: I070b27112ea028f2a1596dcdfa480537765174b9
Diffstat (limited to 'src')
-rw-r--r--src/com/android/gallery3d/ui/GLRootView.java95
1 files changed, 94 insertions, 1 deletions
diff --git a/src/com/android/gallery3d/ui/GLRootView.java b/src/com/android/gallery3d/ui/GLRootView.java
index 9fd9473dc..7f717b7b7 100644
--- a/src/com/android/gallery3d/ui/GLRootView.java
+++ b/src/com/android/gallery3d/ui/GLRootView.java
@@ -25,7 +25,9 @@ import android.os.Build;
import android.os.Process;
import android.os.SystemClock;
import android.util.AttributeSet;
+import android.util.FloatMath;
import android.view.MotionEvent;
+import android.view.MotionEvent.PointerCoords;
import android.view.SurfaceHolder;
import android.view.View;
@@ -430,7 +432,7 @@ public class GLRootView extends GLSurfaceView
}
if (mCompensation != 0) {
- event.transform(mCompensationMatrix);
+ event = transformEvent(event, mCompensationMatrix);
}
mRenderLock.lock();
@@ -447,6 +449,97 @@ public class GLRootView extends GLSurfaceView
}
}
+ private static MotionEvent transformEvent(MotionEvent e, Matrix m) {
+ // We try to use the new transform method if possible because it uses
+ // less memory.
+ if (ApiHelper.HAS_MOTION_EVENT_TRANSFORM) {
+ return transformEventNew(e, m);
+ } else {
+ return transformEventOld(e, m);
+ }
+ }
+
+ @TargetApi(ApiHelper.VERSION_CODES.HONEYCOMB)
+ private static MotionEvent transformEventNew(MotionEvent e, Matrix m) {
+ e.transform(m);
+ return e;
+ }
+
+ // This is copied from Input.cpp in the android framework.
+ private static MotionEvent transformEventOld(MotionEvent e, Matrix m) {
+ long downTime = e.getDownTime();
+ long eventTime = e.getEventTime();
+ int action = e.getAction();
+ int pointerCount = e.getPointerCount();
+ int[] pointerIds = getPointerIds(e);
+ PointerCoords[] pointerCoords = getPointerCoords(e);
+ int metaState = e.getMetaState();
+ float xPrecision = e.getXPrecision();
+ float yPrecision = e.getYPrecision();
+ int deviceId = e.getDeviceId();
+ int edgeFlags = e.getEdgeFlags();
+ int source = e.getSource();
+ int flags = e.getFlags();
+
+ // Copy the x and y coordinates into an array, map them, and copy back.
+ float[] xy = new float[pointerCoords.length * 2];
+ for (int i = 0; i < pointerCount;i++) {
+ xy[2 * i] = pointerCoords[i].x;
+ xy[2 * i + 1] = pointerCoords[i].y;
+ }
+ m.mapPoints(xy);
+ for (int i = 0; i < pointerCount;i++) {
+ pointerCoords[i].x = xy[2 * i];
+ pointerCoords[i].y = xy[2 * i + 1];
+ pointerCoords[i].orientation = transformAngle(
+ m, pointerCoords[i].orientation);
+ }
+
+ MotionEvent n = MotionEvent.obtain(downTime, eventTime, action,
+ pointerCount, pointerIds, pointerCoords, metaState, xPrecision,
+ yPrecision, deviceId, edgeFlags, source, flags);
+
+ return n;
+ }
+
+ private static int[] getPointerIds(MotionEvent e) {
+ int n = e.getPointerCount();
+ int[] r = new int[n];
+ for (int i = 0; i < n; i++) {
+ r[i] = e.getPointerId(i);
+ }
+ return r;
+ }
+
+ private static PointerCoords[] getPointerCoords(MotionEvent e) {
+ int n = e.getPointerCount();
+ PointerCoords[] r = new PointerCoords[n];
+ for (int i = 0; i < n; i++) {
+ r[i] = new PointerCoords();
+ e.getPointerCoords(i, r[i]);
+ }
+ return r;
+ }
+
+ private static float transformAngle(Matrix m, float angleRadians) {
+ // Construct and transform a vector oriented at the specified clockwise
+ // angle from vertical. Coordinate system: down is increasing Y, right is
+ // increasing X.
+ float[] v = new float[2];
+ v[0] = FloatMath.sin(angleRadians);
+ v[1] = -FloatMath.cos(angleRadians);
+ m.mapVectors(v);
+
+ // Derive the transformed vector's clockwise angle from vertical.
+ float result = (float) Math.atan2(v[0], -v[1]);
+ if (result < -Math.PI / 2) {
+ result += Math.PI;
+ } else if (result > Math.PI / 2) {
+ result -= Math.PI;
+ }
+ return result;
+ }
+
private class IdleRunner implements Runnable {
// true if the idle runner is in the queue
private boolean mActive = false;