[英]MotionEvent.ACTION_UP gives wrong coordinates?
I am trying my create a custom view with onTouchEvent()
. 我正在尝试使用
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. 我画了一个圆圈,里面
MotionEvent.ACTION_UP
的的onTouchEvent()
监听器,我改变(X,Y)我想提请到一个新的(X,Y)的圆的坐标,我抬起我的手指坐标。
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" 注意:我在Galaxy Note3上测试了我的应用程序“也许对任何事情都有帮助”
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);
只是您的
canvas.drawCircle(circleY, circleX, radius, redPaint);
are reversed try this instead canvas.drawCircle(circleX, circleY, radius, redPaint);
反向尝试使用
canvas.drawCircle(circleX, circleY, radius, redPaint);
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.