W
W
Wiemo2012-10-13 14:33:03
Android
Wiemo, 2012-10-13 14:33:03

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

4 answer(s)
W
Wiemo, 2012-10-13
@Wiemo

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);
        }
      }
    }
  }




  
}

Interestingly, drawing by the initial coordinates (specified in the declaration of variables) is correct.

P
palmut, 2012-10-13
@palmut

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).

W
Wiemo, 2012-10-13
@Wiemo

The same nonsense does not help

L
LeoCcoder, 2012-10-13
@LeoCcoder

First, make xx and yy volatile. Got better? Then read about multipokovy in java.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question