Get Location of an ImageView - android

I am currently working with onDraw(); and custom shapes.
What I am trying to do here is to draw 3 lines on the blue rectangle below:
And display a person's speed range settings and their current speed by drawing 3 lines. I am planning to do the speed range through getting the location, the width and the height of the rectangle and then dividing this up by the range set by the user.
However, I cannot find a resource that allows me to get the location, width and height of the blue rectangle.
Is there any way to achieve this, or do I simply have to get it from the source XML?

for getting the location of view related to parent view:
float x = view.getX();
float y = view.getY();
for getting the location of view related to Screen:
int[] location = new int[2];
view.getLocationOnScreen(location);
int x = location[0];
int y = location[1];

Related

how to get exact initial coordinates of bitmap image in imageview without using onTouch?

I have tried various methods but I want to get bitmap's coordinates when the activity loads so that I can use it to set polygon view.
I have tried using the imageview width and height but the polygon views occupy all of the screen I want the polygon view to be restricted to bit map for that I need the bitmap coordinates.
I want the (x,y) coordinates which are written in blue as depicted below.any help would be appreciated.the image
If I understood you Q correctly then you need a mathematical sol here to get x,y positions. 1st use bitmap to be set as ImageView.ScaleType= CENTER_INSIDE.
So, your image position is fixed & will touch either X or Y axis.
Calculate ratios:
Br(BitmapRatio) = Bw (bitmapWidth) / Bh (bitmapHeight)
Ir(ImageViewRatio) = Iw / Ih
Now, use below formula:
if(Ir > Br) {
y = 0;
x = Br*Ih/2;
} else {
x = 0;
y = (Iw/Br)/2;
}

Trouble getting the top and bottom of an imageView

I have a simple drawing app where I have a picture of a letter in the back filling an ImageView set to 45% of the screen's height. I have a JSON file that stores points along the letter. I'm trying to display those points on top of the picture of the letter.
Those points' coordinates range between y = -440 and y = 200. In order to properly display the points I need the top and the height of the imageView containing the letter to map the points onto the screen. I have to map the points to the proper points in runtime because with different screen sizes need different scale to display the points properly.
This is what it should look like (this is with a phone specific correction factor):
This is what it actually looks like:
I render the drawing via a Canvas that I paint the points onto. I'm pretty sure the problem lies in how I'm getting the top of the ImageView.
Here's what I've tried:
//1
float y = view.getTop();
//2
final int[] screenPos = new int[2];
iv.getLocationOnScreen(screenPos);
float y = screenPos[1];
//3
float offset = iv.getTop() - screenPos[1];
float y = iv.getTop() + offset;
Is there something I'm supposed to be doing that I'm not? Is there a better way the relative returns of getTop()? Help.
It will give you the position relative to the screen
Rect rect = new Rect();
view.getGlobalVisibleRect(rect);
float top = rect.top;
//there is also rect.bottom, rect.width, rect.height etc.

Draw Circle in ImageView when getting current location IndoorAtlas SDK

I want to get current location and draw blue dot like picture above. The blue dot is moving when the current location change.
Here is my code :
public void onServiceUpdate(ServiceState state) {
mSharedBuilder.setLength(0);
mSharedBuilder.append("Location: ")
.append(state.getRoundtrip()).append("ms")
.append(state.getGeoPoint().getLatitude())
.append(state.getGeoPoint().getLongitude())
.append(state.getMetricPoint().getX())
.append(state.getMetricPoint().getY())
.append(state.getImagePoint().getI())
.append(state.getImagePoint().getJ())
.append(state.getHeadingDegrees())
.append(state.getUncertainty());
double x1 = state.getMetricPoint().getX();
double y1 = state.getMetricPoint().getY();
float x = (float) x1;
float y = (float) y1;
Paint paint = null;
canvas.drawCircle(x,y,1,paint);
}
Could you help me ? The pictures already in resource folder.
First, in this case it is easier if you use coordinates from ServiceState#getImagePoint() - these (I & J) are coordinates of your location in the original floor plan. I.e. if your original mapped & uploaded floor plan had dimensions 800x600, service might return position I:400,J:300 indicating that you are now in the center of your mapped area.
You cannot however draw a circle like this: canvas.drawCircle(400, 300, radius, paint) - because depending on how your image is currently displayed (e.g. scaled) you might be drawing into incorrect location.
You need to take into account where your image is on the screen and what are it's current dimensions, i.e. scaling ratio. E.g. if you have a display of size 1920x1080 and your floor plan image of size 800x600 is drawn centered and without any scaling into an ImageView that occupies the entire screen real estate (assume full 1920x1080 in portrait here), then you would need to draw your current coordinate (400,300) into canvas at x: ((1080-800)/2)+400, y: ((1920-600/2)+300) = 540,960.
To draw accuracy with the blue dot (the semi-transparent larger circle), use uncertainty to calculate circle radius.

Get Y coordinate of top and bottom of ImageView

I have an image view which is contained within a relative layout. I am trying to get the screen y coordinates of the top of the image view and the screen y coordinates of the bottom of the image view. I have tried this:
float yMin = (float)levelH.getTop();
float yMax = (float)levelH.getBottom();
float yMin seems almost correct. I am translating another image view (IM2) up and down this image view(IM1). So I am trying to set a limit on how far (IM2) can translate up and down. So my thinking was to get the y top and bottom of (IM1) I can set those as max and min.
Anyone know how to do this?
ps Im using android accelometer to move (IM2)
getTop() ansd getBottom() look at coordinates within it's parent. To get coordinates of it's position on the screen you can use getLocationOnScreen
Use it like this:
int[] coords = {0,0};
view.getLocationOnScreen(coords);
int absoluteTop = coords[1];
int absoluteBottom = coords[1] + view.getHeight();
Use View.getLocationOnScreen() and/or getLocationInWindow()

Mapview on tablet: How can I center the map with an offset?

Hint: Here is a similar post with HTML.
In the current tablet implementation of my app, I have a fullscreen MapView with some informations displayed in a RelativeLayout on a left panel, like this:
(My layout is quite trivial, and I guess there is no need to post it for readability)
The problem comes when I want to center the map on a specific point... If I use this code:
mapController.setCenter(point);
I will of course get the point in the center of the screen and not in the center of the empty area.
I have really no idea where I could start to turn the offset of the left panel into map coordinates...
Thanks a lot for any help or suggestion
You can achive your objective by getting the map coordinates from top-left and bottom-right corners and divide it by the screen size, to get the value per pixel.
Then you just need to multiply by the offset and add it to the original center.
Example code:
private void centerMap(GeoPoint center, int offX, int offY){
GeoPoint tl = mapView.getProjection().fromPixels(0, 0);
GeoPoint br = mapView.getProjection().fromPixels(mapView.getWidth(), mapView.getHeight());
int newLon = offX * (br.getLongitudeE6() - tl.getLongitudeE6()) / mapView.getWidth() + center.getLongitudeE6();
int newLat = offY * (br.getLatitudeE6() - tl.getLatitudeE6()) / mapView.getHeight() + center.getLatitudeE6();
mapController.setCenter(new GeoPoint(newLat, newLon));
}
To use, you call the above method with your original center and both offsets (x and Y) to apply.
Note: as written, the code above move map left for positive offset values, and right for negative offset values. From the screen in your question you will need to use negative offset, to move map left, and a zero offset for Y.
Regards

Categories

Resources