Java 按屏幕上的精确区域
这是我现在的代码:Java 按屏幕上的精确区域,java,android,bitmap,Java,Android,Bitmap,这是我现在的代码: public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); switch(action) { case MotionEvent.ACTION_DOWN: // Do click work here ...
public boolean onTouchEvent(MotionEvent event)
{
int action = event.getAction();
switch(action)
{
case MotionEvent.ACTION_DOWN:
// Do click work here ...
Y -= 10;
Yopen = 1;
break;
case MotionEvent.ACTION_UP:
// Do release work here ...
Yopen = 0;
break;
}
return super.onTouchEvent(event);
}
但我只想制作三个不同的区域来执行不同的代码。
有谁能帮我,一些关于互联网的好教程。将此代码添加到您的onTouchEvent中,以找出用户触摸的位置并做出适当的响应:
//Grab the current touch coordinates
float x = event.getX();
float y = event.getY();
//If you only want something to happen then the user touches down...
if (event.getAction() != MotionEvent.ACTION_UP) return true;
//If the user pressed in the following area, run it's associated method
if (isXYInRect(x, y, new Rect(x1, y1, x2, y2)))
{
//Do whatever you want for your defined area
}
//or, if the user pressed in the following area, run it's associated method
else if (isXYInRect(x, y, new Rect(x1, y1, x2, y2)))
{
//Do whatever you want for your defined area
}
以下是isXYinRect方法:
//A helper method to determine if a coordinate is within a rectangle
private boolean isXYInRect(float x, float y, Rect rect)
{
//If it is within the bounds...
if (x > rect.left &&
x < rect.right &&
y > rect.top &&
y < rect.bottom)
{
//Then it's a hit
return true;
}
//Otherwise, it's a miss
return false;
}
//确定坐标是否在矩形内的辅助方法
私有布尔值isXYInRect(浮点x、浮点y、Rect)
{
//如果在范围之内。。。
如果(x>矩形左&&
x矩形顶部&&
y<垂直底部)
{
//那就成功了
返回true;
}
//否则,这是一次失误
返回false;
}