简体   繁体   English

如何设置x和y协调Android

[英]how to set x and y coorinates android

i have created a 10x10 grid in the center of the screen in android, now i would like to give each square in the grid a coordinate. 我在android屏幕的中心创建了一个10x10的网格,现在我想给网格中的每个正方形一个坐标。 for example top left square in the grid would be 0 then 1, 2,3 and so on. 例如,网格中的左上角正方形将是0,然后是1,2,3,依此类推。 But i dont know how to do this. 但是我不知道该怎么做。 i am trying to do this in a draw class which extends view. 我正在尝试在扩展视图的绘图类中执行此操作。 my code of what i am trying is below 我正在尝试的代码如下

public int coordinates(int posX, int posY){

        int startX = (screenWidth / 2) - (rectSide / 2);
        int startY = (screenHeight / 2) - (rectSide / 2);

        //for(int i=0; i<=10000; i+=100){
        xCoord = (startX + (posX*100));
        yCoord = (startY + (posY*100));
    }

You know you start at point 0,0 top left . 您知道您从左上角的0,0点开始。 So assuming you have equally spaces squares you can just do the screen height / 10 to get how far apart each square should be in the y direction . 因此,假设您拥有相等的正方形空间,则只需将screen height / 10即可获得每个正方形在y方向上的间隔 And then do the same for the x direction . 然后对x方向执行相同操作。 Say your screen was 1000 pixels tall . 假设您的屏幕高1000像素

Then your grid at position (0,1) would be at (0,100) pixels. 然后,位于position (0,1)网格将为(0,100) pixels. (0,2) would be (0,200) you are just multiplying the y coordinate by the height of each square in the grid. (0,2)将等于(0,200)您只需将y坐标乘以网格中每个正方形的高度即可

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM