How can I find out which area of ​​the image (and not just the coordinates) has been affected in Android?

I am currently developing an Android application for my last project, which is to help autistic people indicate when something hurts and does this by touching a picture of the human body.

The problem I am facing is that I do not know how to identify the part of the image that they touch. An image is a bitmap that is stored in BitmapDrawable mode in ImageView. The first time it is touched, it is scaled using animation and setFilledAfter (true), and from there, when the area touches, the application should recognize the main body parts (for example, head, left hand ...).

The areas to be recognized are not square, so checking coordinates is not an option. I thought that each part would be recognized as a different view, drawn on top of the original picture, but I also open up other solutions.

To make things a little more interesting, the application should work on different devices and resolutions on both mobile phones and tablets.

Thank you in advance for your help. It is really necessary and appreciated.

EDIT

In the end, what I will try is the following. I will have two copies of the image, one for display and one for the inside, with the areas I want to recognize written in different colors. So the plan is to extrapolate the coordinates that I get from the Touch event, to find out which pixel it corresponds to in relation to the original, use Bitmap.getPixel () to determine the color of the waht, and then give the case statement returning A string of each piece.

I am coding this right now if someone is interested in leaving a message, and I will post how it works, and I ask it even with the appropriate code: D

Thanks to Shade for giving me several other options.

+6
android view bitmap touch
source share
2 answers

Without thinking too much about it, it seems that your idea of ​​finding strokes with overlay views seems to be good.

In addition, coordinates are also a good idea - you just need to identify named areas of the image and check if the touch point is in a specific area. It may be a little tiring, but it may be better in terms of complexity compared to 20 species. It will also definitely be faster than adding 20 extra views to your application.

But first of all, I think that you should experiment and see what is suitable for your specific situation, because everything else is guesswork.

EDIT:

If you decide to use the polygon method, you will have to deal with the problem of determining whether a point is inside the polygon ( see here for a brief explanation).

In addition, in terms of views, a view in Android is defined as “a rectangular area occupying the space on the screen”. Thus, the possibility of non-rectangular representations is excluded. It is possible that you are defining a non-rectangular area with clicks inside the view, but I do not know if this is possible.

+2
source share

@Alex, thanks for your idea, it helped me. That my implementation of your decision

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView imgView = (ImageView)this.findViewById(R.id.main_screen); imgView.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.red); switch(action) { case MotionEvent.ACTION_DOWN: int x = (int)event.getX(); int y = (int)event.getY(); int color = bMap.getPixel(x, y) ; ImageView testimage = (ImageView) findViewById(R.id.main_screen); switch(color) { case Color.RED: testimage.setImageDrawable(getResources().getDrawable(R.layout.red)); break; case Color.BLUE: testimage.setImageDrawable(getResources().getDrawable(R.layout.blue)); break; } break; case MotionEvent.ACTION_UP: int x1 = (int)event.getX(); int y1 = (int)event.getY(); color = bMap.getPixel(x1, y1) ; testimage = (ImageView) findViewById(R.id.main_screen); testimage.setImageResource(R.drawable.draw); // TODO: Rename constant upper-case. final int red = 1; final int blue = 2; switch(color){ case Color.RED: Intent i = new Intent(this, NewActivity.class); i.putExtra(NewActivity.EXT_COLOR, red); startActivity(i); break; case Color.BLUE: i = new Intent(this, NewActivity.class); i.putExtra(NewActivity.EXT_COLOR, blue); startActivity(i); break; } break; case MotionEvent.ACTION_MOVE: x = (int) event.getX(); y = (int)event.getY(); testimage = (ImageView) findViewById(R.id.main_screen); color = bMap.getPixel(x, y); switch(color){ case Color.RED: testimage.setImageDrawable(getResources().getDrawable(R.layout.red)); break; case Color.BLUE: testimage.setImageDrawable(getResources().getDrawable(R.layout.blue)); break; default: testimage = (ImageView) findViewById(R.id.main_screen); testimage.setImageResource(R.drawable.draw); break; } break; } return true; } 
0
source share

All Articles