Update Pointer Location app to show multiple pointers.
This commit is contained in:
@@ -47,16 +47,9 @@ public class PointerLocation extends Activity {
|
|||||||
getWindow().setAttributes(lp);
|
getWindow().setAttributes(lp);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MyView extends View {
|
public static class PointerState {
|
||||||
private final Paint mTextPaint;
|
|
||||||
private final Paint mTextBackgroundPaint;
|
|
||||||
private final Paint mTextLevelPaint;
|
|
||||||
private final Paint mPaint;
|
|
||||||
private final Paint mTargetPaint;
|
|
||||||
private final FontMetricsInt mTextMetrics = new FontMetricsInt();
|
|
||||||
private final ArrayList<Float> mXs = new ArrayList<Float>();
|
private final ArrayList<Float> mXs = new ArrayList<Float>();
|
||||||
private final ArrayList<Float> mYs = new ArrayList<Float>();
|
private final ArrayList<Float> mYs = new ArrayList<Float>();
|
||||||
private int mHeaderBottom;
|
|
||||||
private boolean mCurDown;
|
private boolean mCurDown;
|
||||||
private int mCurX;
|
private int mCurX;
|
||||||
private int mCurY;
|
private int mCurY;
|
||||||
@@ -64,12 +57,26 @@ public class PointerLocation extends Activity {
|
|||||||
private float mCurSize;
|
private float mCurSize;
|
||||||
private int mCurWidth;
|
private int mCurWidth;
|
||||||
private VelocityTracker mVelocity;
|
private VelocityTracker mVelocity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class MyView extends View {
|
||||||
|
private final Paint mTextPaint;
|
||||||
|
private final Paint mTextBackgroundPaint;
|
||||||
|
private final Paint mTextLevelPaint;
|
||||||
|
private final Paint mPaint;
|
||||||
|
private final Paint mTargetPaint;
|
||||||
|
private final FontMetricsInt mTextMetrics = new FontMetricsInt();
|
||||||
|
private int mHeaderBottom;
|
||||||
|
private boolean mCurDown;
|
||||||
|
private final ArrayList<PointerState> mPointers
|
||||||
|
= new ArrayList<PointerState>();
|
||||||
|
|
||||||
public MyView(Context c) {
|
public MyView(Context c) {
|
||||||
super(c);
|
super(c);
|
||||||
mTextPaint = new Paint();
|
mTextPaint = new Paint();
|
||||||
mTextPaint.setAntiAlias(true);
|
mTextPaint.setAntiAlias(true);
|
||||||
mTextPaint.setTextSize(10);
|
mTextPaint.setTextSize(10
|
||||||
|
* getResources().getDisplayMetrics().density);
|
||||||
mTextPaint.setARGB(255, 0, 0, 0);
|
mTextPaint.setARGB(255, 0, 0, 0);
|
||||||
mTextBackgroundPaint = new Paint();
|
mTextBackgroundPaint = new Paint();
|
||||||
mTextBackgroundPaint.setAntiAlias(false);
|
mTextBackgroundPaint.setAntiAlias(false);
|
||||||
@@ -106,26 +113,35 @@ public class PointerLocation extends Activity {
|
|||||||
int w = getWidth()/5;
|
int w = getWidth()/5;
|
||||||
int base = -mTextMetrics.ascent+1;
|
int base = -mTextMetrics.ascent+1;
|
||||||
int bottom = mHeaderBottom;
|
int bottom = mHeaderBottom;
|
||||||
canvas.drawRect(0, 0, w-1, bottom, mTextBackgroundPaint);
|
|
||||||
canvas.drawText("X: " + mCurX, 1, base, mTextPaint);
|
|
||||||
canvas.drawRect(w, 0, (w * 2) - 1, bottom, mTextBackgroundPaint);
|
|
||||||
canvas.drawText("Y: " + mCurY, 1 + w, base, mTextPaint);
|
|
||||||
canvas.drawRect(w * 2, 0, (w * 3) - 1, bottom, mTextBackgroundPaint);
|
|
||||||
canvas.drawRect(w * 2, 0, (w * 2) + (mCurPressure * w) - 1, bottom, mTextLevelPaint);
|
|
||||||
canvas.drawText("Pres: " + mCurPressure, 1 + w * 2, base, mTextPaint);
|
|
||||||
canvas.drawRect(w * 3, 0, (w * 4) - 1, bottom, mTextBackgroundPaint);
|
|
||||||
canvas.drawRect(w * 3, 0, (w * 3) + (mCurSize * w) - 1, bottom, mTextLevelPaint);
|
|
||||||
canvas.drawText("Size: " + mCurSize, 1 + w * 3, base, mTextPaint);
|
|
||||||
canvas.drawRect(w * 4, 0, getWidth(), bottom, mTextBackgroundPaint);
|
|
||||||
int velocity = mVelocity == null ? 0 : (int) (mVelocity.getYVelocity() * 1000);
|
|
||||||
canvas.drawText("yVel: " + velocity, 1 + w * 4, base, mTextPaint);
|
|
||||||
|
|
||||||
final int N = mXs.size();
|
final int NP = mPointers.size();
|
||||||
|
|
||||||
|
if (NP > 0) {
|
||||||
|
final PointerState ps = mPointers.get(0);
|
||||||
|
canvas.drawRect(0, 0, w-1, bottom, mTextBackgroundPaint);
|
||||||
|
canvas.drawText("X: " + ps.mCurX, 1, base, mTextPaint);
|
||||||
|
canvas.drawRect(w, 0, (w * 2) - 1, bottom, mTextBackgroundPaint);
|
||||||
|
canvas.drawText("Y: " + ps.mCurY, 1 + w, base, mTextPaint);
|
||||||
|
canvas.drawRect(w * 2, 0, (w * 3) - 1, bottom, mTextBackgroundPaint);
|
||||||
|
canvas.drawRect(w * 2, 0, (w * 2) + (ps.mCurPressure * w) - 1, bottom, mTextLevelPaint);
|
||||||
|
canvas.drawText("Pres: " + ps.mCurPressure, 1 + w * 2, base, mTextPaint);
|
||||||
|
canvas.drawRect(w * 3, 0, (w * 4) - 1, bottom, mTextBackgroundPaint);
|
||||||
|
canvas.drawRect(w * 3, 0, (w * 3) + (ps.mCurSize * w) - 1, bottom, mTextLevelPaint);
|
||||||
|
canvas.drawText("Size: " + ps.mCurSize, 1 + w * 3, base, mTextPaint);
|
||||||
|
canvas.drawRect(w * 4, 0, getWidth(), bottom, mTextBackgroundPaint);
|
||||||
|
int velocity = ps.mVelocity == null ? 0 : (int) (ps.mVelocity.getYVelocity() * 1000);
|
||||||
|
canvas.drawText("yVel: " + velocity, 1 + w * 4, base, mTextPaint);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int p=0; p<NP; p++) {
|
||||||
|
final PointerState ps = mPointers.get(p);
|
||||||
|
|
||||||
|
final int N = ps.mXs.size();
|
||||||
float lastX=0, lastY=0;
|
float lastX=0, lastY=0;
|
||||||
mPaint.setARGB(255, 0, 255, 255);
|
mPaint.setARGB(255, 0, 255, 255);
|
||||||
for (int i=0; i<N; i++) {
|
for (int i=0; i<N; i++) {
|
||||||
float x = mXs.get(i);
|
float x = ps.mXs.get(i);
|
||||||
float y = mYs.get(i);
|
float y = ps.mYs.get(i);
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
canvas.drawLine(lastX, lastY, x, y, mTargetPaint);
|
canvas.drawLine(lastX, lastY, x, y, mTargetPaint);
|
||||||
canvas.drawPoint(lastX, lastY, mPaint);
|
canvas.drawPoint(lastX, lastY, mPaint);
|
||||||
@@ -133,28 +149,36 @@ public class PointerLocation extends Activity {
|
|||||||
lastX = x;
|
lastX = x;
|
||||||
lastY = y;
|
lastY = y;
|
||||||
}
|
}
|
||||||
if (mVelocity != null) {
|
if (ps.mVelocity != null) {
|
||||||
mPaint.setARGB(255, 255, 0, 0);
|
mPaint.setARGB(255, 255, 0, 0);
|
||||||
float xVel = mVelocity.getXVelocity() * (1000/60);
|
float xVel = ps.mVelocity.getXVelocity() * (1000/60);
|
||||||
float yVel = mVelocity.getYVelocity() * (1000/60);
|
float yVel = ps.mVelocity.getYVelocity() * (1000/60);
|
||||||
canvas.drawLine(lastX, lastY, lastX+xVel, lastY+yVel, mPaint);
|
canvas.drawLine(lastX, lastY, lastX+xVel, lastY+yVel, mPaint);
|
||||||
} else {
|
} else {
|
||||||
canvas.drawPoint(lastX, lastY, mPaint);
|
canvas.drawPoint(lastX, lastY, mPaint);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (mCurDown) {
|
if (mCurDown && NP > 0) {
|
||||||
canvas.drawLine(0, (int)mCurY, getWidth(), (int)mCurY, mTargetPaint);
|
final PointerState ps = mPointers.get(0);
|
||||||
canvas.drawLine((int)mCurX, 0, (int)mCurX, getHeight(), mTargetPaint);
|
canvas.drawLine(0, (int)ps.mCurY, getWidth(), (int)ps.mCurY, mTargetPaint);
|
||||||
int pressureLevel = (int)(mCurPressure*255);
|
canvas.drawLine((int)ps.mCurX, 0, (int)ps.mCurX, getHeight(), mTargetPaint);
|
||||||
|
int pressureLevel = (int)(ps.mCurPressure*255);
|
||||||
mPaint.setARGB(255, pressureLevel, 128, 255-pressureLevel);
|
mPaint.setARGB(255, pressureLevel, 128, 255-pressureLevel);
|
||||||
canvas.drawPoint(mCurX, mCurY, mPaint);
|
canvas.drawPoint(ps.mCurX, ps.mCurY, mPaint);
|
||||||
canvas.drawCircle(mCurX, mCurY, mCurWidth, mPaint);
|
canvas.drawCircle(ps.mCurX, ps.mCurY, ps.mCurWidth, mPaint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onTouchEvent(MotionEvent event) {
|
public boolean onTouchEvent(MotionEvent event) {
|
||||||
int action = event.getAction();
|
int action = event.getAction();
|
||||||
|
|
||||||
|
//Log.i("Pointer", "Motion: action=0x" + Integer.toHexString(action)
|
||||||
|
// + " pointers=" + event.getPointerCount());
|
||||||
|
|
||||||
|
int NP = mPointers.size();
|
||||||
|
|
||||||
//mRect.set(0, 0, getWidth(), mHeaderBottom+1);
|
//mRect.set(0, 0, getWidth(), mHeaderBottom+1);
|
||||||
//invalidate(mRect);
|
//invalidate(mRect);
|
||||||
//if (mCurDown) {
|
//if (mCurDown) {
|
||||||
@@ -164,26 +188,57 @@ public class PointerLocation extends Activity {
|
|||||||
// mRect.setEmpty();
|
// mRect.setEmpty();
|
||||||
//}
|
//}
|
||||||
if (action == MotionEvent.ACTION_DOWN) {
|
if (action == MotionEvent.ACTION_DOWN) {
|
||||||
mXs.clear();
|
for (int p=0; p<NP; p++) {
|
||||||
mYs.clear();
|
final PointerState ps = mPointers.get(p);
|
||||||
mVelocity = VelocityTracker.obtain();
|
ps.mXs.clear();
|
||||||
|
ps.mYs.clear();
|
||||||
|
ps.mVelocity = VelocityTracker.obtain();
|
||||||
}
|
}
|
||||||
mVelocity.addMovement(event);
|
}
|
||||||
mVelocity.computeCurrentVelocity(1);
|
|
||||||
|
while (NP < event.getPointerCount()) {
|
||||||
|
PointerState ps = new PointerState();
|
||||||
|
ps.mVelocity = VelocityTracker.obtain();
|
||||||
|
mPointers.add(ps);
|
||||||
|
NP++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((action&MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_DOWN) {
|
||||||
|
final PointerState ps = mPointers.get(
|
||||||
|
(action&MotionEvent.ACTION_POINTER_MASK)
|
||||||
|
>> MotionEvent.ACTION_POINTER_SHIFT);
|
||||||
|
ps.mXs.clear();
|
||||||
|
ps.mYs.clear();
|
||||||
|
ps.mVelocity = VelocityTracker.obtain();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (NP > event.getPointerCount()) {
|
||||||
|
NP = event.getPointerCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
mCurDown = action != MotionEvent.ACTION_UP
|
||||||
|
&& action != MotionEvent.ACTION_CANCEL;
|
||||||
|
|
||||||
|
for (int p=0; p<NP; p++) {
|
||||||
|
final PointerState ps = mPointers.get(p);
|
||||||
|
ps.mVelocity.addMovement(event);
|
||||||
|
ps.mVelocity.computeCurrentVelocity(1);
|
||||||
final int N = event.getHistorySize();
|
final int N = event.getHistorySize();
|
||||||
for (int i=0; i<N; i++) {
|
for (int i=0; i<N; i++) {
|
||||||
mXs.add(event.getHistoricalX(i));
|
ps.mXs.add(event.getHistoricalX(p, i));
|
||||||
mYs.add(event.getHistoricalY(i));
|
ps.mYs.add(event.getHistoricalY(p, i));
|
||||||
}
|
}
|
||||||
mXs.add(event.getX());
|
ps.mXs.add(event.getX(p));
|
||||||
mYs.add(event.getY());
|
ps.mYs.add(event.getY(p));
|
||||||
mCurDown = action == MotionEvent.ACTION_DOWN
|
ps.mCurX = (int)event.getX(p);
|
||||||
|| action == MotionEvent.ACTION_MOVE;
|
ps.mCurY = (int)event.getY(p);
|
||||||
mCurX = (int)event.getX();
|
//Log.i("Pointer", "Pointer #" + p + ": (" + ps.mCurX
|
||||||
mCurY = (int)event.getY();
|
// + "," + ps.mCurY + ")");
|
||||||
mCurPressure = event.getPressure();
|
ps.mCurPressure = event.getPressure(p);
|
||||||
mCurSize = event.getSize();
|
ps.mCurSize = event.getSize(p);
|
||||||
mCurWidth = (int)(mCurSize*(getWidth()/3));
|
ps.mCurWidth = (int)(ps.mCurSize*(getWidth()/3));
|
||||||
|
}
|
||||||
|
|
||||||
//if (mCurDown) {
|
//if (mCurDown) {
|
||||||
// mRect.union(mCurX-mCurWidth-3, mCurY-mCurWidth-3,
|
// mRect.union(mCurX-mCurWidth-3, mCurY-mCurWidth-3,
|
||||||
// mCurX+mCurWidth+3, mCurY+mCurWidth+3);
|
// mCurX+mCurWidth+3, mCurY+mCurWidth+3);
|
||||||
|
|||||||
Reference in New Issue
Block a user