0

I have a ViewGroup which fills a part of it's size, see the picture. Further this ViewGroup implements onTouchListener.

Is there an easy way to find out, whether a transparent or the filled part was touched? Something like: ViewGroup.hasSomePixels(relativeX,relativeY) ?

see this image

Skip
  • 6,240
  • 11
  • 67
  • 117
  • I'm looking for a method, if there will be any pixels, when the view will be drawn on point x,y , if possible without previeous knowledge how the view will look like. Just looking in which half the click occured wont work with complicated shapes. – Skip Nov 11 '11 at 15:44
  • For the future I will avoid this situation by wrapping every object in an own `View`, so that android can forward touch events automatically to the top visible `View` – Skip Nov 11 '11 at 17:03

4 Answers4

2

If you implement the onTouch method you will see that you get a MotionEvent, when the view is touched.

onTouch(View v, MotionEvent event) {...}

the MotionEvent gives you information about the touch position. You can get the raw pixel coordinates on the screen with

event.getRawX();
event.getRawY();

or the coordinates relative to the view with

event.getX();
event.getY();

take a look at the MotionEvent documentation to learn more about it.

After that you can get the drawing cache and get the pixel value of the position.

this.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(this.getDrawingCache());
this.setDrawingCacheEnabled(false);
int color = bitmap.getPixel(x, y);

Then you have to check if the color is transparent or not.

Franziskus Karsunke
  • 4,948
  • 3
  • 40
  • 54
  • I dont need the coordinates, I want to know whether the coordinates on this canvas's view will be filled or not, when onDraw will be called. – Skip Nov 11 '11 at 15:41
2

I presume the image is a bitmap or a picture right? Then you can just do:

ImageView imageView = ((ImageView)v);
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);

and then decode the colour using Color.red(pixel) etc.. Admittedly, you'll need to keep track of any offsets (if the image is too large for the screen and can be dragged etc, but it's possible.

If you're dealing with a Canvas object, remember it just writes it all to a bitmap, so you can still do the above method (once you've accounted for offsets).

Dororo
  • 3,420
  • 2
  • 30
  • 46
  • The picture is drawn on the canvas dynamically, by using paths and filling them. Have many different Paths, no chance to check 1 Object. They come together on the canvas, `onDraw` – Skip Nov 11 '11 at 16:07
0

You can get point that was touched by calling: event.getX() and event.getY(). You can get dimensions of the screen by calling:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();

You could do something like:

if (event.getY() <= height / 2) {
     //upper part of the screen: green was touched
} else {
     //white was touched
}
Maggie
  • 7,823
  • 7
  • 45
  • 66
  • I would like to know if there are pixels in the coordinates x,y without the knowledge how the filled part looks like. Because i have much more complicated, dynamic shapes, so just looking which shape was touched wont work. – Skip Nov 11 '11 at 15:45
0

Since I was working with Paths - for me it was the solution to surround the green area by a path and to check, whether the given point x,y is contained within the path.

Since paths do not support the contains(x,y) method, I used a workaround described here

Community
  • 1
  • 1
Skip
  • 6,240
  • 11
  • 67
  • 117