What does top, left, right and bottom mean in Android Rect object - android

I have an Android project where I should make Apples fall. The apples are painted in a Rect. So I created a function that change the Rect position and repaint. Here's my function :
private void updateApplesPosition() {
for(Rect rect:fallingDownFruitsList)
rect.set(rect.left, rect.top +10, rect.right, rect.bottom +10);
}
I have a problem : the Apples don't fall but go from right to left. To make the apples fall I changed the code by this :
private void updateApplesPosition() {
for(Rect rect:fallingDownFruitsList)
rect.set(rect.left+10, rect.top, rect.right+10, rect.bottom);
}

This image will explain you in detail:
left The X coordinate of the left side of the rectangle
top The Y coordinate of the top of the rectangle
right The X coordinate of the right side of the rectangle
bottom The Y coordinate of the bottom of the rectangle

From the docs
Parameters
left The X coordinate of the left side of the rectangle
top The Y coordinate of the top of the rectangle
right The X coordinate of the right side of the rectangle
bottom The Y coordinate of the bottom of the rectangle

Adding a crucial information.
The documentation says:
Note that the right and bottom coordinates are exclusive.
So if your rectangle is a single pixel at position 10,10
left = 10 : The X coordinate of the left side of the rectangle
top = 10 : The Y coordinate of the top of the rectangle = 10
right = 11 : The X coordinate of the right side of the rectangle plus one
bottom = 11 : The Y coordinate of the bottom of the rectangle plus one
Note that the right and bottom coordinates are exclusive.
The methods getWidth is declared as such
public final int width() { return right - left; }
Here it will return 11 - 10 = 1, as expected.
https://developer.android.com/reference/android/graphics/Rect

Related

Set min X, max X, min Y, max Y for a Canvas in Android Studio Kotlin

In Android Studio, I created a canvas. Width = 300dp, Height = 300dp.
I draw a line start from left top corner and end to right bottom corner. I traced the x and y coordinates. It is from (0, 0) in left top corner and to (1000+, 1000+) in right bottom corner.
But,
How to set (0,0) in left top corner and (300,300) in right bottom corner?

Placing bitmap at various points around a circle results in the bitmap being different distances away from center

I want to place a small triangular bitmap centered on the dotted circle as similar to as follows:
Here is my code that places the bitmap according to a specified angle:
x = (float) (center.x + radius* Math.cos(Math.toRadians(getRotationAngle())));
y = (float) (center.y + radius * Math.sin(Math.toRadians(getRotationAngle())));
canvas.drawBitmap(mCurrentTimeIndicatorBitmap, x, y, new Paint(Paint.FILTER_BITMAP_FLAG));
getRotationAngle() returns a value between 0 and 360 (inclusive). center.x, center.y are a constant 500 and radius is a constant 313.3. However, as I rotate around the circle, the bitmap's distance to the dotted line varies and I do not know why. The above picture is with rotation angle at 0, where it is farthest from the center.
Near rotation angle 90:
Near rotation angle 180, where it is the furthest from the dotted line & closest to the center:
Near rotation angle 270:
As you can see the triangle is various distances away from the dotted line. What is causing this? My code to draw the dotted lines is very similar but no issue occurs there.
When you draw bitmap, you are using left top corner as base point. So this corner walks around the circle exactly. But bitmap center moves along the circle that is shifted to right and bottom.
Just subtract half-size of bitmap to get corner coordinates at every step (or shift center coordinates)
x = (float) (center.x - mCurrentTimeIndicatorBitmap.width / 2 +
radius* Math.cos(Math.toRadians(getRotationAngle())));
y = (float) (center.y - mCurrentTimeIndicatorBitmap.height / 2 +
radius * Math.sin(Math.toRadians(getRotationAngle())));
canvas.drawBitmap(mCurrentTimeIndicatorBitmap, x, y, new Paint(Paint.FILTER_BITMAP_FLAG));

Bouncing back on the edges

I am trying to make a small game where you can fling a coin on the screen. while i was searching for that kind of animation i found this page
http://mobile.tutsplus.com/tutorials/android/android-gesture/
I managed to customise the events according to my aim, but i couldn't manage to set boundaries on the screen so that the coin(bitmap) will bounce back when it comes to the edges.
I tried several calculations but in the end the coin moves weirdly based on the contact points on the edges.
Can someone help me about it, according to the working code on the website
Below is a sample code from my pong game. Each object has position (x,y) and its speed. Movement is speed applied to position. One extra info is that the screen coordinate system starts from the left top corner. Y axis go down but increasing. Since screen coordinates start from 0, the boundaries become screen.width -1 and screen.height -1
#Override
public void onDraw(Canvas c){
p.setStyle(Style.FILL_AND_STROKE);
p.setColor(0xff00ff00);
// If ball's current x location plus the ball diameter is greater than screen width - 1, then make speed on x axis negative. Because you hit the right side and will bounce back to left.
if((Px+ballDiameter) > width - 1){
Vx = -Vx;
}
// If ball's current y location plus the ball diameter is greater than screen height -1, then make speed on y axis negative. Because you hit the bottom and you will go back up.
if((Py + ballDiameter) > height - 1){
Vy = -Vy;
}
// If current x location of the ball minus ball diameter is less than 1, then make the speed on x axis nagative. Because you hit to the left side of the screen and the ball would bounce back to the right side
if((Px - ballDiameter) < 1){
Vx = -Vx;
}
// If current y location of the ball minus ball diameter is less than 1, then make the speed on y axis negative. Because the ball hit the top of the screen and it should go down.
if((Py - ballDiameter ) <1){
Dy = 1;
Vy = -Vy;
}
Px += Vx; // increase x position by speed
Py += Vy; // increase y position by speed
c.drawCircle(Px, Py, ballDiameter, p);
invalidate();
}

Touch point coordinates with respect to Image after pinch zoom

How to calculate the coordinates with respect to image after zoom the image?
To zoom the image I followed the url: https://github.com/MikeOrtiz/TouchImageView/blob/master/src/com/example/touch/TouchImageView.java.
if we touch the image at particular point before Zoom then corresponding values are
point:(3,2)
top left corner of image:(0,0)
top left corner of screen:(0,0)
scale factors:(1.25,0.98)
After zoom the image:
if we drag the image until the image top left corner coincides the screen top left corner and touch image exactly at same touch point(before pinch) then
point:(540,220)
top left corner of image:(0,0)
top left corner of screen:(0,0)
scale factors:(4.78,2.67)
if we drag the image until the image top right corner coincides with the screen top right corner and touch image exactly at same touch point(before pinch) then
point:(1080,340)
top left corner of screen:(0,0)
top left corner of image:(-2430,0)
scale factors:(4.78,2.67)
if we drag the image until the image bottom left corner coincides with the screen bottom left corner and touch image exactly at same touch point(before pinch) then
point:(670,80)
top left corner of screen:(0,0)
top left corner of image:(0,-890)
scale factors:(4.78,2.67)
if we drag the image until the image bottom right corner coincides with the screen bottom right corner and touch image exactly at same touch point(before pinch) then
point:(456,274)
top left corner of screen:(0,0)
top left corner of image:(-2430,-890)
scale factors:(4.78,2.67)
if we set the image over the screen [ not to set the any corner]
point:(743,146)
top left corner of screen:(0,0)
top left corner of image:(-1280,-423)
scale factors:(4.78,2.67)
In all the above scenarios I am getting the coordinates in touch event as
x_cord=event.getX();
y_cord=event.getY();
The touch points I am getting are with respect to the screen.
How can I calculate the touch points according to the Image?
Thanks & Regards
mini.
We can calculate by calculating relative point according to the image size
float[] values = new float[9];
matrix.getValues(values);
x_coord = ((e.getX() - values[2])*scaleX )/values[0];
y_coord= ((e.gety() - values[5])*scaleY )/ values[4];
In my case I calculated those points as follows. This will be helpful for someone.
x = (event.getX() / values[Matrix.MSCALE_X] - (values[Matrix.MTRANS_X]/values[Matrix.MSCALE_X]));
y = (event.getY() / values[Matrix.MSCALE_Y] - (values[Matrix.MTRANS_Y]/values[Matrix.MSCALE_Y]));

Using MotionEvent

From my knowledge x and y starts at the top left of the screen as 0,0
My question is there a calculation to get a positioning in the event? or another way(eg. another new class or method within this class)?
Now the question is vague so I want to add an example:
I remember in math close you have 4 sections in a graph top left, top right, bottom left, and bottom right.
Using the graph style to get variables in:
Getting position from top left to bottom left.
Getting position from bottom right to top right
Getting position from top left to bottom right
Use event.getX() and event.getY() then you can calculate coordinates in any section of graph used in math class.
x, height - y
x, height - y
width - x, height - y

Categories

Resources