Class Dimension for java on android
Asked Answered
M

3

7

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

Mel answered 16/1, 2012 at 5:35 Comment(2)
Do you mean java swing Dimension class equivivalent? Single line question may not get helpful answer. Please provide as much information as you can.Obnubilate
yeah !! its the java swing Dimension class equivalentMel
Q
15

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;
    
Quiff answered 17/3, 2015 at 8:20 Comment(0)
G
1

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.

Gregor answered 1/9, 2012 at 20:52 Comment(1)
Pair<Integer, Integer> will have 2 extra "new" for Integer boxing that will eventually need to be garbage collected. E.g. if you are using Dimensions in layout code and have a lot of view this is unnecessary additional burden on gc(). Instead you can implement Dimensions holder with atomic types and have a View data member holding it for the lifetime of the view and use it in e.g. layout algorithms w/o any boxing/unboxing and garbage collecting. The same way basic Android views are using temporary Rect[F] storage to avoid allocations. Added benefit is JIT can inline final methods - faster codePetula
P
-1

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));
    }

}
Petula answered 11/6, 2014 at 21:41 Comment(1)
Because most programmers tend to forget to implement hashCode() and Parcelable and end up wasting precious time with such banalities when they could just have used Point provided by the fine Android API and moved on to more pressing matters ;)Quiff

© 2022 - 2024 — McMap. All rights reserved.