简体   繁体   中英

MotionEvent.ACTION_UP gives wrong coordinates?

I am trying my create a custom view with onTouchEvent() . I drew a circle and inside the MotionEvent.ACTION_UP of the onTouchEvent() listener, I change the (x,y) coordinates of the circle I want to draw to a new (x,y) coordinates of where I lift my finger.

But when I run the App, the circle is not getting drawn where I lift my finger?! it is getting drawn in somewhere else away from where my finger moved_up. Please see the code below and let me know what is wrong.

Note: I test my App on Galaxy Note3 "maybe it helps for anything"

Code :

public class CrazyEightsView extends View {

private Paint redPaint;
private int circleX; 
private int circleY;
private float radius;

public CrazyEightsView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub

    redPaint = new Paint();
    redPaint.setAntiAlias(true);
    redPaint.setColor(Color.RED);
    circleX = 100;
    circleY = 100;
    radius = 30;
}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    canvas.drawCircle(circleY, circleX, radius, redPaint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    int eventAction = event.getAction();
    int X = (int) event.getX();
    int Y = (int) event.getY();

    switch (eventAction) {

    case MotionEvent.ACTION_DOWN:
        break;
    case MotionEvent.ACTION_MOVE:
        break;
    case MotionEvent.ACTION_UP:
        circleX = X;
        circleY = Y;
        break;
    }

    invalidate();
    return true;
}

}

Just your coordinates of canvas.drawCircle(circleY, circleX, radius, redPaint); are reversed try this instead canvas.drawCircle(circleX, circleY, radius, redPaint); .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM