【问题标题】:How to center an image in a rectangle?如何使图像在矩形中居中?
【发布时间】:2013-05-04 13:41:02
【问题描述】:

如何在矩形内居中图像:

Batch.draw(imagename, new Rectangle(x, y, imagename.width, imagename.height), Color.White);

【问题讨论】:

  • 我知道我已经回答了这个问题,但请为您的问题提供更多背景信息。

标签: c# image xna center


【解决方案1】:

您可以使用 spritebatch 的源重载

Batch.draw(imagename, new Rectangle(x, y, imagename.width, imagename.height), Color.White,0f,new Vector2(imagename.Width /2, imagename.Height /2) ,SpriteEffects.Null, 0);

【讨论】:

    【解决方案2】:

    SpriteBatch.Draw() 的一个重载要求Texture2D(图像)、Vector2(图像的位置)和Color(图像的色调)。您不必使用矩形来绘制图像。只需使用一些数学运算即可获得该位置的正确 XY 值。

    您必须考虑图像的高度/宽度,以及包含对象(如屏幕或其他东西)的高度/宽度。

    另一种选择是使用带有Origin 参数的重载。将此设置为图像的相对中心。这会将图像的“绘制原点”从左上角更改为中心。

    【讨论】:

      猜你喜欢
      • 2023-01-01
      • 2011-11-20
      • 2019-05-25
      • 2018-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多