Android 上的 java 类 Dimension

发布于 2024-12-27 10:40:43 字数 58 浏览 2 评论 0原文

Android 类 java.awt.Dimension 的等效形式是什么?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倚栏听风 2025-01-03 10:40:43

您可以选择以下选项之一:

  1. android.util.Size(自 API 21 起)。它有 getWidth()getHeight(),但请注意它是不可变的,这意味着一旦创建就无法修改它(只能创建新的、更新的实例)。

  2. android.graphics.Rect。它有 getWidth()getHeight() 但它们基于内部 lefttoprightbottom 并且可能看起来因所有额外的变量和实用方法而显得臃肿。

  3. android.graphics.Point 这是一个普通的容器,但是名字不对,它的主要成员叫做xy这对于调整尺寸来说并不理想。 但是,这似乎是从 Android 框架本身获取显示宽度和高度时要使用/滥用的类,如下所示:

    显示 display = getWindowManager().getDefaultDisplay();
    点大小 = new Point();
    显示.getSize(大小);
    int 宽度 = size.x;
    int 高度 = size.y;
    

You can choose one of these options:

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

  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;
    
七婞 2025-01-03 10:40:43

您可以使用 Pair这是 Android 的通用元组类。 (不过,您需要将 getWidth()getHeight() 替换为 firstsecond。) Android 团队似乎在 Android API 的其他地方使用临时类来实现此目的,例如 相机大小

You could use Pair<Integer, Integer> which is Android's generic tuple class. (You would need to replace getWidth() and getHeight() with first and second, though.) In other places of the Android API the Android team seems to use ad-hoc classes for this purpose, for instance Camera.Size.

老娘不死你永远是小三 2025-01-03 10:40:43

为什么你需要滥用其他类而不是实现一些非常简单的东西,例如:

public class Dimensions {

    public int width;
    public int height;

    public Dimensions() {}

    public Dimensions(int w, int h) {
        width = w;
        height = h;
    }

    public Dimensions(Dimensions p) {
        this.width = p.width;
        this.height = p.height;
    }

    public final void set(int w, int h) {
        width = w;
        height = h;
    }

    public final void set(Dimensions d) {
        this.width = d.width;
        this.height = d.height;
    }

    public final boolean equals(int w, int h) {
        return this.width == w && this.height == h;
    }

    public final boolean equals(Object o) {
        return o instanceof Dimensions && (o == this || equals(((Dimensions)o).width, ((Dimensions)o).height));
    }

}

Why do you need to abuse other classes instead of implementing something extremely simple like:

public class Dimensions {

    public int width;
    public int height;

    public Dimensions() {}

    public Dimensions(int w, int h) {
        width = w;
        height = h;
    }

    public Dimensions(Dimensions p) {
        this.width = p.width;
        this.height = p.height;
    }

    public final void set(int w, int h) {
        width = w;
        height = h;
    }

    public final void set(Dimensions d) {
        this.width = d.width;
        this.height = d.height;
    }

    public final boolean equals(int w, int h) {
        return this.width == w && this.height == h;
    }

    public final boolean equals(Object o) {
        return o instanceof Dimensions && (o == this || equals(((Dimensions)o).width, ((Dimensions)o).height));
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文