我正在使用位图将许多图像绘制到画布上。我希望这些位图可以点击..所以我使用rects绘制它们以便稍后我可以检查触摸点是否在OnTouch方法中是否在这些rects内.. < / p>
但是如果我触摸位图上的任何地方,rect.contains(x,y)方法总是返回false ..如果触摸点位于位图之上,它返回true,就好像位图和矩形正在共享相同的点(左,上),但向下绘制位图,向上绘制矩形.. 你能告诉我吗?
这是我的drawCanvas代码:
private void drawCanvas(int position) {
Bitmap icon9 = BitmapFactory.decodeResource(getResources(), R.drawable.camera_icon);
Bitmap icon9Scaled = Bitmap.createScaledBitmap(icon9, 60, 60, true);
icon9.recycle();
rect9 = new Rect();
rect9.set(15, 330, 15 + icon9Scaled.getWidth(), 330 + icon9Scaled.getHeight());
// src9 = new Rect(0, 0, 60, 60);
canvas.drawBitmap(icon9Scaled, null, rect9, null);
}
这是我的OnTouche方法:
@Override
public boolean onTouch(View v, MotionEvent event) {
//super.onTouchEvent(event);
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
// Toast.makeText(getApplicationContext(), "Is TOUCHED", Toast.LENGTH_LONG).show();
if(rect9.contains(x,y))
Toast.makeText(getApplicationContext(), "Icon 9 Is TOUCHED", Toast.LENGTH_LONG).show();
Log.d("Is Icon 9 Touched", Boolean.toString(rect9.contains(x,y)));
Log.d("Is Icon 9 Touched X", Integer.toString(x));
Log.d("Is Icon 9 Touched Y", Integer.toString(y));
break;
}
return true;
}