How could I place a rectangle at the bottom of the screen?:
private void cargarJuego() {
r = new Rect(100,100,200,200);
escenario = new Escenario(r);
setContentView(new GameView(this,escenario));
}
How could I place a rectangle at the bottom of the screen?:
private void cargarJuego() {
r = new Rect(100,100,200,200);
escenario = new Escenario(r);
setContentView(new GameView(this,escenario));
}
Before commenting on how to do what you ask, it is important to review the drawRect() method of the Canvas class , which has several implementations, you can see that it can receive the left, top, right, and bottom points as well as the Paint object:
The points indicate the position in which the rectangle (or square) will be drawn, in the case of android the coordinates 0,0 start in the upper left position:
similar to the fourth quadrant of a Cartesian plane:
You can create a class that creates your rectangle, in this case it will start drawing it at coordinate 0,0 and as an example 400 pixels wide by 200 high:
and display it in your
Activity
:Resulting:
On the other hand, if we want it to be displayed at the bottom, we can increase the value of
top
ybottom
so that it is positioned vertically downwards.I have seen that what you want is to make a game, so I will give you an example of how to move the rectangle, by
drag and drop
using the previous class to create the rectangle:activity_main.xml
Using
OnTouchListener
, touching the view and dragging it will move it to the desired position, modifying its position.MainActivity.java