Android ACTION_POINTER_DOWN不改变布尔值

时间:2014-01-25 17:20:04

标签: android boolean multi-touch motionevent

我一直在尝试检测自定义视图的不同区域的多个触摸。 触摸特定区域时,计数器增加,布尔值设置为true。可以同时触摸多个区域,只有当所有指针都被移除时,屏幕上才会显示特定的Toast。

当触摸屏幕时,计数器正常上下移动但布尔值没有变化......我不明白为什么。

这是我的代码

public class Multitouch extends View {

private Context c;
private int cuenta;
public boolean b1;
public boolean b2;
public boolean b3;
public boolean b4;
public boolean b5;
public boolean b6;
public boolean be;

public Multitouch(Context context, AttributeSet attrs)
{
    super(context, attrs);
    c = context;
    cuenta = 0;
    b1 = false;
    b2 = false;
    b3 = false;
    b4 = false;
    b5 = false;
    b6 = false;
}

@Override
public boolean onTouchEvent(MotionEvent event)
{
    switch (event.getActionMasked())
    {
        case MotionEvent.ACTION_POINTER_DOWN:
        case MotionEvent.ACTION_DOWN:
            cuenta++;
            Toast.makeText(c, "x: " + event.getX() + " y: " + event.getY(), Toast.LENGTH_SHORT).show();
            if ((event.getX() >= 630) && (event.getX() <= 800) 
                    && (event.getY() >= 0) && (event.getY() <= 130))
                b1 = true;
            else if ((event.getX() >= 630) && (event.getX() <= 800) 
                    && (event.getY() >= 131) && (event.getY() <= 265))
                b2 = true;
            else if ((event.getX() >= 630) && (event.getX() <= 800) 
                    && (event.getY() >= 266) && (event.getY() <= 390))
                b3 = true;
            else if ((event.getX() >= 0) && (event.getX() <= 170) 
                    && (event.getY() >= 0) && (event.getY() <= 130))
                b4 = true;
            else if ((event.getX() >= 0) && (event.getX() <= 170) 
                    && (event.getY() >= 131) && (event.getY() <= 265))
                b5 = true;
            else if ((event.getX() >= 0) && (event.getX() <= 170) 
                    && (event.getY() >= 266) && (event.getY() <= 390))
                b6 = true;

            Toast.makeText(c, ""+b1+b2+b3+b4+b5+b6+cuenta, Toast.LENGTH_SHORT).show();
            break;

        case MotionEvent.ACTION_POINTER_UP:
            cuenta--;
            break;

        case MotionEvent.ACTION_UP:
            cuenta--;
            if (cuenta == 0)
            {
                if(b1 && !b2 && !b3 && !b4 && !b5 && !b6)
                    Toast.makeText(c, "hello", Toast.LENGTH_SHORT).show();
                if(b1 && b2 && !b3 && !b4 && !b5 && !b6)
                    Toast.makeText(c, "how", Toast.LENGTH_SHORT).show();    
                if(b1 && !b2 && !b3 && b4 && !b5 && !b6)
                    Toast.makeText(c, "are", Toast.LENGTH_SHORT).show();
                if(b1 && b2 && !b3 && b4 && !b5 && !b6)
                    Toast.makeText(c, "you", Toast.LENGTH_SHORT).show();
            }
            b1 = false;
            b2 = false;
            b3 = false;
            b4 = false;
            b5 = false;
            b6 = false;

            break;

        default:
            break;
    }
    return true;
}
...
}

始终触发Toast.makeText(c, ""+b1+b2+b3+b4+b5+b6+cuenta, Toast.LENGTH_SHORT).show(),表示MotionEvent.ACTION_POINTER_DOWNMotionEvent.ACTION_DOWN正在运行,并显示布尔值。

任何想法? 提前致谢

0 个答案:

没有答案