FileDocCategorySizeDatePackage
Size.javaAPI DocAndroid 5.1 API4651Thu Mar 12 22:22:10 GMT 2015android.util

Size

public final class Size extends Object
Immutable class for describing width and height dimensions in pixels.

Fields Summary
private final int
mWidth
private final int
mHeight
Constructors Summary
public Size(int width, int height)
Create a new immutable Size instance.

param
width The width of the size, in pixels
param
height The height of the size, in pixels

        mWidth = width;
        mHeight = height;
    
Methods Summary
public booleanequals(java.lang.Object obj)
Check if this size is equal to another size.

Two sizes are equal if and only if both their widths and heights are equal.

A size object is never equal to any other type of object.

return
{@code true} if the objects were equal, {@code false} otherwise

        if (obj == null) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        if (obj instanceof Size) {
            Size other = (Size) obj;
            return mWidth == other.mWidth && mHeight == other.mHeight;
        }
        return false;
    
public intgetHeight()
Get the height of the size (in pixels).

return
height

        return mHeight;
    
public intgetWidth()
Get the width of the size (in pixels).

return
width

        return mWidth;
    
public inthashCode()
{@inheritDoc}

        // assuming most sizes are <2^16, doing a rotate will give us perfect hashing
        return mHeight ^ ((mWidth << (Integer.SIZE / 2)) | (mWidth >>> (Integer.SIZE / 2)));
    
private static java.lang.NumberFormatExceptioninvalidSize(java.lang.String s)

        throw new NumberFormatException("Invalid Size: \"" + s + "\"");
    
public static android.util.SizeparseSize(java.lang.String string)
Parses the specified string as a size value.

The ASCII characters {@code \}{@code u002a} ('*') and {@code \}{@code u0078} ('x') are recognized as separators between the width and height.

For any {@code Size s}: {@code Size.parseSize(s.toString()).equals(s)}. However, the method also handles sizes expressed in the following forms:

"width{@code x}height" or "width{@code *}height" {@code => new Size(width, height)}, where width and height are string integers potentially containing a sign, such as "-10", "+7" or "5".

{@code
Size.parseSize("3*+6").equals(new Size(3, 6)) == true
Size.parseSize("-3x-6").equals(new Size(-3, -6)) == true
Size.parseSize("4 by 3") => throws NumberFormatException
}

param
string the string representation of a size value.
return
the size value represented by {@code string}.
throws
NumberFormatException if {@code string} cannot be parsed as a size value.
throws
NullPointerException if {@code string} was {@code null}

        checkNotNull(string, "string must not be null");

        int sep_ix = string.indexOf('*");
        if (sep_ix < 0) {
            sep_ix = string.indexOf('x");
        }
        if (sep_ix < 0) {
            throw invalidSize(string);
        }
        try {
            return new Size(Integer.parseInt(string.substring(0, sep_ix)),
                    Integer.parseInt(string.substring(sep_ix + 1)));
        } catch (NumberFormatException e) {
            throw invalidSize(string);
        }
    
public java.lang.StringtoString()
Return the size represented as a string with the format {@code "WxH"}

return
string representation of the size

        return mWidth + "x" + mHeight;