Package xiaosi. grivaty;
Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. Canvas;
Import android. graphics. Rect;
Import android. view. MotionEvent;
Import android. view. View;
Public class Rects extends View
{
Private Bitmap bitmap = null;
Private float x, y;
Public Rects (Context context)
{
Super (context );
}
@ Override
Protected void onDraw (Canvas canvas)
{
Super. onDraw (canvas );
Bitmap = BitmapFactory. decodeResource (getResources (), R. drawable. v );
Canvas. drawBitmap (bitmap, 0, 0, null );
// Create a Rect in the same position as the bitmap
Rect rect = new Rect (0, 0, bitmap. getWidth (), bitmap. getHeight ());
If (rect. contains (int) x, (int) y )){
System. out. println ("in the range ");
}
Else {
System. out. println ("out of range ");
}
System. out. println ("Image Width:" + bitmap. getWidth () + "Image Height:" + bitmap. getHeight ());
System. out. println ("Click X:" + x + "Click Y:" + y );
}
@ Override
Public boolean onTouchEvent (MotionEvent event)
{
If (event. getAction () = MotionEvent. ACTION_DOWN)
{
X = event. getX ();
Y = event. getY ();
// Redraw
Invalidate ();
}
Return true;
}
}
From sunset hut