6.8.13. Overriding View Method onTouchEvent

In this example, we override View method onTouchEvent (Fig. 6.19) to determine when the user touches the screen. The MotionEvent parameter contains information about the event that occurred. Line 523 uses the MotionEvent’s getAction method to determine which type of touch event occurred. Then, lines 526–527 determine whether the user touched the screen (MotionEvent.ACTION_DOWN) or dragged a finger across the screen (MotionEvent.ACTION_MOVE). In either case, line 529 calls the cannonView’s fireCannonball method to aim and fire the cannon toward that touch point. Line 532 then returns true to indicate that the touch event was handled.


518      // called when the user touches the screen in this Activity
519      @Override
520      public boolean onTouchEvent(MotionEvent e)
521      {
522         // get int representing the type of action which caused this event
523         int action = e.getAction();
524
525         // the user user touched the screen or dragged along the screen
526         if (action == MotionEvent.ACTION_DOWN ||
527            action == MotionEvent.ACTION_MOVE )
528         {
529            fireCannonball(e); // fire the cannonball toward the touch point
530         }
531
532         return true;
533      } // end method onTouchEvent
534


Fig. 6.19 | Overriding View method onTouchEvent. .

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset