android - Get View position -


i developing simple app produced bubbles on screen on touch. bubble move around on screen , popped when reaches border of screen or if user touches it. have coded bubble pop when reaches borders of screen can't figure out way detect if user touched it.

i want detect if user touched bubble on screen.

note:- bubbles created using custom view. have included important functions can include whole code if want. here's code

public class bubbleactivity extends activity {  // these variables testing purposes, not modify private final static int random = 0; private final static int single = 1; private final static int still = 2; private static int speedmode = random;  private static final int menu_still = menu.first; private static final int menu_single_speed = menu.first + 1; private static final int menu_random_speed = menu.first + 2;  private static final string tag = "lab-graphics";  // main view private relativelayout mframe;  // bubble image private bitmap mbitmap;  // display dimensions private int mdisplaywidth, mdisplayheight;  // sound variables  // audiomanager private audiomanager maudiomanager; // soundpool private soundpool msoundpool; // id bubble popping sound private int msoundid; // audio volume private float mstreamvolume;  // gesture detector private gesturedetector mgesturedetector;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      setcontentview(r.layout.main);      // set user interface     mframe = (relativelayout) findviewbyid(r.id.frame);      // load basic bubble bitmap     mbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.b64);  }  @override protected void onresume() {     super.onresume();      // manage bubble popping sound }  @override public void onwindowfocuschanged(boolean hasfocus) {     super.onwindowfocuschanged(hasfocus);     if (hasfocus) {          // size of display view knows borders         mdisplaywidth = mframe.getwidth();         mdisplayheight = mframe.getheight();      } }  // set gesturedetector private void setupgesturedetector() {      mgesturedetector = new gesturedetector(this,              new gesturedetector.simpleongesturelistener() {                  // detecting if user touched bubble here                  @override                 public boolean onsingletapconfirmed(motionevent event) {                      // trying bubble position can't x=0, y=0 tried                     // many things                     log.d(tag,""+((viewgroup)mframe).getchildcount());   for(int i=0; i<((viewgroup)mframe).getchildcount(); ++i) {                             view nextchild = ((viewgroup)mframe).getchildat(i);                             rect rect = new rect();                             nextchild.getlocalvisiblerect(rect);                             int[] location = new int[2];                              nextchild.getlocationonscreen(location);                             log.d(tag, "x = " + location[0] + " y = " + location[1]);                         }                     if(event.getaction() == motionevent.action_down){                         bubbleview bubbleview = new bubbleview(getapplicationcontext(), event.getx(),event.gety());                         bubbleview.start();                         mframe.addview(bubbleview);                     }                      return true;                 }             }); }  @override public boolean ontouchevent(motionevent event) {      // todo - delegate touch gesturedetector       return mgesturedetector.ontouchevent(event);  }  @override protected void onpause() {      // todo - release soundpool resources      super.onpause(); }  // bubbleview view displays bubble. // class handles animating, drawing, popping amongst other actions. // new bubbleview created each bubble on display  private class bubbleview extends view {      private static final int bitmap_size = 64;     private static final int refresh_rate = 40;     private final paint mpainter = new paint();     private scheduledfuture<?> mmoverfuture;     private int mscaledbitmapwidth;     private bitmap mscaledbitmap;      // location, speed , direction of bubble     private float mxpos, mypos, mdx, mdy;     private long mrotate, mdrotate;      public bubbleview(context context, float x, float y) {         super(context);         log("creating bubble at: x:" + x + " y:" + y);          // create new random number generator         // randomize size, rotation, speed , direction         random r = new random();          // creates bubble bitmap bubbleview         createscaledbitmap(r);          // adjust position center bubble under user's finger         mxpos = x - mscaledbitmapwidth / 2;         mypos = y - mscaledbitmapwidth / 2;          // set bubbleview's speed , direction         setspeedanddirection(r);          // set bubbleview's rotation         setrotation(r);          mpainter.setantialias(true);      }       // start moving bubbleview & updating display     private void start() {          // creates workerthread         scheduledexecutorservice executor = executors                 .newscheduledthreadpool(1);          // execute run() in worker thread every refresh_rate         // milliseconds         // save reference job in mmoverfuture         mmoverfuture = executor.schedulewithfixeddelay(new runnable() {             @override             public void run() {                 // todo - implement movement logic.                 // each time method run bubbleview should                 // move 1 step. if bubbleview exits display,                 // stop bubbleview's worker thread.                 // otherwise, request bubbleview redrawn.                 if(!isoutofview()){                     movewhileonscreen();                 }                 else{                     stop(true);                 }              }         }, 0, refresh_rate, timeunit.milliseconds);     }      private synchronized boolean intersects(float x, float y) {          // todo - return true if bubbleview intersects position (x,y)          return false;     }      // cancel bubble's movement     // remove bubble mframe     // play pop sound if bubbleview popped      private void stop(final boolean popped) {          if (null != mmoverfuture && mmoverfuture.cancel(true)) {              // work performed on ui thread              mframe.post(new runnable() {                 @override                 public void run() {                      // todo - remove bubbleview mframe                      if (popped) {                         log("pop!");                          // todo - if bubble popped user,                         // play popping sound                         mframe.removeview(bubbleview.this);                         //mmoverfuture.cancel(true);                         msoundpool.play(msoundid, 1, 1, 1, 0, 1);                     }                      log("bubble removed view!");                  }             });         }     }      // change bubble's speed , direction     private synchronized void deflect(float velocityx, float velocityy) {         log("velocity x:" + velocityx + " velocity y:" + velocityy);          //todo - set mdx , mdy new velocities divided refresh_rate          mdx = velocityx/refresh_rate;         mdy = velocityy/refresh_rate;      }      // draw bubble @ current location     @override     protected synchronized void ondraw(canvas canvas) {          // todo - save canvas         canvas.save();          // todo - increase rotation of original image mdrotate         mrotate = mrotate + mdrotate;          // todo rotate canvas current rotation         canvas.rotate(mrotate, mxpos + mscaledbitmapwidth/2, mypos + mscaledbitmapwidth/2);          // todo - draw bitmap @ it's new location         canvas.drawbitmap(mscaledbitmap, mxpos, mypos,mpainter);           // todo - restore canvas         canvas.restore();        }       private synchronized boolean movewhileonscreen() {          // todo - move bubbleview         // returns true if bubbleview has exited screen         mxpos = mdx+mxpos;         mypos = mdy+mypos;          postinvalidate();         return false;     }      private boolean isoutofview() {          // todo - return true if bubbleview has exited screen         if(mxpos + mscaledbitmapwidth/2 >= mdisplaywidth - mscaledbitmapwidth/2 || mxpos  <0                 ||mypos + mscaledbitmapwidth/2 >= mdisplayheight - mscaledbitmapwidth/2 || mypos  <0){             return true;         }         return false;      } } 

update :-

to clarify bit, want location of bubbles on screen , compare them event.getx() , event.gety() detect if tapped on bubble. ii have check bubble tap in onsingletapconfirmed(). correctly able total number of bubbles can't detect location on screen.

for(int i=0; i<((viewgroup)mframe).getchildcount(); ++i) {                             view nextchild = ((viewgroup)mframe).getchildat(i);                             rect rect = new rect();                             nextchild.getlocalvisiblerect(rect);                             int[] location = new int[2];                              nextchild.getlocationonscreen(location);                             log.d(tag, "x = " + location[0] + " y = " + location[1]);                         } 

above code gives correct number of bubbles return coordinates 0,0.

in onsingletapconfirmed function, try following iterate through bubbleviews , pass event x , y coordinates on.

for(int i=0;i<mframe.getchildcount();i++){     bubbleview bubblethis = (bubbleview) mframe.getchildat(i);     if (bubblethis.intersects(event.getx(),event.gety())){         bubblethis.stop(true);         return true;     } } 

the function in bubbleview should return true if x , y fall inside boundaries. add function inside intersects function in bubbleview clarification:

private synchronized boolean intersects(float x, float y) {     if ( (x>mxpos && x<(mxpos+mscaledbitmapwidth)) && (y>mypos && y<(mypos+mscaledbitmapwidth)) ) {         return true;     }     return false; } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -