![](/img/trans.png)
[英]MotionEvent.ACTION_UP and/or ACTION_MOVE not always firing
[英]Motionevent.ACTION_UP firing constantly?
在我的程序中,我让它在手指向下移动时画一个矩形,而不是在手指向上移动时画一个矩形。 这是为了向用户显示他/她用作“猜测”以找到根的值的范围。 但是矩形永远不会显示! 但是,如果我删除了在“ action_up”部分中关闭矩形的调用,则用户可以绘制矩形。 这是代码:
在绘制函数中:
if(dataline>1)//if greater than 1, draw rectangle
{
myPaint.setColor(Color.CYAN);
canvas.drawRect(tX1,0, tX2,canvas.getHeight(),myPaint);
}
在运动事件函数中:public boolean onTouchEvent(MotionEvent ev){final int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
// Remember where we started
mLastTouchX = x;
mLastTouchY = y;
tX1=(int)ev.getX();
tX2=tX1;
x_1 = ev.getX();
x_1=(x_1-X1)/(zoom_x);
clicks= 1;
tX1=(int) ev.getX();//set first x coord
tX2=tX1;// make second x coord equal to the first
}
case MotionEvent.ACTION_MOVE: {
final float x = ev.getX();
final float y = ev.getY();
// Calculate the distance moved
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mLastTouchX = x;
mLastTouchY = y;
dataline=2;//let onDraw() draw the rectangle while dragging finger
tX2+= (int)dx;// find new second coordinate
}
case MotionEvent.ACTION_UP: {
dataline=0;//if commented out, rectangle is drawn otherwise, it is never seen.
}
}
return true;
}
问题解决了! 我了解到,您必须在每种情况下都放入return语句,否则它将仅在所有情况下运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.