问题
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:
- Draw a highlighted window over the selected parts of the image
- 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:
- should I access pixel level information of the image?
- which classes are needed to implement this functionality?
- 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