Answer the question
In order to leave comments, you need to log in
Y-coordinate offset when passed from UI thread?
The problem is this: there is a thread in which the circle is drawn.
In the ui thread (main activity class), the onTouchEvent method is called, which takes the x,y coordinates and passes them to the drawing thread (by direct assignment to the thread class), as a result, everything is fine for x, and the y coordinate is shifted by some value, i.e. the circle does not appear under the finger, but below. Or do you still need to use Handler or AsyncTask, and not stupidly assign
public boolean onTouchEvent(MotionEvent event) {
int X = (int) event.getX();
int Y = (int) event.getY();
myThread.xx=X;
myThread.yy=Y;
return true;
}
Answer the question
In order to leave comments, you need to log in
class DrawThread extends Thread {
private boolean Flag = false;
private SurfaceHolder surfaceHolder;
public DrawThread(SurfaceHolder surfaceHolder, Resources resources) {
this.surfaceHolder = surfaceHolder;
}
public void setRunning(boolean run) {
Flag = run;
}
static boolean Flag2 = true;
Canvas canvas;
Paint paint = new Paint();
static int xx = 20;
static int yy = 20;
public void run() {
while (Flag) {
canvas = null;
try {
canvas = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
paint.setColor(Color.GREEN);
if (Flag2) {
canvas.drawCircle(xx, yy, 20, paint);
}
}
} finally {
if (canvas != null) {
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
Also try multiplying getX() and getY() by getXPrecision() and getYPrecision() respectively, since those functions return the precision of the Y coordinates being reported. You can multiply this number with getY() to find the actual hardware value of the Y coordinate. (quote from the MotionEvent documentation).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question