Select Content inside ImageView - Android

柔情痞子 提交于 2019-12-11 15:51:47

问题


I have an Activity containing an ImageView and I'd like to allow the User to select part of it's content with the touch (or mouse click) capabilities.

I'd like to write a procedure able to achieve two things:

  1. Draw a highlighted window over the selected parts of the image
  2. Return an object containing the coordinates of the selected (highlighted) pixels.

For better understanding you can check the little mock up I've created:

The User should touch the screen over some part of the image and it should get highlighted. When pressing the back button I'd like to obtain via Java the coordinates of the pixels that were highlighted.

Can you help me understand how to do?

In particular I'd like to find out the following:

  1. should I access pixel level information of the image?
  2. which classes are needed to implement this functionality?
  3. some idea of pseudo code?

Thks for any kind of help!


回答1:


I would subclass ImageView then you can capture the touch events by overriding onTouchEvent(...)

When you get to the onDraw(...) method you can call super to draw the image as normal, then add your own code to draw a highlight over the top.

EDIT

Well instead of using ImageView you can extend it and write your own class, all this class has to do is override onTouchEvent(...) so you know when the view is being touched and can save the location on screen of the touch events. Next you edit the drawing methods:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas); // So the image you want is drawn as normal

    myMethodForDrawingAFancyHighlight(Canvas canvas); // add your special effects on top of the image
}


来源:https://stackoverflow.com/questions/11311337/select-content-inside-imageview-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!