首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MotionEvent问题

MotionEvent问题
EN

Stack Overflow用户
提问于 2011-09-25 20:51:34
回答 2查看 2.2K关注 0票数 1

我想知道如何获取MotionEvent的精确值、get(x)和get(y)值?正在发生的事情是,当我触摸屏幕上的特定区域时,我告诉发生一个动作。问题是,一旦我触摸屏幕并将手指移开,它仍然认为我的手指在相同的位置(因为那是我最后一次触摸的位置)。因此,当我有一个以上的Down事件(用于多点触摸)时,它会抛出所有东西。有没有办法重置X和Y值,这样当我离开屏幕时,它们会回到0或null (或其他值)?

这是我刚刚上传的一段视频,为了更好地解释它,因为它有点令人困惑

http://www.youtube.com/watch?v=OHGj2z5SwQs

下面是我正在使用的代码

代码语言:javascript
复制
    int x = (int) e.getX();
    int y = (int) e.getY();
    int x2 = (int) e.getX(1);
    int y2 = (int) e.getY(1);


    boolean a1 = y > 0 && y < 200....etc        

    boolean a5 = etc... 

    switch (e.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        x =  0;
        y = 0;          
        x2 = 0;
        y2 = 0;
                    ////I'm setting the x and y values to 0 as suggested

        text.setText("x:" + String.valueOf(x) + "y:" + String.valueOf(y));
                    //// This is so I can see the values on the screen
        if (a1 && a5){
            viewA1.setBackgroundColor(Color.GREEN);
            viewA5.setBackgroundColor(Color.GREEN);
        }
        if (a1) {

            viewA1.setBackgroundColor(Color.GREEN);
        }



        else if (a5) {
            viewA5.setBackgroundColor(Color.GREEN);

        }           



        break;

    case MotionEvent.ACTION_POINTER_1_DOWN:
        // /A Strummer
        x =  0;
        y = 0;

        x2 = 0;
        y2 = 0;

        text1.setText("x:" + String.valueOf(x2) + "y:" + String.valueOf(y2));
        if (a1 && a5){

            viewA1.setBackgroundColor(Color.GREEN);
            viewA5.setBackgroundColor(Color.GREEN);

        }
        if (a1) {

            viewA1.setBackgroundColor(Color.GREEN);
        }



        else if (a5) {

            viewA1.setBackgroundColor(Color.GREEN);

        }       
     /////I have pretty much the same method for ACTION_UP & ACTION_POINTER_UP; I set x & y to 0.

如果你能想到什么请告诉我。我尝试了你们解释的方法,看起来会有帮助,但事实并非如此。

EN

回答 2

Stack Overflow用户

发布于 2011-09-28 08:36:07

我没有技巧,但是有一个有效的解决方案和一些多点触控入门的技巧。我以前从来没有这样做过,所以在看到你的问题后,我渴望知道多点触控是如何工作的。实际上,要把它做好是相当棘手的。这是startes的一个很好的例子(带有四个角的视图,当其中一个指针接触到它时会着色-使用一个到五个指针/手指)。因此,我将基于下面发布的工作解决方案对其进行解释。

基本上它是这样工作的:getPointerCount()会给你当前接触的指针的数量。getX(i)getY(i)会给你相应的坐标。但是这部分很棘手:指针0,1,2是接触的,你抬起1 (第二个手指),现在你有0,1。这是getPointerId(1)将返回2的点,因为这仍然是你的指针2,它正在接触,但它现在在位置1而不是2。你只有一次机会对这种变化做出反应,那就是触发ACTION_POINTER_UP。在此操作和ACTION_POINTER_DOWN上,getActionIndex()将返回指针事件列表中添加或删除的指针的操作指针位置。否则,它总是返回0,这是没有人告诉你的事情。

结论是,您实际上无法跟踪哪个指针正在移动(这就是为什么getActionIndex()在移动时返回0)。这似乎是荒谬的逻辑,因为为每个手指触发5个不同的事件将是愚蠢的。但从多点触摸开始,这是一个非常重要的事实;-)

下面是示例。coordsXcoordsY将跟踪指针坐标,如果位置2包含有效坐标,这意味着无论您是否抬起其他手指,这都是您的第三个手指仍在触摸屏幕。这在这种情况下可能不相关,但这是一个事实,在其他多点触摸实现中仍然有用。

代码语言:javascript
复制
public class MultitouchView extends LinearLayout {

    // we have a maximum of 5 pointer
    // we will set Integer.MIN_VALUE if the pointer is not present
    private int [] coordsX = new int [5];
    private int [] coordsY = new int [5];

    // add 4 views with a certain gravity so they are placed in corners
    public MultitouchView(Context context, AttributeSet attrs) {
        super(context, attrs);

        // initialize as not present
        Arrays.fill(coordsX, Integer.MIN_VALUE);
        Arrays.fill(coordsY, Integer.MIN_VALUE);


        /* the layout is inflated from a XML file and is basically this one:
         * all subviews are matching / filling parent and have a weight of 1
         * <LinearLayout vertical>
         *   <LinearLayout horizontal>
         *     <View /><View />
         *   </LinearLayout>
         *   <LinearLayout horizontal>
         *     <View /><View />
         *   </LinearLayout>
         * </LinearLayout>
         */
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        if (e.getAction() == MotionEvent.ACTION_CANCEL) {
            // every touch is going to be canceled, clear everything
            Arrays.fill(coordsX, Integer.MIN_VALUE);
            Arrays.fill(coordsY, Integer.MIN_VALUE);
        } else {
            // track all touches
            for (int i = 0; i < e.getPointerCount(); i++) {
                int id = e.getPointerId(i);

                if (e.getActionIndex() == i
                        && (e.getActionMasked() == MotionEvent.ACTION_POINTER_UP
                        || e.getActionMasked() == MotionEvent.ACTION_UP)) {
                    // pointer with this id is about to leave, clear it
                    coordsX[id] = coordsY[id] = Integer.MIN_VALUE;
                } else {
                    // update all other pointer positions
                    coordsX[id] = (int) e.getX(i);
                    coordsY[id] = (int) e.getY(i);
                }
            }
        }

        int hw = getWidth() / 2;
        int hh = getHeight() / 2;

        // first no corner is touched
        boolean topLeft = false, topRight = false, bottomRight = false, bottomLeft = false;

        // check if any of the given pointer is touching a certain corner
        for (int i = 0; i < coordsX.length; i++) {
            // pointer is not active (anymore)
            if (coordsX[i] == Integer.MIN_VALUE || coordsY[i] == Integer.MIN_VALUE) {
                continue;
            }

            topLeft = topLeft || coordsX[i] < hw && coordsY[i] < hh;
            topRight = topRight || coordsX[i] > hw && coordsY[i] < hh;
            bottomRight = bottomRight || coordsX[i] > hw && coordsY[i] > hh;
            bottomLeft = bottomLeft || coordsX[i] < hw && coordsY[i] > hh;
        }

        // set the result we have now to the views represnting the corners
        ((ViewGroup) getChildAt(0)).getChildAt(0).setBackgroundColor(topLeft ? 0xffffff00 : 0x0);
        ((ViewGroup) getChildAt(0)).getChildAt(1).setBackgroundColor(topRight ? 0xffff0000 : 0x0);
        ((ViewGroup) getChildAt(1)).getChildAt(0).setBackgroundColor(bottomLeft ? 0xff00ff00 : 0x0);
        ((ViewGroup) getChildAt(1)).getChildAt(1).setBackgroundColor(bottomRight ? 0xff0000ff : 0x0);

        return true;
    }
}
票数 5
EN

Stack Overflow用户

发布于 2011-09-25 21:06:34

您应该使用‘motionEvent.getPoinerCount()’来检查接触点的数量。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7545591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档