Android的onTouchEvent坐标跳过各地 [英] Android onTouchEvent Coordinates Skip around

查看:140
本文介绍了Android的onTouchEvent坐标跳过各地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我我的应用程序,我有要求用户移动它在屏幕上,而要做到这一点,我正在使用的RelativeLayout和onTouchListener的元素。

这是我遇到的问题是,我的 onTouchListener ,我得到一些奇怪的结果为里面的getX()的getY()

我添加code的下面一行到我的 onTouch() - > ACTION_MOVE 块:

Log.v(榜样,触摸:{X:+ event.getX()+} {Y:+ event.getY()+});

下面是我的LogCat中的结果。


  

3月4日至29日:16:11.000:V /对myApp(24188):接触下来

  3月4日至29日:16:11.057:V /对myApp(24188):触摸:{X:69.78699} {Y:107.774216}

  3月4日至29日:16:11.103:V /对myApp(24188):触摸:{X:77.86926} {Y:173.37648}

  3月4日至29日:16:11.158:V /对myApp(24188):触摸:{X:69.78699} {Y:108.781845}

  3月4日至29日:16:1120.5:V /对myApp(24188):触摸:{X:77.86926} {Y:174.38597}

  3月4日至29日:16:11.463:V /对myApp(24188):触摸:{X:69.78699} {Y:109.64778}

  3月4日至29日:16:11.502:V /对myApp(24188):触摸:{X:77.86926} {Y:175.62172}

  3月4日至29日:16:1159.6:V /对myApp(24188):触摸:{X:69.23099} {Y:109.40666}

  3月4日至29日:16:11.654:V /对myApp(24188):触摸起来


正如你所看到的,非常缓慢地移动我的手指在一条直线上,我得到一个心惊肉跳结果,在正确的坐标实际上是每隔一行。

这是导致我的看法向后跳和第四所希望的位置,当我拖动它在屏幕上的偏移位置之间。

我检查已经添加,以确保我只创建一个单一的元素,这种监听器不被从任何地方另一种叫比这个单一视图。

有没有其他人看到过这个问题,并且有任何见解,以我可以做什么来解决它?

修改

  View.OnTouchListener layerMover =新View.OnTouchListener(){    @覆盖
    公共布尔onTouch(视图V,MotionEvent事件){        开关(event.getAction()){
        案例MotionEvent.ACTION_DOWN:
            Log.d(榜样,触摸下);
        案例MotionEvent.ACTION_UP:
            Log.d(榜样,润色);
        案例MotionEvent.ACTION_MOVE:
            Log.v(榜样,触摸:{X:+ event.getX()+} {Y:+ event.getY()+});
                v.setX(event.getX());
                v.setY(event.getY());            v.invalidate();
        }        返回true;    }
};


解决方案

我能够通过使用来解决这个问题 event.getRawX() event.getRawY()

I my app, I have an element that requires the user to move it around on the screen, and to do this I am using a RelativeLayout and on onTouchListener.

The problem that I am having, is that inside of my onTouchListener, I am getting some weird results for getX() and getY().

I added the following line of code into my onTouch() -> ACTION_MOVE block:

Log.v("example", "Touch: {x:" + event.getX() + " } {y:" + event.getY() + "}");

Below are my LogCat results.

04-29 03:16:11.000: V/myApp(24188): Touch Down!
04-29 03:16:11.057: V/myApp(24188): Touch: {x:69.78699 } {y:107.774216}
04-29 03:16:11.103: V/myApp(24188): Touch: {x:77.86926 } {y:173.37648}
04-29 03:16:11.158: V/myApp(24188): Touch: {x:69.78699 } {y:108.781845}
04-29 03:16:11.205: V/myApp(24188): Touch: {x:77.86926 } {y:174.38597}
04-29 03:16:11.463: V/myApp(24188): Touch: {x:69.78699 } {y:109.64778}
04-29 03:16:11.502: V/myApp(24188): Touch: {x:77.86926 } {y:175.62172}
04-29 03:16:11.596: V/myApp(24188): Touch: {x:69.23099 } {y:109.40666}
04-29 03:16:11.654: V/myApp(24188): Touch Up!

As you can see, moving my finger very slowly in a straight line, I get a jumpy result, where the correct coordinates are actually every other line.

This is resulting in my view jumping back and fourth between the desired position and the offset position as i drag it across the screen.

I have added in checks to be sure that I am only creating a single element, and this this listener is not being called from anywhere other than this single view.

Has anyone else seen this problem before, and have any insight as to what I can do to fix it?

EDIT

View.OnTouchListener layerMover = new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.d("example", "Touch down");
        case MotionEvent.ACTION_UP:
            Log.d("example", "Touch up");
        case MotionEvent.ACTION_MOVE:
            Log.v("example", "Touch: {x:" + event.getX() + " } {y:" + event.getY() + "}");
                v.setX(event.getX());
                v.setY(event.getY());

            v.invalidate();
        }

        return true;

    }
};

解决方案

I was able to solve this problem by using event.getRawX(), and event.getRawY().

这篇关于Android的onTouchEvent坐标跳过各地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆