Absolute Coordinates for my Graphic Component - java

I am doing a project for a contest and in android for a tablet. By wanting to make the user interface more friendly I am using some meters to display speed, torque, temperature and current in a nice way.
So...I inserted the imageview's coresponding to the meters and now it's time to set that pins ( the place from where the indicator rises (it's a line on a circle) ).
I have put my meters images in relative layouts and set parameters for everyone relative to screen resolution.
My problem now is that I want to center a image of that pin inside the meter.
I tryed almost everything I can find to get the ablsolute coordinate x and y but no succes. I need something that can give me the exact location of the meter meening X and Y coordinates. To be more specific and I really hope I make myself undearstood I give the next example:
I have a screen...lets say 600x600 and I pinned a imageview in the center.
Now...I want to get the real coordinates for the imageview on the surface..so I want to get X = 300 - imageWidth/2, Y = 300 - ImageHeight/2 or something like that. So...When I put in a random position from xml layout using just the margin left and margin top parameters I didnt used hardcoded coordinates but now I need to get fixed coordinates beacouse I want to center anoter image inside the parent image.
Or another example: basicly I want to center a image inside a image and I think to do that I need fixed coordinates of the parent's image.
Thank you for your time, any critcs are much apreciated.

I just found a solution. It's not really my favorite but if I put my image inside a RelativeLayout and the other image centered in this RelativeLayout..it works like a charm.2 . Anyone has a better ideea?

Related

Java - Screen Coordinates to Image Coordinates

I am using a Java application to display an image on the screen. I also am using an eye-tracker device which records the absolute pixel X,Y locations where the person is looking on the screen.
However, what I need to do is convert these X,Y coordinates from the screen positions into the X,Y locations of the image. In other words, somehow I need to figure out that (just an example) 482, 458 translates to pixel 1,1 (the upper left pixel) of the image.
How can I determine the image's placement on the screen (not relative to anything)?
I saw a few posts about "getComponentLocation" and some other APIs, but in my experimentation with these, they seem to be giving coordinates relative to the window. I have also had problems with that because the 1,1 coordinate that they give is within the window, and there is actually a bar at the top of the window (that has the title and the close and minimize buttons) whose width I do not know, so I cannot easily translate.
Surely there must be a way to get the absolute pixel location on the screen of a component?
If we are talking about Swing/AWT application than class java.awt.Component has method getLocationOnScreen which seemed to do what you want
And yes as #RealSkeptic mentioned in comments to question:
SwingUtilities.html#convertPointFromScreen
will do all this work for you considering components hierarchy

Why ImageView's getX, getY, setX and setY do not represent the actual value in the Relative Layout?

I am trying to make a simple app just to understand better how to use Touch events on Android. Right now, my Activity just moves an ImageView to the coordinates of the touch on the screen (the MotionEvent's coordinates). I could manage to do this by applying a simple trick to set the new position of the image to a value that is made of the touch coordinates relative to the position of the image (getX() and getY()). The dx and dy variables are responsible to store this relative values so the touch keeps constant within the image.
Now I want to put 2 ImageViews in the Activity and have only two fixed spots for ImageViews and once the user drags one ImageView over the other ImageView's center, the two images switch places, but to do that correctly I would like to get the real coordinates of the image center. I commented the parts of the code that make it work correctly to show the issue. As you can see in the first picture when the view is created, the Image Coordinates are simply (0, 0), no matter where it is (as long as the initial position is defined as a layout parameter like CENTER_IN_PARENT).
As you can see in the second picture and as far as I observed ImageView's getX() and getY() return the coordinate as if the top-left corner of the image was in the top-left corner of the DISPLAY when the image is located at the top-left corner of the ACTIVITY WINDOW.
I would like to understand why that happens and if anyone knows how to actually get the coordinates of ImageView that can be directly compared to the coordinates of MotionEvent.
Pictures:
FIRST PICTURE
SECOND PICTURE
I still don't know exactly why getX(), getY(), setX(), and setY() do not represent the position of the View on the screen, but I figured out how to get the actual location of the view objects on the screen: getLocationOnScreen(coordinates). Where coordinates is a 2 position int array (int[2]).
For example, to get the real value correspondent to the top-left corner of my ImageView image I simply need to do something like:
int[] img_coordinates = new int[2];
ImageView image = (ImageView) findViewById(R.id.myImage);
image.getLocationOnScreen(img_coordinates);
And then I can actually compare these coordinates with the Touch-event's getRawX and getRawY.
If I want to get the center point of my view I just need to do this:
x_center = (double)img_coordinates[0] + image.getWidth()/2.0;
y_center = (double)img_coordinates[1] + image.getHeight()/2.0;
I hope this helps other people too, it took me a while to find this approach and I am actually not sure if it is the best approach but it does the trick for me.

Putting an angle on image view

I'd like to set an angle on an image view and have that angle be generated randomly, and set the ancho point of the image view to the center of the android screen. After that I'd like the computer to generate a spot from a certain distance of the middle to the end of the screen on that angle and set a button to appear there. I'm not sure if eclipse has a quick automatic way to do that.
Thanks.
I've added a picture to help. I'd like the arrow to point to a random angle and then a button to appear on that angle but outside of the circle (the circle is imaginary just showing that it needs to appear outside of a certain distance from the center.
You can use setRotation method for rotate a view in android.
image.setRotation(90); // instead of 90 you can give your generated value.
// But make sure the value should be float.
image.setRotationX(90); // if you want rotate depends x axis
image.setRotationY(90); // if you want rotate depends y axis
also take a look on here
I hope this will help you.

Updating Image co-ordinate in android after panning

I am trying develop something from my research work using android. But I am not a coding person, so I am having hard time figuring out how to do things. I figured out ways to acheive my functionality but I am kinda struck with a issue which I could not resolve on my own. It would be great if you guys could help me with it.
I am trying to display a image that is bigger than the screen size and make it to play a sound or vibrate when I touch a particular colored pixel within the image. I was able to perform this for the first instance of the image(i.e., the image displayed once I start my application), but as soon as I pan it doesn't work. For example, my image has a green color pixel in the middle of the screen and after I pan it moved to the left. I am making it to vibrate once I touch the green pixel. The device vibrates when the green is i center, but after I pan it is not getting updated. It still vibrates when I touch the center of the screen even tough there is a different color. I am guessing that the program fixes the screen co-ordinates and are not using the image co-ordinates. I tried using event.getX, getRawX. But both are referencing to screen co-ordinate only.
My question is
*is there a way to target the image co-ordinate rather than screen co-ordinate?
*If not, how else can I accomplish this task?
Well, it's kind of semantic, but there is no concept of "image co-ordinates".
If you think about, the touch is handled in the first instance by a piece of hardware which has absolutely no knowledge of what your are touching except its' physical pixels and this is what it reports to Android.
In turn, Android has no knowledge of what that chunk of image pixels is. The position of a particular image in your pixel relative to the screen only has meaning inside your app. Since the touch event originates outside your app, there is no way to associate the two....
....unless you make the association. So, you moved the image in your code in response to a touch event. Remember how far you moved it, using a variable defined in the class handling the touch event, and then use that as an offset to the x and y given to you in subsequent touch events.
E.g. You pan the image 200 pixels left. A dead centre touch now corresponds to the centre pixel of your image (x/2) + 200 since the physical pixel touched is now 200 to the right of the image centre.
[EDIT] Having thought a little more about this, you might be using a matrix to pan the image, and if you aren't, then do check out the Matrix class. If you are, then you can query the matrix at any time to get the total amount of pan in x and y at any time. If you are also doing scaling and/or rotation, things get a bit more complex but that would merit a new question.
[EDIT]
To get the colour at your x,y:
int pixel = bitmap.getPixel(x,y);
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);

How to change the position of a view?

I have two views called x and y they are both black lines (for example I made the height of the x line is 1dp and width 230dp and as background filled with the color black).
Now i want to move the position of the lines programmatically (for example I want the y line 50dp to the right of the orginal position).
Can someone help me how to do this?
I have tried things such as setpadding but the line doesn't move.
Thanks in advance!
(ps: my minimum sdk is set for 7 so i can't use the newest api's).
Old Answer
Have a look at the Absolute Layout, it allows you to position
child elements using x, y coordinates. It is deprecated but it's the
only way in Android to do real x,y coordinate positioning.
I would ask what the main point behind what you are trying to do is
though? It sounds like you started with a goal, were led down a path
and now are asking how to get to the end of that path, rather than
asking how to do what you need to do.
Edited
For drawing graphs have a look instead at https://stackoverflow.com/questions/2271248/how-to-draw-charts-in-android.
Using a Layout class to draw a chart will only lead to a really slow app, since the layout classes are designed for creating relatively static layouts, not drawing full graphics.
Instead use either the Canvas and draw your drawables yourself or use the graphing packages listed in the SO question I linked above.

Categories

Resources