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!
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
}
Related
Good day to you all, I am creating a ride app and I was asking for some assistance in the following problem I am facing.
So, On the image below the grey background represents a google map fragment and above it sits two views(black) I wanted to know if it were possible to only show a polyline and markers between the black views only in the red area and have the rest of the map showing in the background as well.
If it helps I have managed to get the LatLng's of the corners of the red box using:
googleMap.getProjection().fromScreenLocation();
Thank you in advance!
Yes, it is possible. Several ways. For example, the red area of your figure can be View (e.g. custom view) with transparent background and you can draw your polylines on it (you need to take into account position of transparent "red" view on map view for correction of fromScreenLocation()/toScreenLocation() methods results). Also, probably, you can use two MapViews one over another and synchronize its scrolling and zooming, but IMHO best way is to to implement custom view component based on MapVew (or MapFragment) an override onDraw() for MapVew-based (or dispatchDraw() for MapFragment-based) method like in this or that answers:
..
#Override
public void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
canvas.save();
drawPolylinesInRegion(canvas);
canvas.restore();
}
...
In drawPolylinesInRegion(canvas); method you should draw your polylines and markers "manually" converting coordinates of them via method opposite you mentioned: toScreenLocation() and taking into account size of "gray regions" of you example figure.
I was wondering how to achieve this. Is it like you instantiate a textView and let the user position it and set properties and then when the user hit the save button you take the property from the textView and make a paint object an add it to bitmap image? (if yes how do you get and set the position because the image that user sees is normally scaled down so it fit on the screen)
or is it like you make a new bitmap combining TextPaint and DynamicLayout like this (i dont know how to get width and height of my text because it will change based on text size, font, text length, etc...)
I'm a beginner and this would be my first app and also new to Java.
You would want to use canvas or Surface view. You would use bitmap to draw the image and then you canvas.paintText(...) to 'draw' the text with a Paint() data type. Use MotionEvent to follow the User's input. I'm not certain on how to save the canvas but I've heard of it being done fairly easily. You can use this site for learning the basics of canvas, paint, bitmap, Motion Events, and more. http://gamecodeschool.com/android/coding-a-breakout-game-for-android/
I can't provide specific examples with out code.
Hope this Helps!
I want to create a clickable image, my image has some different clickable parts in it, like this one:
I want to draw a custom shape like :
A,B,C,D,E,F
and make sure when user click on of this something happen.
the problem is I don't have any kind of idea to, how create shapes like the shapes in the image make sure it just fix on the image and in different screen size don't see a massed up thing.
Will there be more than many of such images?
If no I suggest you to create mask image for each region where black part of image represents the region and white part excludes rest.
To draw image:
create custom View
in constructor don't forget to use setWillNotDraw to true so you can do custom drawing
override View.onDraw method where you can draw main image and all others with some filters via setColorFilter.
To handle click events:
override onTouchEvent method
get touch position
compare touch position with point color in mask image
To optimise:
create mask image downscaled by some scale factor
during comparison divide touch position by scale factor
This is not ideal, but solution with vectors is non trivial I think
Take it as image and setOnclickListner for that image
I have created a canvas on android which has a circle drawn on and buttons however I want an image from the drawables to be dropped in via some sort of interaction so I thought of using a button, but I have created this screen only using the content view of the java class, thus I cannot use xml so is there any way you guys would know how I can grab the image on click in java (programatically).
Thanks.
you can draw anything on canvas,and add events to custom class that you have designed...
take a look http://android-er.blogspot.in/2010/05/draw-bitmap-on-view.html
The question is about java applet programming
I used a java applet to draw an image in the paint method using the following code:
g.draw(Myimage,0,0,this);
The image was drawn on the screen, But what i want to do is to be able to change the position of this image without clearing the screen and without drawing blank image in the previous position of the image..
Thanks in advance.
You can simply draw another image in other place. Just change the parameters in your code. For more details about graphics object follow the link. http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Graphics.html
Other wise why dont you simply extend GraphicsProgram class provided by acm. It has a move function which will exactly do what you want to do. Check out the link.
http://jtf.acm.org/rationale/graphics-package.html
Hope this helps.
But what i want to do is to be able to change the position of this image without clearing the screen and without drawing blank image in the previous position of the image..
Then use a JLabel. When you want to move the label you use the setLocation() method. The RepaintManager will repaint the location where the label was and then paint the label in its new location so you don't have to worry about calculating the area affected by the move.