Android:似乎无法正确使用 MotionEvent.ACTION_MOVE
我对 Android 编程和 Java 很陌生(尽管我有一些 C# 经验,所以这会有所帮助)。
现在我正在闲逛一些事情来了解一切是如何运作的。我做了一个实现 onTouchListener 的活动。我已经重写了 onTouch 并打开了 event.getAction():
public boolean onTouch(View v, MotionEvent event)
{
float x;
float y;
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN: // gets called
{
x = event.getX();
y = event.getY();
circle c = new circle(this, x, y, 10, 0xFFFFFF);
_main.addView(c, tapCount++);
break;
}
case MotionEvent.ACTION_MOVE: // doesnt seem to do anything
{
x = event.getX();
y = event.getY();
circle c = new circle(this, x, y, 10, 0xFFFFFF);
_main.addView(c, tapCount++);
break;
}
}
return false;
}
其中“circle”是一个绘制圆圈的类。
当我在屏幕上拖动手指时,我期望看到的是一串圆圈。相反,只有当我开始触摸时才会绘制圆圈。
我将我的代码与示例进行了比较(例如:Google 的这篇博文:http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html ),我找不到我的错误。
编辑: 完整课程链接: http://pastebin.com/tVDQjQhu
编辑: 固定的。必须在 onTouch() 函数中返回 true。哦!
I am quite new to Android programming and Java (though I have some experience with C#, so that helps).
At this moment I'm goofing around with a couple of things to get to know how everything works. I've made an activity which implements onTouchListener. I've overridden onTouch and have a switch on event.getAction():
public boolean onTouch(View v, MotionEvent event)
{
float x;
float y;
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN: // gets called
{
x = event.getX();
y = event.getY();
circle c = new circle(this, x, y, 10, 0xFFFFFF);
_main.addView(c, tapCount++);
break;
}
case MotionEvent.ACTION_MOVE: // doesnt seem to do anything
{
x = event.getX();
y = event.getY();
circle c = new circle(this, x, y, 10, 0xFFFFFF);
_main.addView(c, tapCount++);
break;
}
}
return false;
}
Where "circle" is a class which draws a circle.
What I expected to see was a trail of circles as I dragged my finger over the screen. In stead, the circle is only being drawn when I start touching.
I have compared my code to examples (for example: this blogpost by Google: http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html ) and I can't find my mistake.
EDIT:
Link to the full class:
http://pastebin.com/tVDQjQhu
EDIT:
Fixed. One has to return true in the onTouch() function. d'oh!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在使用
MotionEvent.ACTION_MOVE
时遇到了同样的问题。要使
MotionEvent.ACTION_MOVE
正常工作,请返回 true 而不是 false。I was having the same problem while using
MotionEvent.ACTION_MOVE
.For
MotionEvent.ACTION_MOVE
to work, return true instead of false.对我来说,解决方案是
在 onTouch 函数中,因为当您从 onTouch(View v, MotionEvent event) 返回 false 时,您应该使用此
}
,那么只会调用 MotionEvent.ACTION_DOWN 。所以你应该从这个函数返回 true
希望这有帮助
For me Solution was
in the onTouch function as you should use this
}
when you return false from onTouch(View v, MotionEvent event) then only MotionEvent.ACTION_DOWN will be called. so you should return true from this function
Hope this is helps
也有同样的问题,我不知道这是否是一个错误,但我设法通过在实现中添加
OnClickListener
并实现public void onClick(View v )
然后还在构造函数上添加setOnClickListener(this)
。Had the same problems too, I don't know if it's a bug or not, but I managed to make it work by adding
OnClickListener
to the implementation, and implementingpublic void onClick(View v)
then also addingsetOnClickListener(this)
on the constructor.您必须对每个事件使用 return true。如果只使用 return 一次,则只有 Action_Down 会调用。
这会对你有所帮助。
You have to use return true for every event. If you use return only one time then only Action_Down will call.
This will help you.