我想在屏幕上有四个不同的区域,并能够确定是否触摸了每个区域。每个区域都应该有一个相应的布尔值,如果触摸该值则为true。当一个区域为真时,画布的一部分将变为不同的颜色。
每个区域独立工作非常重要,因此如果区域1和区域2为真且用户放弃区域1,它将立即变为假而不影响区域2.
谢谢!
编辑: 我尝试了很多东西,但我刚刚重新开始。这是一个具有带有画布的SurfaceView的类。我无法弄清楚到底发生了什么。
public boolean onTouch(View v, MotionEvent ev) {
// TODO Auto-generated method stub
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
x = ev.getX();
y = ev.getY();
if (canvasHeight != 0 && canvasWidth != 0) {
if (x < canvasWidth/2 && y < canvasWidth/2){
x1 = x;
y1 = y;
}
if (x < canvasWidth && y > canvasHeight){
x2 = x;
y2 = y;
}
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
答案 0 :(得分:1)
以下是我解决问题的方法:
我希望这能让你开始!
<强>更新强>
我建议你从简单开始,不要使用多点触控。在触摸式方法中,您可以获得触摸的x
和y
值。一旦屏幕被触摸,您可以调用这样的方法,其中x和y是您的参数,例如。 checkRegion(x,y)
。
该方法可以返回该区域的int(因为在这种情况下,您一次只能触摸一个):
public int checkRegion(int x, int y) {
int clickedRegion;
// Some code that will return the region number: 1 = top left, 2 = top right, 3 = bottom left, 4 = bottom right
return clickedRegion;
}
答案 1 :(得分:0)
首先,您需要检查所有四个区域;
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
x = ev.getX();
y = ev.getY();
if (canvasHeight != 0 && canvasWidth != 0) {
//you need four sections, not the two?
if (x < canvasWidth/2 && y < canvasWidth/2){
//set your respective canvas color to what you want it for this quadrant
}
if (x < canvasWidth/2 && y > canvasWidth/2){
//set your respective canvas color to what you want it for this quadrant
}
if (x < canvasWidth && y > canvasHeight){
//set your respective canvas color to what you want it for this quadrant
}
if (x < canvasWidth && y < canvasHeight){
//set your respective canvas color to what you want it for this quadrant
}
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
现在,假设您可以使用它,您需要做的就是在action_pointer_down中再次执行此操作,它将注册为“次要”点击。
如果动作是Action_up,只需找出用户举起手指的位置并做同样的事情。你应该考虑的一个问题是这个;如果用户点击象限1,但在象限3中将其提升,该怎么办?这是一个稍微复杂的条件,但我认为你现在可以跳过它。