I have a board for my game and I want to place numbers over each one of the black windows
What my board looks like ----------------------> What I want to achieve
I know which are the (x,y) coordinates to which the numbers will have to be placed, based on the Full HD image I'm using as a background; since the background is the same for all resolutions, I apply a scaling based on the device screen size. Nevertheless the numbers are all placed in the wrong position, with different distances between each other and not vertically aligned.
The following is the relevant code:
public class GraphicsActivity extends Activity implements SurfaceHolder.Callback {
private GraphicsSurfaceView view;
private static int[] player1positionX = {1556, 1325, 1101, 810, 587, 356};
private static int[] player1positionY = {886, 709, 886, 886, 709, 886};
private static int[] player2positionX = {356, 587, 810, 1101, 1325, 1556};
private static int[] player2positionY = {182, 361, 182, 182, 361, 182};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Display display = getWindowManager().getDefaultDisplay();
display.getRealSize(size);
float scaleX = size.x / 960f;
float scaleY = size.y / 540f;
view = new GraphicsSurfaceView(this, scaleX, scaleY);
LinearLayout bowlsPlayer1 = new LinearLayout(this);
LinearLayout bowlsPlayer2 = new LinearLayout(this);
gameBoard.addView(view);
gameBoard.addView(bowlsPlayer1);
gameBoard.addView(bowlsPlayer2);
setContentView(gameBoard);
/** Some code **/
for (int i = 0; i < Game.BOWL_NUMBER; i++) {
TextView bowl1 = new TextView(GraphicsActivity.this);
bowl1.setText(String.valueOf(Game.INITIAL_SEED));
bowl1.setTextColor(Color.WHITE);
bowl1.setTextSize(40);
bowl1.setX(player1positionX[i] / scaleX);
bowl1.setY(player1positionY[i] / scaleY);
bowlsPlayer1.addView(bowl1);
TextView bowl2 = new TextView(GraphicsActivity.this);
bowl2.setText(String.valueOf(Game.INITIAL_SEED));
bowl2.setTextColor(Color.WHITE);
bowl2.setTextSize(40);
bowl2.setX(player2positionX[i] / scaleX);
bowl2.setY(player2positionY[i] / scaleY);
bowlsPlayer2.addView(bowl2);
}
}
How can I correctly place the numbers on the screen?
EDIT 1:
After Der Golem
suggestion I switched from pixel to dp; this was useful to work better with the coordinates I had. I also switched from a LinealLayout
to a RelativeLayout
in which to insert the numbers and finally they are put all at the same distance.
How can I center the numbers placing the center of the TextView on the coordinates and not the upper left corner?