Track pixel by pixel in Android

I am writing an Android application that requires very accurate motion measurements. Ideally, I would like the user to tap his finger on the screen and move one pixel, I can track it. I am currently redefining onTouch to track where the user is located. The problem is that when the finger quickly moves around the screen, onTouch skips as many as 15 pixels in motion. Is there a more accurate way to do this in onTouch?

This is an example of what I'm doing now:

@Override public boolean onTouch(View view, MotionEvent event){ if(event.getAction() == MotionEvent.ACTION_MOVE){ Log.d(TAG, event.getX() + ", " + event.getY(); } } 

Thanks.

+4
source share
2 answers

We had the same problem, and we could not get additional accuracy. I'm not sure if the structure will offer pixel-to-pixel motion. However, when the user makes precise movements at slower speeds, this tends to be more accurate. So, if the user has a finger down and literally moves one pixel, you should get this event. However, if they quickly swipe through 800 pixels, don't expect 800 events.

+2
source

Here is a little more accurate than just getX and getY: historical touch events. I am sure this works with SDK8, but did not test it on lower versions.

  @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); mCurDown = action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE; int N = event.getHistorySize(); for (int i=0; i<N; i++) { drawPoint(event.getHistoricalX(i), event.getHistoricalY(i), event.getHistoricalPressure(i), event.getHistoricalSize(i)); } drawPoint(event.getX(), event.getY(), event.getPressure(), event.getSize()); return true; } private void drawPoint(float x, float y, float pressure, float size) { mCurX = (int)x; mCurY = (int)y; mCurPressure = pressure; mCurSize = size; mCurWidth = (int)(mCurSize*(getWidth()/3)); if (mCurWidth < 1) mCurWidth = 1; if (mCurDown && mBitmap != null) { int pressureLevel = (int)(mCurPressure*255); mPaint.setARGB(pressureLevel, 255, 255, 255); mCanvas.drawCircle(mCurX, mCurY, mCurWidth, mPaint); mRect.set(mCurX-mCurWidth-2, mCurY-mCurWidth-2, mCurX+mCurWidth+2, mCurY+mCurWidth+2); invalidate(mRect); } } 
+3
source

Source: https://habr.com/ru/post/1383489/


All Articles