Gravitypublic class Gravity extends Object Standard constants and tools for placing an object within a potentially
larger container. |
Fields Summary |
---|
public static final int | NO_GRAVITYConstant indicating that no gravity has been set | public static final int | AXIS_SPECIFIEDRaw bit indicating the gravity for an axis has been specified. | public static final int | AXIS_PULL_BEFORERaw bit controlling how the left/top edge is placed. | public static final int | AXIS_PULL_AFTERRaw bit controlling how the right/bottom edge is placed. | public static final int | AXIS_CLIPRaw bit controlling whether the right/bottom edge is clipped to its
container, based on the gravity direction being applied. | public static final int | AXIS_X_SHIFTBits defining the horizontal axis. | public static final int | AXIS_Y_SHIFTBits defining the vertical axis. | public static final int | TOPPush object to the top of its container, not changing its size. | public static final int | BOTTOMPush object to the bottom of its container, not changing its size. | public static final int | LEFTPush object to the left of its container, not changing its size. | public static final int | RIGHTPush object to the right of its container, not changing its size. | public static final int | CENTER_VERTICALPlace object in the vertical center of its container, not changing its
size. | public static final int | FILL_VERTICALGrow the vertical size of the object if needed so it completely fills
its container. | public static final int | CENTER_HORIZONTALPlace object in the horizontal center of its container, not changing its
size. | public static final int | FILL_HORIZONTALGrow the horizontal size of the object if needed so it completely fills
its container. | public static final int | CENTERPlace the object in the center of its container in both the vertical
and horizontal axis, not changing its size. | public static final int | FILLGrow the horizontal and vertical size of the object if needed so it
completely fills its container. | public static final int | CLIP_VERTICALFlag to clip the edges of the object to its container along the
vertical axis. | public static final int | CLIP_HORIZONTALFlag to clip the edges of the object to its container along the
horizontal axis. | public static final int | RELATIVE_LAYOUT_DIRECTIONRaw bit controlling whether the layout direction is relative or not (START/END instead of
absolute LEFT/RIGHT). | public static final int | HORIZONTAL_GRAVITY_MASKBinary mask to get the absolute horizontal gravity of a gravity. | public static final int | VERTICAL_GRAVITY_MASKBinary mask to get the vertical gravity of a gravity. | public static final int | DISPLAY_CLIP_VERTICALSpecial constant to enable clipping to an overall display along the
vertical dimension. This is not applied by default by
{@link #apply(int, int, int, Rect, int, int, Rect)}; you must do so
yourself by calling {@link #applyDisplay}. | public static final int | DISPLAY_CLIP_HORIZONTALSpecial constant to enable clipping to an overall display along the
horizontal dimension. This is not applied by default by
{@link #apply(int, int, int, Rect, int, int, Rect)}; you must do so
yourself by calling {@link #applyDisplay}. | public static final int | STARTPush object to x-axis position at the start of its container, not changing its size. | public static final int | ENDPush object to x-axis position at the end of its container, not changing its size. | public static final int | RELATIVE_HORIZONTAL_GRAVITY_MASKBinary mask for the horizontal gravity and script specific direction bit. |
Methods Summary |
---|
public static void | apply(int gravity, int w, int h, android.graphics.Rect container, android.graphics.Rect outRect)Apply a gravity constant to an object. This supposes that the layout direction is LTR.
apply(gravity, w, h, container, 0, 0, outRect);
| public static void | apply(int gravity, int w, int h, android.graphics.Rect container, android.graphics.Rect outRect, int layoutDirection)Apply a gravity constant to an object and take care if layout direction is RTL or not.
int absGravity = getAbsoluteGravity(gravity, layoutDirection);
apply(absGravity, w, h, container, 0, 0, outRect);
| public static void | apply(int gravity, int w, int h, android.graphics.Rect container, int xAdj, int yAdj, android.graphics.Rect outRect)Apply a gravity constant to an object.
switch (gravity&((AXIS_PULL_BEFORE|AXIS_PULL_AFTER)<<AXIS_X_SHIFT)) {
case 0:
outRect.left = container.left
+ ((container.right - container.left - w)/2) + xAdj;
outRect.right = outRect.left + w;
if ((gravity&(AXIS_CLIP<<AXIS_X_SHIFT))
== (AXIS_CLIP<<AXIS_X_SHIFT)) {
if (outRect.left < container.left) {
outRect.left = container.left;
}
if (outRect.right > container.right) {
outRect.right = container.right;
}
}
break;
case AXIS_PULL_BEFORE<<AXIS_X_SHIFT:
outRect.left = container.left + xAdj;
outRect.right = outRect.left + w;
if ((gravity&(AXIS_CLIP<<AXIS_X_SHIFT))
== (AXIS_CLIP<<AXIS_X_SHIFT)) {
if (outRect.right > container.right) {
outRect.right = container.right;
}
}
break;
case AXIS_PULL_AFTER<<AXIS_X_SHIFT:
outRect.right = container.right - xAdj;
outRect.left = outRect.right - w;
if ((gravity&(AXIS_CLIP<<AXIS_X_SHIFT))
== (AXIS_CLIP<<AXIS_X_SHIFT)) {
if (outRect.left < container.left) {
outRect.left = container.left;
}
}
break;
default:
outRect.left = container.left + xAdj;
outRect.right = container.right + xAdj;
break;
}
switch (gravity&((AXIS_PULL_BEFORE|AXIS_PULL_AFTER)<<AXIS_Y_SHIFT)) {
case 0:
outRect.top = container.top
+ ((container.bottom - container.top - h)/2) + yAdj;
outRect.bottom = outRect.top + h;
if ((gravity&(AXIS_CLIP<<AXIS_Y_SHIFT))
== (AXIS_CLIP<<AXIS_Y_SHIFT)) {
if (outRect.top < container.top) {
outRect.top = container.top;
}
if (outRect.bottom > container.bottom) {
outRect.bottom = container.bottom;
}
}
break;
case AXIS_PULL_BEFORE<<AXIS_Y_SHIFT:
outRect.top = container.top + yAdj;
outRect.bottom = outRect.top + h;
if ((gravity&(AXIS_CLIP<<AXIS_Y_SHIFT))
== (AXIS_CLIP<<AXIS_Y_SHIFT)) {
if (outRect.bottom > container.bottom) {
outRect.bottom = container.bottom;
}
}
break;
case AXIS_PULL_AFTER<<AXIS_Y_SHIFT:
outRect.bottom = container.bottom - yAdj;
outRect.top = outRect.bottom - h;
if ((gravity&(AXIS_CLIP<<AXIS_Y_SHIFT))
== (AXIS_CLIP<<AXIS_Y_SHIFT)) {
if (outRect.top < container.top) {
outRect.top = container.top;
}
}
break;
default:
outRect.top = container.top + yAdj;
outRect.bottom = container.bottom + yAdj;
break;
}
| public static void | apply(int gravity, int w, int h, android.graphics.Rect container, int xAdj, int yAdj, android.graphics.Rect outRect, int layoutDirection)Apply a gravity constant to an object.
int absGravity = getAbsoluteGravity(gravity, layoutDirection);
apply(absGravity, w, h, container, xAdj, yAdj, outRect);
| public static void | applyDisplay(int gravity, android.graphics.Rect display, android.graphics.Rect inoutObj)Apply additional gravity behavior based on the overall "display" that an
object exists in. This can be used after
{@link #apply(int, int, int, Rect, int, int, Rect)} to place the object
within a visible display. By default this moves or clips the object
to be visible in the display; the gravity flags
{@link #DISPLAY_CLIP_HORIZONTAL} and {@link #DISPLAY_CLIP_VERTICAL}
can be used to change this behavior.
if ((gravity&DISPLAY_CLIP_VERTICAL) != 0) {
if (inoutObj.top < display.top) inoutObj.top = display.top;
if (inoutObj.bottom > display.bottom) inoutObj.bottom = display.bottom;
} else {
int off = 0;
if (inoutObj.top < display.top) off = display.top-inoutObj.top;
else if (inoutObj.bottom > display.bottom) off = display.bottom-inoutObj.bottom;
if (off != 0) {
if (inoutObj.height() > (display.bottom-display.top)) {
inoutObj.top = display.top;
inoutObj.bottom = display.bottom;
} else {
inoutObj.top += off;
inoutObj.bottom += off;
}
}
}
if ((gravity&DISPLAY_CLIP_HORIZONTAL) != 0) {
if (inoutObj.left < display.left) inoutObj.left = display.left;
if (inoutObj.right > display.right) inoutObj.right = display.right;
} else {
int off = 0;
if (inoutObj.left < display.left) off = display.left-inoutObj.left;
else if (inoutObj.right > display.right) off = display.right-inoutObj.right;
if (off != 0) {
if (inoutObj.width() > (display.right-display.left)) {
inoutObj.left = display.left;
inoutObj.right = display.right;
} else {
inoutObj.left += off;
inoutObj.right += off;
}
}
}
| public static void | applyDisplay(int gravity, android.graphics.Rect display, android.graphics.Rect inoutObj, int layoutDirection)Apply additional gravity behavior based on the overall "display" that an
object exists in. This can be used after
{@link #apply(int, int, int, Rect, int, int, Rect)} to place the object
within a visible display. By default this moves or clips the object
to be visible in the display; the gravity flags
{@link #DISPLAY_CLIP_HORIZONTAL} and {@link #DISPLAY_CLIP_VERTICAL}
can be used to change this behavior.
int absGravity = getAbsoluteGravity(gravity, layoutDirection);
applyDisplay(absGravity, display, inoutObj);
| public static int | getAbsoluteGravity(int gravity, int layoutDirection)Convert script specific gravity to absolute horizontal value.
if horizontal direction is LTR, then START will set LEFT and END will set RIGHT.
if horizontal direction is RTL, then START will set RIGHT and END will set LEFT.
int result = gravity;
// If layout is script specific and gravity is horizontal relative (START or END)
if ((result & RELATIVE_LAYOUT_DIRECTION) > 0) {
if ((result & Gravity.START) == Gravity.START) {
// Remove the START bit
result &= ~START;
if (layoutDirection == View.LAYOUT_DIRECTION_RTL) {
// Set the RIGHT bit
result |= RIGHT;
} else {
// Set the LEFT bit
result |= LEFT;
}
} else if ((result & Gravity.END) == Gravity.END) {
// Remove the END bit
result &= ~END;
if (layoutDirection == View.LAYOUT_DIRECTION_RTL) {
// Set the LEFT bit
result |= LEFT;
} else {
// Set the RIGHT bit
result |= RIGHT;
}
}
// Don't need the script specific bit any more, so remove it as we are converting to
// absolute values (LEFT or RIGHT)
result &= ~RELATIVE_LAYOUT_DIRECTION;
}
return result;
| public static boolean | isHorizontal(int gravity)Indicate whether the supplied gravity has an horizontal pull.
return gravity > 0 && (gravity & RELATIVE_HORIZONTAL_GRAVITY_MASK) != 0;
| public static boolean | isVertical(int gravity)Indicate whether the supplied gravity has a vertical pull.
return gravity > 0 && (gravity & VERTICAL_GRAVITY_MASK) != 0;
|
|