0

I have a board for my game and I want to place numbers over each one of the black windows Before After

      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. Current result

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. Actual result

How can I center the numbers placing the center of the TextView on the coordinates and not the upper left corner?

Community
  • 1
  • 1
Mangusto
  • 1,505
  • 1
  • 13
  • 29
  • Where are you getting 960 and 540 from? If those are your width and height, then you have quite a view player position values that lie outside of that range. – zgc7009 Jan 06 '15 at 18:04
  • Because of some animations on the background, each frame is resized to 960x540 – Mangusto Jan 06 '15 at 18:09
  • You are reasoning in **px** (resolution based). But Android bases its scaling on **dp** (density based). So, you first have to get the scale factor for the current screen density and then calculate the coordinates in **px**. Also mind that you should provide your graphics in different densities... Some graphics may be optimized by using 9 patches and/or Android drawables. – Phantômaxx Jan 06 '15 at 18:15

0 Answers0