FileDocCategorySizeDatePackage
SizeF.javaAPI DocAndroid 5.1 API5481Thu Mar 12 22:22:10 GMT 2015android.util

SizeF

public final class SizeF extends Object
Immutable class for describing width and height dimensions in some arbitrary unit.

Width and height are finite values stored as a floating point representation.

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

Both the {@code width} and the {@code height} must be a finite number. In particular, {@code NaN} and positive/negative infinity are illegal values.

param
width The width of the size
param
height The height of the size
throws
IllegalArgumentException if either {@code width} or {@code height} was not finite.

        mWidth = checkArgumentFinite(width, "width");
        mHeight = checkArgumentFinite(height, "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 the same.

For this purpose, the width/height float values are considered to be the same if and only if the method {@link Float#floatToIntBits(float)} returns the identical {@code int} value when applied to each.

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

        if (obj == null) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        if (obj instanceof SizeF) {
            final SizeF other = (SizeF) obj;
            return mWidth == other.mWidth && mHeight == other.mHeight;
        }
        return false;
    
public floatgetHeight()
Get the height of the size (as an arbitrary unit).

return
height

        return mHeight;
    
public floatgetWidth()
Get the width of the size (as an arbitrary unit).

return
width

        return mWidth;
    
public inthashCode()
{@inheritDoc}

        return Float.floatToIntBits(mWidth) ^ Float.floatToIntBits(mHeight);
    
private static java.lang.NumberFormatExceptioninvalidSizeF(java.lang.String s)

        throw new NumberFormatException("Invalid SizeF: \"" + s + "\"");
    
public static android.util.SizeFparseSizeF(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 SizeF s}: {@code SizeF.parseSizeF(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 SizeF(width, height)}, where width and height are string floats potentially containing a sign, such as "-10.3", "+7" or "5.2", but not containing an {@code 'x'} (such as a float in hexadecimal string format).

{@code
SizeF.parseSizeF("3.2*+6").equals(new SizeF(3.2f, 6.0f)) == true
SizeF.parseSizeF("-3x-6").equals(new SizeF(-3.0f, -6.0f)) == true
SizeF.parseSizeF("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 invalidSizeF(string);
        }
        try {
            return new SizeF(Float.parseFloat(string.substring(0, sep_ix)),
                    Float.parseFloat(string.substring(sep_ix + 1)));
        } catch (NumberFormatException e) {
            throw invalidSizeF(string);
        } catch (IllegalArgumentException e) {
            throw invalidSizeF(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;