- 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 rect with bitmap-like position
- Rect rect = new Rect (0, 0, Bitmap.getwidth (), Bitmap.getheight ());
- if (rect.contains (int) x, (int) y)) {
- System.out.println ("within");
- }
- else{
- System.out.println ("outside the Scope");
- }
- System.out.println ("Picture 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;
- }
- }
Click on Android development experience to determine if the image is within the range