java - How to tell if an X and Y coordinate are inside my button? -


i have managed, great difficulty, make bitmap overlay screen. can touch input, gets touch input everywhere on screen.

i want know how able check if touch on bitmap, visible on screen.

the service , view class below. have thought , thought, couldn't think of way :(

package <package>;  import android.app.notificationmanager; import android.app.pendingintent; import android.app.service; import android.content.context; import android.content.intent; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.pixelformat; import android.graphics.rect; import android.os.ibinder; import android.support.v4.app.notificationcompat; import android.view.gravity; import android.view.motionevent; import android.view.viewgroup; import android.view.windowmanager; import android.widget.toast;  public class myservice extends service {     buttonview mview;     bitmap bit;      @override     public ibinder onbind(intent intent) {         return null;     }      @override     public void oncreate() {         super.oncreate();          bit = bitmapfactory.decoderesource(getresources(), r.drawable.button);          notificationcompat.builder builder = new notificationcompat.builder(                 this);         builder.setcontenttitle("ingress tools running");         builder.setcontenttext("click stop ingress tools");         builder.setsmallicon(r.drawable.ic_launcher);         builder.setcontentintent(pendingintent.getactivity(this, 0, new intent(                 this, stopactivity.class), 0));         notificationmanager manager = (notificationmanager) getsystemservice(context.notification_service);         manager.notify(1, builder.build());          mview = new buttonview(this, bit);         windowmanager.layoutparams params = new windowmanager.layoutparams(                 windowmanager.layoutparams.fill_parent,                 windowmanager.layoutparams.fill_parent,                 windowmanager.layoutparams.type_phone,                 windowmanager.layoutparams.flag_not_focusable                         | windowmanager.layoutparams.flag_not_touch_modal                         | windowmanager.layoutparams.flag_watch_outside_touch,                 pixelformat.translucent);         params.gravity = gravity.right;         params.settitle("load average");         windowmanager wm = (windowmanager) getsystemservice(window_service);         wm.addview(mview, params);      }      @override     public void ondestroy() {         super.ondestroy();         toast.maketext(getbasecontext(), "ondestroy", toast.length_long).show();         if (mview != null) {             ((windowmanager) getsystemservice(window_service))                     .removeview(mview);             mview = null;         }     } }  class buttonview extends viewgroup {     private paint mloadpaint;     private rect r;     private bitmap bit;      public buttonview(context context, bitmap bit) {         super(context);         toast.maketext(context, "hudview", toast.length_long).show();          mloadpaint = new paint();         mloadpaint.setantialias(true);         mloadpaint.settextsize(10);         mloadpaint.setargb(255, 255, 0, 0);         r = new rect();         r.set(380, 134, 468, 213);         this.bit = bit;     }      @override     protected void ondraw(canvas canvas) {         super.ondraw(canvas);         //canvas.drawcolor(color.black);          canvas.drawbitmap(bit, 100, 100, null);     }      @override     protected void onlayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {     }      @override     public boolean ontouchevent(motionevent event) {          int area = bit.getwidth() * bit.getheight();           //if (event.gety() <= maxy && event.getx() <= maxx) {             toast.maketext(getcontext(), "open tools: ", toast.length_long)                     .show();          //}         return true;      } } 

consider using framelayout (or other subclass of viewgroup) instead of viewgroup directly. because current implementation of onlayout method not correct, lead problems displaying of child views.

now, closer question. should ininitialize rect , store left, top, right , bottom position of bitmap. can see, you're initialized r variable, not using anywhere.

so, can initialize this:

r = new rect(100, 100, 100 + bit.getwidth(), 100 + bit.getheight()); 

now in ontouchevent can check:

r.contains((int) event.getx(), (int) event.gety()); 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -