我有这张照片(这是一张照片,而不是四张)。我想使其成为一个图像按钮。但是,我想将其分为四个部分,所以当我单击Paul时,它将为我打开有关Paul的新意向,对John,Ringo和George依此类推。我能做到吗?怎么样?
您可以使用View.OnTouchListener
从发生的点击中获取坐标。
View.OnTouchListener onTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
float x = event.getX();
int width = v.getWidth();
int oneFourthWidth = width / 4;
if (x <= oneFourthWidth) {
// John Lennon
} else if (x > oneFourthWidth && x <= oneFourthWidth * 2) {
// Paul McCartney
} else if (x > oneFourthWidth * 2 && x <= oneFourthWidth * 3) {
// George Harrison
} else if (x > oneFourthWidth * 3) {
// Ringo Starr
}
break;
}
return true;
}
};
yourView.setOnTouchListener(onTouchListener);
每个图像的精确坐标的计算取决于具体情况。