MotionEvent.ACTION_UP给出了错误的坐标?

时间:2014-06-30 10:56:09

标签: android view ontouchevent ondraw touch-event

我正在尝试使用onTouchEvent()创建自定义视图。我画了一个圆圈并且在MotionEvent.ACTION_UP听众的onTouchEvent()内,我将要绘制的圆的(x,y)坐标更改为我提升的新(x,y)坐标我的手指。

但是当我运行应用程序时,在我抬起手指的地方没有画出圆圈?!它被吸引到远离我的手指移动的其他地方。请参阅下面的代码,让我知道出了什么问题。

注意:我在Galaxy Note3&#34上测试我的应用程序;也许它对任何事情都有帮助"

代码

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

}

1 个答案:

答案 0 :(得分:1)

只是你的canvas.drawCircle(circleY, circleX, radius, redPaint);坐标被颠倒了 试试这个canvas.drawCircle(circleX, circleY, radius, redPaint);