Class Dimension for java on android

前端 未结 3 1449
别那么骄傲
别那么骄傲 2020-12-10 16:16

What is the equivalent form of class java.awt.Dimension for android?

3条回答
  •  無奈伤痛
    2020-12-10 17:03

    You can choose one of these options:

    1. android.util.Size (since API 21). It has getWidth() and getHeight() but it's immutable, meaning once it's created you can't modify it.

    2. android.graphics.Rect. It has getWidth() and getHeight() but they're based on internal left, top, right, bottom and may seem bloated with all its extra variables and utility methods.

    3. android.graphics.Point which is a plain container, but the name is not right and it's main members are called x and y which isn't ideal for sizing. However, this seems to be the class to use/abuse when getting display width and height from the Android framework itself as seen here:

      Display display = getWindowManager().getDefaultDisplay();
      Point size = new Point();
      display.getSize(size);
      int width = size.x;
      int height = size.y;
      

提交回复
热议问题