Bulk Emailing

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Wednesday, 4 August 2010

Region Handling in Android Canvas

Posted on 22:44 by Unknown
In my previous post I have shared an Image Geo Tagging application for Android. A very important concept of Canvas Region handling was used in that application and thought lets discuss more on that concept. The concept of Region can be used in User Click event handling for any Canvas based drawing and Custom View.

Region is a section of Canvas area- its shape can be rectangular or non rectangular (i.e. circle, triangles, multi-phased polygons etc.). Android provides android.graphics.Region class, which has a very important and useful method contains(int x, int y);which easily detects whether your click event on the screen falls within the region or not. x and y are the co-ordinate position on the Screen.

How I can use it-
Suppose you have created a custom View and want to do specific operations depending upon which portion of screen user has clicked. Lets explain this with following screen shot-

Above screen is from my application (Image Geo Tagging for Android). Each Pin and its associated image thumbnail popups are part of a single Custom Overlay. Each Custom Overlay instance needs to detect when user has clicked on Pin and Popup, so that it can take appropriate action.

To handle this, we have to define 2 regions-
Region pinRegion = new Region (x1, y1, x1 + pinIcon.getWidth, y1 + pinIcon.getHeight());
Region popupRegion = new Region (x2, y2, x2 + popUpWidth, y2 + popUpHeight);

You need to now overwrite onTouchEvent( MotionEvent e, MapView mapView) method to get user Click events. Get the (x, y) co-ordinates from MotionEvent instance and use the magic function contains(x, y) e.g. pinRegion.contains(x, y) and popupRegion.contains(x, y).

This is really an important concept, if you are planning to control user Click events on specific sections of your Custom View or any Canvas based drawing. For complete code bases please refer my previous post
Email ThisBlogThis!Share to XShare to Facebook
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • CityWeather
    Update: Release 1.1 has been uploaded. It will now provide weekly forecast of your selected cities. Download   CityWeather is an Android...

Blog Archive

  • ►  2013 (6)
    • ►  September (2)
    • ►  May (1)
    • ►  April (1)
    • ►  February (1)
    • ►  January (1)
  • ►  2012 (4)
    • ►  July (2)
    • ►  March (1)
    • ►  January (1)
  • ►  2011 (11)
    • ►  November (1)
    • ►  October (2)
    • ►  August (1)
    • ►  June (1)
    • ►  April (2)
    • ►  March (3)
    • ►  January (1)
  • ▼  2010 (27)
    • ►  December (2)
    • ►  November (3)
    • ►  September (2)
    • ▼  August (4)
      • Overlay on Android Layout
      • Overlay on Android ListView
      • Region Handling in Android Canvas
      • Image Geo Tagging for Android
    • ►  July (4)
    • ►  June (7)
    • ►  May (5)
Powered by Blogger.

About Me

Unknown
View my complete profile