Android Bounding Rectangles - android

I'm trying to make a bounding rectangle around an ImageView for collision detection. Unfortunately, I can only find code for sprites and not ImageViews. Is there a way to make a rectangle that goes outside of an image for an ImageView? One of the ImageViews moves through animation while the other one stays still.

Using imageViews for move animation isn't best idea. It takes too many resources. However you can set width and height of imageView to WRAP_CONTENT and use sizes of ImageView as bounding rect
In your xml layout set android:layout_height and android:layout_width of your ImageView to "wrap_content". And when you need bounding rect of ImageView:
...
int[] l = new int[2];
imageView.getLocationOnScreen(l);
int x = l[0];
int y = l[1];
int w = imageView.getWidth();
int h = imageView.getHeight();
...
x, y, w ,h — required bounding rect in screen coordinates

Related

How to get coordinates of a view in constraint layout?

I have an ImageView in the center of a constraint layout. I want to get its coordinates in the screen for animation. I have referred similar questions, but I always get 0 as the coordinate.
How can I get the coordinates?
What I have tried:
imageView.getTop();
imageView.getX();
imageView.getPivotX();
Rect rect = new Rect();
imageView.getGlobalVisibleRect(rect);
rect.centerX()
Note:
ImageView is placed at the center using constraints and it is of dimension 40dp x 40dp (small image).

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.

Android Screen Bounds in a Rect

Is there a way to get a RectF of the Android Device Screen Bounds? I'm trying to scale my Path to a RectF that is the exact screen size. I tried
int width = Resources.getSystem().getDisplayMetrics().widthPixels;
int height = Resources.getSystem().getDisplayMetrics().heightPixels;
screenBounds.set(0,0,width,height(;
Doesn't actually work. I've tried getDrawingRect() on my View that is match_parent and match_height but it did nothing.

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()

Is there a way to find the on-screen coordinates of an ImageView's drawable?

I have an activity which consists of just an ImageView. The drawable contained in the ImageView is a Bitmap that was captured from the camera. The image is automatically scaled (maintaining the Bitmap's aspect ratio) so that the scaled width matches the width of the ImageView, and the image is centered on the screen vertically.
I need to figure out the coordinates of the top-left pixel of the actual drawable (not of the ImageView itself), but the ImageView class doesn't seem to give me a way of doing that.
I feel like I could potentially calculate it based on the dimensions of the original bitmap and the dimensions of the ImageView, but that would involve a lot of math that should be unnecessary, and would be prone to floating point errors.
Does anyone know of a way to find the coordinates of the top-left pixel of the Drawable relative to the ImageView?
Thanks.
Looking at the ImageView source code, it translates a Matrix halfway the vertical size (i.e., center it vertically), so you could retrieve it with the ImageView getImageMatrix() method and check the vertical translation by Matrix#getValues(float[]). If I read it right, it will be the sixth value.
I can't test it right now, but it would be something like:
Matrix matrix = iv.getImageMatrix ();
float[] values = new float[];
matrix.getValues(values);
float startY = values[5];

Categories

Resources