1

I need to apply click/touch events for only visible part of the View. Say for example a image of size 200X200. Apart from center 50X50, remaining part is transparent. I want to get touch events only for that 50X50 visible part Not on remaining transparent part.

enter image description here

In above image (its single image), only inner Diamond has got visible part. Apart from that Diamond is transparent area. So, if I touch Diamond then only I want to do something else ignore.

Edit :

Rachita's link helped me. I gone through that link and got idea how can I implement. But I could not understand some constants like 320, 240 etc while creating Points. In my case, I know the Diamond (in above image) x and y Ponits (hard coded values asctually). So, using those how can I determine, whether I touched inside Diamond or outside?

my Diamond points are as below

    pointA = new Point(0, 183);
    pointB = new Point(183, 0);
    pointC = new Point(366, 183);
    pointD = new Point(183, 366);

Edit :

Finally got solution from Luksprog. Its based on checking touched point pixel color. If color is 0 means, you touched transparent layer else you touched some colored part of the image. Simple, but very effective. Have a look at it here.

Community
  • 1
  • 1
Braj
  • 2,164
  • 2
  • 26
  • 44
  • are these objects x,x,x,x,V just an images or components? – deadfish Jun 25 '13 at 08:06
  • they r neither separate images nor components... its 1 whole image... I marked them in order to make people understand... I want to get notified only if I touch Diamond else ignore... – Braj Jun 25 '13 at 08:51
  • Also have a look at this: http://stackoverflow.com/questions/14362027/how-to-set-button-by-a-contour-polygon-android/14398104#14398104 – user Jun 25 '13 at 12:20
  • @Luksprog - That is quite interesting. Let me see. Thank u. – Braj Jun 25 '13 at 13:06
  • @Luksprog - I just can't believe it. Its working!!! Its exactly what I was looking for. Its so simple. Thank u very much. – Braj Jun 25 '13 at 13:22
  • Thank u every one who tried to help. – Braj Jun 25 '13 at 13:23

1 Answers1

4

AFAIK you can not implement this with onclick listener or my any other direct way .You will have to use onTouchListener .

  1. Firstly set your view dynamically at a specific (x,y) position using this How can I dynamically set the position of view in Android?

  2. Calculate the region your diamond will occupy (you should khow the size of image inorder to calculate area of diamond)

3.Trigger a action in onTouchListener only when x, y fall in the required region. Use How to get the Touch position in android?

check this link to calculate if a given point lies in the required square

EDIT

To understand the coordinate system of android refer to this link How do android screen coordinates work?

 Display mdisp = getWindowManager().getDefaultDisplay();
    int maxX= mdisp.getWidth();
    int maxY= mdisp.getHeight();

(x,y) :-

1) (0,0) is top left corner.

2) (maxX,0) is top right corner

3) (0,maxY) is bottom left corner

4) (maxX,maxY) is bottom right corner

here maxX and maxY are screen maximum height and width in pixels, which we have retrieved in above given code.

Remember if you want to support multiple devices with different screen sizes,make sure you use a relative value for x,y ie some ratio of screen height or width ,as different devices have different ppi

Check if touched point lies in the required polygon

I thinks these link might help you determining if the point touched (you can get x,y from onTouch event eg.event.getX()) lies in the required polygon whose points you have mentioned in the question . determine if a given point is inside the polygon and How can I determine whether a 2D Point is within a Polygon?

Community
  • 1
  • 1
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
  • That algorithm is interesting. Will try it. Thanks. – Braj Jun 21 '13 at 13:13
  • he might be able to check if the touch is on the diamond by investigating the bitmap being shown, depending on how he shows the diamond . i wonder how it should be done, if he uses a simple bitmap instead of drawing lines. – android developer Jun 24 '13 at 14:26
  • @androiddeveloper - I think am very close according to Rachita's link... only thing I did not understand in that is creating Points concept... in that link, they used some hard coded values like 320, 240 etc... If I could get that, remaining things r simple enough to understand and implement... – Braj Jun 25 '13 at 05:51
  • @Braj I thinks these link might help you determining if the point touched (you can get x,y from onTouch event eg.event.getX()) lies in the required polygon whose points you have mentioned in the question . http://stackoverflow.com/questions/217578/point-in-polygon-aka-hit-test and http://stackoverflow.com/questions/5223909/determine-if-a-given-point-is-inside-the-polygon – Rachita Nanda Jun 25 '13 at 06:03
  • @RachitaNanda - Thank u. I have quite idea about screen coordinates and all. Requirement here is quite different. Roughly, its like, making that Diamond as separate area say for ex, representing it as Rect and then checking whether touched point lies inside that area or not... Even if its hard-coding also, its ok... – Braj Jun 25 '13 at 08:54
  • AFAIK to mark the inner square as a separate area ,you will need to calculate vertices of inner polygon(points-x,y) and then using the above link i specified for algo to determine whether the touched point lies within polygon. For calculating vertices you should know the height ,width of the image .Pls put some of your code ,for better understanding .Are you setting images from drawable or as a bitmap? – Rachita Nanda Jun 25 '13 at 12:34