简体   繁体   English

如何将图像放在屏幕中央

[英]How can I put an Image at the center of a screen

I am trying to put an image (playBtn) in the center of a screen, With the code below my result is more towards the top right. 我试图将图像(playBtn)放在屏幕中央,下面的代码将结果放在右上角。 please see my code below 请在下面查看我的代码

 public class FlappyBird extends ApplicationAdapter {

        Texture img;
        public static final int WIDTH = 1440;
        public static final int HEIGHT = 2560;



    }

Above, I have setup my WIDTH & HEIGHT which I believe is not the right way to do so. 上面,我已经设置了宽度和高度,我认为这不是正确的方法。 Still, this gives me the required values but I can't seem to work with the playbth and place it in the center 仍然,这给了我所需的值,但是我似乎无法使用playbth并将其放置在中间

   public class MenuState extends State {
        private Texture background;
        private Texture playBtn;


        public MenuState(GameStateManager gsm) {
            super(gsm);
            background = new Texture("bg.png");
            playBtn = new Texture("playbtn.png");
        }



        @Override
        public void render(SpriteBatch sb) {

            sb.begin();
            sb.draw(background, 0, 0, FlappyBird.WIDTH, FlappyBird.HEIGHT);
            //sb.draw(playBtn, (FlappyBird.WIDTH / 2) - (playBtn.getWidth() / 2), FlappyBird.HEIGHT / 2);
            sb.draw(playBtn,(FlappyBird.WIDTH/2)-(playBtn.getWidth()),FlappyBird.HEIGHT / 2);
            sb.end();


        }
    }

Practically all objects are positioned from one of their left corners. 实际上,所有对象都从其左角之一定位。 So while the image itself appears to be unaligned, it is actually the top-left or bottom-left corner of the image that you have centered, depending on how your programming language works with the Y axis. 因此,虽然图像本身看起来未对齐,但实际上它是图像居中的左上角或左下角,这取决于您的编程语言如何与Y轴一起使用。

I threw a couple quick diagrams together to get a better visual representation. 我将几个快速图表放在一起以获得更好的视觉表示。 For this example, the Y axis increases downwards, so point (0,0) would be the top-left of the window instead of the bottom-left. 对于此示例,Y轴向下增加,因此点(0,0)将是窗口的左上角,而不是左下角。

Say you have a window that is 200px wide and 100px tall, and you have a 20px by 20px object that you want to place in the exact center of the window. 假设您有一个宽200像素,高100像素的窗口,并且有一个20像素乘20像素的对象要放置在窗口的正中央。 The general assumption is to place the object at half the width and height of the window, but doing so will get you a result similar to this: 一般的假设是将对象放置在窗口宽度和高度的一半处,但是这样做将获得类似于以下结果:

This is, however, a very simple issue to fix. 但是,这是一个非常简单的问题。 Simply position the object from half the width of the window, minus half the width of the object for the X axis, and half the height of the window minus half the height of the object for the Y axis: 只需将对象放置在窗口宽度的一半处,减去X轴对象宽度的一半,然后将窗口高度的一半减去Y轴对象高度的一半即可:

As mentioned before, some programming languages differ where the Y axis increases upwards and decreases downwards. 如前所述,某些编程语言在Y轴向上增加和向下减少的地方有所不同。 If this is the case for you, then you will have to take the bottom-left corners of your objects into account instead of their top-left corners, but I believe the equations should still be the same. 如果您是这种情况,那么您将不得不考虑对象的左下角而不是对象的左上角,但是我认为方程式应该仍然相同。

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

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