title: Good way to check if a view is pressed
body: <p>I'm trying to manage a multi task app that works with many buttons.</p>
  
  <p>For this purpose i made this function: </p>
  
  <pre><code>public boolean isTouched(View view, MotionEvent event) {
      boolean touched = false;
      int count = event.getPointerCount();
  
      int[] location = { 0, 0 };
      view.getLocationOnScreen(location);
      Point min = new Point(location[0], location[1]);
      Point max = new Point(min.x + view.getWidth(), min.y + view.getHeight());
  
      for (int i = 0; i &lt; count; i++) {
  
  
          int rawX = (int) event.getX(i);
          int rawY = (int) event.getY(i);
          if(rawX&gt;=min.x &amp;&amp; rawX&lt;=max.x &amp;&amp; rawY&gt;=min.y &amp;&amp; rawY&lt;=max.y){
              //Log.d("mylog", "***Found a view: " + v.getId());
              touched = true;
              if (event.getAction() == MotionEvent.ACTION_UP) touched = false;
          }
      }
      return touched;
      }
  </code></pre>
  
  <p>And the function override of the activity that intercept onTouchEvent and manage two ImageView</p>
  
  <pre><code>@Override
  public boolean onTouchEvent (MotionEvent event) {
  
      if (isTouched(button1,event)) {
          // it is pressed
          if ((Integer)button1.getTag() == 0) {  // but before wasn't pressed
              mp1.start();
              button1.setBackgroundResource(R.drawable.happy_on);
              button1.setTag(1);
  
              // start music 1
          }
      } else {
          // it isn't pressed
          if ((Integer)button1.getTag() == 1) {   // and before it was pressed
              mp1.stop();
              try {
                  mp1.prepare();
              } catch (IOException e) {
                  e.printStackTrace(); 
              }  catch (IllegalStateException e) {
                  Log.e("hb","illegal state exception mp2") ;
                  mp1= MediaPlayer.create(getBaseContext(), R.raw.female01) ;
  
              }
  
              button1.setBackgroundResource(R.drawable.happy_off);
              button1.setTag(0);
              // stop music 1
          }
      }
  
           if (isTouched(button2,event)) {
               // it is pressed
               if ((Integer)button2.getTag() == 0) {  // but before wasn't pressed
                   mp2.start();
  
                   button2.setBackgroundResource(R.drawable.to_on);
                   button2.setTag(1);
                   // start music 2
               }
  
           } else {
               // it isn't pressed
               if ((Integer)button2.getTag() == 1) {   // and before it was pressed
                   mp2.stop();
                   try {
                       mp2.prepare();
                   } catch (IOException e) {
                       e.printStackTrace(); 
                   } catch (IllegalStateException e) {
                       Log.e("hb","illegal state exception mp2") ;
                       mp2= MediaPlayer.create(getBaseContext(), R.raw.female02) ;                     }
  
                   button2.setBackgroundResource(R.drawable.to_off);
                   button2.setTag(0);
                   // stop music 1
               }
           }
  
      return false;
  
      }
  </code></pre>
  
  <p>Buttons (i used imageview) are pressed like a piano keyboard: you can pass it over with one, two finger and a async action starts when the pointer enter in the view. At event_up or pointer leave the button, action stop.</p>
  
  <p>It works, but it is a very slow method: it is flooded by many action_move events.</p>
  
  <p>Can anybody know a method to optimize this way of multi touch managing?</p>
  
  <p>EDIT: mp1 and mp2 are mp3 mediaplayer objects. When i'm passing from a button to another button without release the pointer it works, but there is a some-milliseconds gap that make the melody jump. The effect i would to make is like the keyboard of a piano, without gaps.</p>
  
  <p>I think that the gap is produced for the weight of the function isTouched. How can I reduce this weigth?</p>
  
  <p>Thank you</p>