I saw a lot of applications where when you click on different elements, be it a house or a tree, a certain action is performed. This is an invisible button in the form of this object? Or how is this generally implemented?
- Do you need the click to be processed only on the borders of the visible area, and not on the entire area of the widget? - pavlofff
- @pavlofff, yes, exactly - Oleksandr Zakrevskyi
3 answers
To solve your problem, you need to override the onTouch() method for the widget, which will contain some image that needs to be clicked. For example, for the ImageView widget:
ImageView imgView = (ImageView) findViewById(R.id.imageView); imgView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { final ImageView img = (ImageView) v; final Bitmap bitmap = ((BitmapDrawable) img.getDrawable()).getBitmap(); int x = (int) (event.getX()*bitmap.getWidth()/img.getWidth()); int y = (int) (event.getY()*bitmap.getHeight()/img.getHeight()); int color = bitmap.getPixel(x,y); Log.i("Color",": " + color); return (color == 0) ? true: false; } }); The method must return true , so that the click is ignored by the onClick() method, and false , so that it is registered with this method.
You can get the current click coordinates using the event.getX() and event.getY() methods, respectively, for the horizontal and vertical axes. These coordinates will correspond to the absolute coordinates on the device, starting from the upper left corner of the widget.
You can get the absolute size of the original image using the bitmap.getWidth() and bitmap.getHeight() methods.
Get the absolute size of the widget on the screen using the img.getWidth() and img.getHeight() methods
In this example, the algorithm for determining whether the image on the clicked area is based on the fact that we get a Bitmap with the assigned image from the widget, then we determine what color (the getPixel() method) is in the touch coordinates. If it is transparent (color = 0), then cancel the processing of the click, if any other, then skip.
Calculations of coordinates are made according to the formula, based on the fact that Bitmap (original image) may not correspond to the actual size of the widget (for example, it is stretched to the half-screen) and we reduce the touch coordinates on the widget to the coordinates on the real image.
This is just one of the examples of how you can define a touch in a certain area, you can implement any of your own algorithms that determine whether the touch fell into a specific area on the screen or not.
If there are several similar widgets, it would be more reasonable to make a custom View inherited from the same ImageView , for example, in which to override the onTouch() method.
- Thanks, exactly what I wanted! But how to check if the background is pink or green? in one word, if not transparent, but the exact shade is known? - Oleksandr Zakrevskyi
- oneThe @OleksandrZakrevskiy value returned by the
bitmap.getPixel()method is the color in the #ARGB view, that is, thecolorvariable contains acolorcode of two bytes in the hexadecimal representation: alpha channel, red, green, blue. The easiest way to get the desired value is to simply output it to the log and then substitute it into a condition. I updated the code by tapping my finger on the widget and you will receive in the log the color code of the place you taped to. - pavlofff
For any View, you can set the background and onClickListener . Be it ImageView or Button .
- Well it is clear. But then, the button will be rectangular, so if I click somewhere near the tree itself, for example, but still in this rectangular area, then the action will occur. How to avoid it? Is it possible to make a button in the form of something? - Oleksandr Zakrevskyi
- one@OleksandrZakrevskiy Did you find that pressing next to a house or a tree does not lead to the execution of an action? - Nick Volynkin ♦
- Convinced leads. The tree is not rectangular and the entire area of the button does not occupy. - Oleksandr Zakrevskyi
Watching what exactly is it about? For example, in TextView, images can be found directly in the text, etc. And the whole thing is that the html page is actually displayed, with links <a href> clicking on which leads to actions