Android:似乎无法正确使用 MotionEvent.ACTION_MOVE

发布于 2024-11-14 07:12:31 字数 1329 浏览 2 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

习惯成性 2024-11-21 07:12:31

我在使用 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.

全部不再 2024-11-21 07:12:31

对我来说,解决方案是

return true

在 onTouch 函数中,因为当您从 onTouch(View v, MotionEvent event) 返回 false 时,您应该使用此

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 true; //the problem was here

}

,那么只会调用 MotionEvent.ACTION_DOWN 。所以你应该从这个函数返回 true

希望这有帮助

For me Solution was

return true

in the onTouch function as you should use this

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 true; //the problem was here

}

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

颜漓半夏 2024-11-21 07:12:31

也有同样的问题,我不知道这是否是一个错误,但我设法通过在实现中添加 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 implementing public void onClick(View v) then also adding setOnClickListener(this) on the constructor.

凝望流年 2024-11-21 07:12:31

您必须对每个事件使用 return true。如果只使用 return 一次,则只有 Action_Down 会调用。
这会对你有所帮助。

@Override
    public boolean onTouchEvent(MotionEvent event) {
        final float X = event.getX();
        final float Y = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x = X;
                y = Y;
                invalidate();
                return true;
            case MotionEvent.ACTION_MOVE:
                x = X;
                y = Y;
                invalidate();
                return true;
            case MotionEvent.ACTION_UP:
                invalidate();
                return true;
        }
        return super.onTouchEvent(event);
    }

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.

@Override
    public boolean onTouchEvent(MotionEvent event) {
        final float X = event.getX();
        final float Y = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x = X;
                y = Y;
                invalidate();
                return true;
            case MotionEvent.ACTION_MOVE:
                x = X;
                y = Y;
                invalidate();
                return true;
            case MotionEvent.ACTION_UP:
                invalidate();
                return true;
        }
        return super.onTouchEvent(event);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文