android.view.Gravity#getAbsoluteGravity ( )源码实例Demo

下面列出了android.view.Gravity#getAbsoluteGravity ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

private void attachNavigationView() {

        CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) getLayoutParams();
        int gravity = Gravity.LEFT;
        if (layoutParams.getAnchorId() != View.NO_ID && layoutParams.anchorGravity != Gravity.NO_GRAVITY) {
            if (Gravity.isHorizontal(layoutParams.anchorGravity)) {
                gravity = layoutParams.anchorGravity;
            }
        } else if (layoutParams.gravity != Gravity.NO_GRAVITY) {
            if (Gravity.isHorizontal(layoutParams.gravity)) {
                gravity = layoutParams.gravity;
            }
        }

        // Gravity.START and Gravity.END don't work for views added in WindowManager with RTL.
        // We need to 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.
        gravity = Gravity.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(this));

        mWindowManager.addView(mNavigationView, createLayoutParams(gravity));
    }
 
源代码2 项目: Dashchan   文件: DrawerLayout.java
/**
 * Returns the title of the drawer with the given gravity.
 *
 * @param edgeGravity Gravity.LEFT, RIGHT, START or END. Expresses which
 *            drawer to return the title for.
 * @return The title of the drawer, or null if none set.
 * @see #setDrawerTitle(int, CharSequence)
 */
public CharSequence getDrawerTitle(int edgeGravity) {
	final int absGravity = Gravity.getAbsoluteGravity(edgeGravity, getLayoutDirection(this));
	if (absGravity == Gravity.LEFT) {
		return mTitleLeft;
	} else if (absGravity == Gravity.RIGHT) {
		return mTitleRight;
	}
	return null;
}
 
源代码3 项目: Dashchan   文件: DrawerLayout.java
/**
 * Enable or disable interaction with the given drawer.
 *
 * <p>This allows the application to restrict the user's ability to open or close
 * the given drawer. DrawerLayout will still respond to calls to {@link #openDrawer(int)},
 * {@link #closeDrawer(int)} and friends if a drawer is locked.</p>
 *
 * <p>Locking a drawer open or closed will implicitly open or close
 * that drawer as appropriate.</p>
 *
 * @param lockMode The new lock mode for the given drawer. One of {@link #LOCK_MODE_UNLOCKED},
 *                 {@link #LOCK_MODE_LOCKED_CLOSED} or {@link #LOCK_MODE_LOCKED_OPEN}.
 * @param edgeGravity Gravity.LEFT, RIGHT, START or END.
 *                    Expresses which drawer to change the mode for.
 *
 * @see #LOCK_MODE_UNLOCKED
 * @see #LOCK_MODE_LOCKED_CLOSED
 * @see #LOCK_MODE_LOCKED_OPEN
 */
public void setDrawerLockMode(int lockMode, int edgeGravity) {
	final int absGravity = Gravity.getAbsoluteGravity(edgeGravity, getLayoutDirection(this));

	switch (edgeGravity) {
		case Gravity.LEFT:
			mLockModeLeft = lockMode;
			break;
		case Gravity.RIGHT:
			mLockModeRight = lockMode;
			break;
		case Gravity.START:
			mLockModeStart = lockMode;
			break;
		case Gravity.END:
			mLockModeEnd = lockMode;
			break;
	}

	if (lockMode != LOCK_MODE_UNLOCKED) {
		// Cancel interaction in progress
		final ViewDragHelper helper = absGravity == Gravity.LEFT ? mLeftDragger : mRightDragger;
		helper.cancel();
	}
	switch (lockMode) {
		case LOCK_MODE_LOCKED_OPEN:
			final View toOpen = findDrawerWithGravity(absGravity);
			if (toOpen != null) {
				openDrawer(toOpen);
			}
			break;
		case LOCK_MODE_LOCKED_CLOSED:
			final View toClose = findDrawerWithGravity(absGravity);
			if (toClose != null) {
				closeDrawer(toClose);
			}
			break;
		// default: do nothing
	}
}
 
源代码4 项目: android_9.0.0_r45   文件: Toolbar.java
private int getChildHorizontalGravity(int gravity) {
    final int ld = getLayoutDirection();
    final int absGrav = Gravity.getAbsoluteGravity(gravity, ld);
    final int hGrav = absGrav & Gravity.HORIZONTAL_GRAVITY_MASK;
    switch (hGrav) {
        case Gravity.LEFT:
        case Gravity.RIGHT:
        case Gravity.CENTER_HORIZONTAL:
            return hGrav;
        default:
            return ld == LAYOUT_DIRECTION_RTL ? Gravity.RIGHT : Gravity.LEFT;
    }
}
 
源代码5 项目: AndroidFloatLabel   文件: FloatLabel.java
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void layoutChild(View child, int parentLeft, int parentTop, int parentRight, int parentBottom) {
    if (child.getVisibility() != GONE) {
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();

        final int width = child.getMeasuredWidth();
        final int height = child.getMeasuredHeight();

        int childLeft;
        final int childTop = parentTop + lp.topMargin;

        int gravity = lp.gravity;
        if (gravity == -1) {
            gravity = Gravity.TOP | Gravity.START;
        }

        final int layoutDirection;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            layoutDirection = LAYOUT_DIRECTION_LTR;
        } else {
            layoutDirection = getLayoutDirection();
        }

        final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);

        switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
            case Gravity.CENTER_HORIZONTAL:
                childLeft = parentLeft + (parentRight - parentLeft - width) / 2 + lp.leftMargin - lp.rightMargin;
                break;
            case Gravity.END:
                childLeft = parentRight - width - lp.rightMargin;
                break;
            case Gravity.START:
            default:
                childLeft = parentLeft + lp.leftMargin;
        }

        child.layout(childLeft, childTop, childLeft + width, childTop + height);
    }
}
 
源代码6 项目: Dashchan   文件: DrawerLayout.java
/**
 * @param gravity the gravity of the child to return. If specified as a
 *            relative value, it will be resolved according to the current
 *            layout direction.
 * @return the drawer with the specified gravity
 */
View findDrawerWithGravity(int gravity) {
	final int absHorizGravity = Gravity.getAbsoluteGravity(gravity, getLayoutDirection(this)) &
			Gravity.HORIZONTAL_GRAVITY_MASK;
	final int childCount = getChildCount();
	for (int i = 0; i < childCount; i++) {
		final View child = getChildAt(i);
		final int childAbsGravity = getDrawerViewAbsoluteGravity(child);
		if ((childAbsGravity & Gravity.HORIZONTAL_GRAVITY_MASK) == absHorizGravity) {
			return child;
		}
	}
	return null;
}
 
源代码7 项目: ProjectX   文件: Compat.java
static void apply(Drawable drawable, int gravity, float w, float h,
                  Rect container, RectF outRect) {
    if (drawable == null)
        return;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        gravity = Gravity.getAbsoluteGravity(gravity, drawable.getLayoutDirection());
        apply(gravity, w, h, container, 0, 0, outRect);
    } else
        apply(gravity, w, h, container, 0, 0, outRect);
}
 
源代码8 项目: barterli_android   文件: FullWidthDrawerLayout.java
boolean isChildADrawerView(final View child) {
    final int gravity = ((LayoutParams) child.getLayoutParams()).gravity;
    final int absGravity = Gravity
                    .getAbsoluteGravity(gravity, GravityCompat.getAbsoluteGravity(gravity, ViewCompat
                                    .getLayoutDirection(child)));
    return (absGravity & (Gravity.LEFT | Gravity.RIGHT)) != 0;
}
 
源代码9 项目: android_9.0.0_r45   文件: Toast.java
public void handleShow(IBinder windowToken) {
    if (localLOGV) Log.v(TAG, "HANDLE SHOW: " + this + " mView=" + mView
            + " mNextView=" + mNextView);
    // If a cancel/hide is pending - no need to show - at this point
    // the window token is already invalid and no need to do any work.
    if (mHandler.hasMessages(CANCEL) || mHandler.hasMessages(HIDE)) {
        return;
    }
    if (mView != mNextView) {
        // remove the old view if necessary
        handleHide();
        mView = mNextView;
        Context context = mView.getContext().getApplicationContext();
        String packageName = mView.getContext().getOpPackageName();
        if (context == null) {
            context = mView.getContext();
        }
        mWM = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        // We can resolve the Gravity here by using the Locale for getting
        // the layout direction
        final Configuration config = mView.getContext().getResources().getConfiguration();
        final int gravity = Gravity.getAbsoluteGravity(mGravity, config.getLayoutDirection());
        mParams.gravity = gravity;
        if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) {
            mParams.horizontalWeight = 1.0f;
        }
        if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) {
            mParams.verticalWeight = 1.0f;
        }
        mParams.x = mX;
        mParams.y = mY;
        mParams.verticalMargin = mVerticalMargin;
        mParams.horizontalMargin = mHorizontalMargin;
        mParams.packageName = packageName;
        mParams.hideTimeoutMilliseconds = mDuration ==
            Toast.LENGTH_LONG ? LONG_DURATION_TIMEOUT : SHORT_DURATION_TIMEOUT;
        mParams.token = windowToken;
        if (mView.getParent() != null) {
            if (localLOGV) Log.v(TAG, "REMOVE! " + mView + " in " + this);
            mWM.removeView(mView);
        }
        if (localLOGV) Log.v(TAG, "ADD! " + mView + " in " + this);
        // Since the notification manager service cancels the token right
        // after it notifies us to cancel the toast there is an inherent
        // race and we may attempt to add a window after the token has been
        // invalidated. Let us hedge against that.
        try {
            mWM.addView(mView, mParams);
            trySendAccessibilityEvent();
        } catch (WindowManager.BadTokenException e) {
            /* ignore */
        }
    }
}
 
源代码10 项目: guideshow   文件: GravityCompatJellybeanMr1.java
public static int getAbsoluteGravity(int gravity, int layoutDirection) {
    return Gravity.getAbsoluteGravity(gravity, layoutDirection);
}
 
源代码11 项目: android_9.0.0_r45   文件: Spinner.java
/**
 * Creates and positions all views for this Spinner.
 *
 * @param delta Change in the selected position. +1 means selection is moving to the right,
 * so views are scrolling to the left. -1 means selection is moving to the left.
 */
@Override
void layout(int delta, boolean animate) {
    int childrenLeft = mSpinnerPadding.left;
    int childrenWidth = mRight - mLeft - mSpinnerPadding.left - mSpinnerPadding.right;

    if (mDataChanged) {
        handleDataChanged();
    }

    // Handle the empty set by removing all views
    if (mItemCount == 0) {
        resetList();
        return;
    }

    if (mNextSelectedPosition >= 0) {
        setSelectedPositionInt(mNextSelectedPosition);
    }

    recycleAllViews();

    // Clear out old views
    removeAllViewsInLayout();

    // Make selected view and position it
    mFirstPosition = mSelectedPosition;

    if (mAdapter != null) {
        View sel = makeView(mSelectedPosition, true);
        int width = sel.getMeasuredWidth();
        int selectedOffset = childrenLeft;
        final int layoutDirection = getLayoutDirection();
        final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection);
        switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
            case Gravity.CENTER_HORIZONTAL:
                selectedOffset = childrenLeft + (childrenWidth / 2) - (width / 2);
                break;
            case Gravity.RIGHT:
                selectedOffset = childrenLeft + childrenWidth - width;
                break;
        }
        sel.offsetLeftAndRight(selectedOffset);
    }

    // Flush any cached views that did not get reused above
    mRecycler.clear();

    invalidate();

    checkSelectionChanged();

    mDataChanged = false;
    mNeedSync = false;
    setNextSelectedPositionInt(mSelectedPosition);
}
 
源代码12 项目: android_9.0.0_r45   文件: TableRow.java
/**
 * {@inheritDoc}
 */
@Override
void measureChildBeforeLayout(View child, int childIndex,
        int widthMeasureSpec, int totalWidth,
        int heightMeasureSpec, int totalHeight) {
    if (mConstrainedColumnWidths != null) {
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();

        int measureMode = MeasureSpec.EXACTLY;
        int columnWidth = 0;

        final int span = lp.span;
        final int[] constrainedColumnWidths = mConstrainedColumnWidths;
        for (int i = 0; i < span; i++) {
            columnWidth += constrainedColumnWidths[childIndex + i];
        }

        final int gravity = lp.gravity;
        final boolean isHorizontalGravity = Gravity.isHorizontal(gravity);

        if (isHorizontalGravity) {
            measureMode = MeasureSpec.AT_MOST;
        }

        // no need to care about padding here,
        // ViewGroup.getChildMeasureSpec() would get rid of it anyway
        // because of the EXACTLY measure spec we use
        int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
                Math.max(0, columnWidth - lp.leftMargin - lp.rightMargin), measureMode
        );
        int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
                mPaddingTop + mPaddingBottom + lp.topMargin +
                lp .bottomMargin + totalHeight, lp.height);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);

        if (isHorizontalGravity) {
            final int childWidth = child.getMeasuredWidth();
            lp.mOffset[LayoutParams.LOCATION_NEXT] = columnWidth - childWidth;

            final int layoutDirection = getLayoutDirection();
            final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
            switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
                case Gravity.LEFT:
                    // don't offset on X axis
                    break;
                case Gravity.RIGHT:
                    lp.mOffset[LayoutParams.LOCATION] = lp.mOffset[LayoutParams.LOCATION_NEXT];
                    break;
                case Gravity.CENTER_HORIZONTAL:
                    lp.mOffset[LayoutParams.LOCATION] = lp.mOffset[LayoutParams.LOCATION_NEXT] / 2;
                    break;
            }
        } else {
            lp.mOffset[LayoutParams.LOCATION] = lp.mOffset[LayoutParams.LOCATION_NEXT] = 0;
        }
    } else {
        // fail silently when column widths are not available
        super.measureChildBeforeLayout(child, childIndex, widthMeasureSpec,
                totalWidth, heightMeasureSpec, totalHeight);
    }
}
 
源代码13 项目: android_9.0.0_r45   文件: FrameLayout.java
void layoutChildren(int left, int top, int right, int bottom, boolean forceLeftGravity) {
    final int count = getChildCount();

    final int parentLeft = getPaddingLeftWithForeground();
    final int parentRight = right - left - getPaddingRightWithForeground();

    final int parentTop = getPaddingTopWithForeground();
    final int parentBottom = bottom - top - getPaddingBottomWithForeground();

    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();

            final int width = child.getMeasuredWidth();
            final int height = child.getMeasuredHeight();

            int childLeft;
            int childTop;

            int gravity = lp.gravity;
            if (gravity == -1) {
                gravity = DEFAULT_CHILD_GRAVITY;
            }

            final int layoutDirection = getLayoutDirection();
            final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
            final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;

            switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
                case Gravity.CENTER_HORIZONTAL:
                    childLeft = parentLeft + (parentRight - parentLeft - width) / 2 +
                    lp.leftMargin - lp.rightMargin;
                    break;
                case Gravity.RIGHT:
                    if (!forceLeftGravity) {
                        childLeft = parentRight - width - lp.rightMargin;
                        break;
                    }
                case Gravity.LEFT:
                default:
                    childLeft = parentLeft + lp.leftMargin;
            }

            switch (verticalGravity) {
                case Gravity.TOP:
                    childTop = parentTop + lp.topMargin;
                    break;
                case Gravity.CENTER_VERTICAL:
                    childTop = parentTop + (parentBottom - parentTop - height) / 2 +
                    lp.topMargin - lp.bottomMargin;
                    break;
                case Gravity.BOTTOM:
                    childTop = parentBottom - height - lp.bottomMargin;
                    break;
                default:
                    childTop = parentTop + lp.topMargin;
            }

            child.layout(childLeft, childTop, childLeft + width, childTop + height);
        }
    }
}
 
源代码14 项目: ArcLayout   文件: ArcOrigin.java
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static int getAbsoluteOrigin(int origin, int layoutDirection) {
  return Gravity.getAbsoluteGravity(origin, layoutDirection);
}
 
源代码15 项目: styT   文件: PromptUtils.java
/**
 * Gets the absolute text alignment value based on the supplied gravity and the activities
 * layout direction.
 *
 * @param gravity The gravity to convert to absolute values
 * @return absolute layout direction
 */
@SuppressLint("RtlHardcoded")
@NonNull
public static Layout.Alignment getTextAlignment(@NonNull final Resources resources,
                                                final int gravity,
                                                @Nullable final CharSequence text)
{
    final int absoluteGravity;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
    {
        int realGravity = gravity;
        final int layoutDirection = resources.getConfiguration().getLayoutDirection();
        if (text != null && layoutDirection == View.LAYOUT_DIRECTION_RTL
                && new Bidi(text.toString(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT).isRightToLeft())
        {
            if (gravity == Gravity.START)
            {
                realGravity = Gravity.END;
            }
            else if (gravity == Gravity.END)
            {
                realGravity = Gravity.START;
            }
        }
        absoluteGravity = Gravity.getAbsoluteGravity(realGravity, layoutDirection);
    }
    else
    {
        if ((gravity & Gravity.START) == Gravity.START)
        {
            absoluteGravity = Gravity.LEFT;
        }
        else if ((gravity & Gravity.END) == Gravity.END)
        {
            absoluteGravity = Gravity.RIGHT;
        }
        else
        {
            absoluteGravity = gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
        }
    }
    final Layout.Alignment alignment;
    switch (absoluteGravity)
    {
        case Gravity.RIGHT:
            alignment = Layout.Alignment.ALIGN_OPPOSITE;
            break;
        case Gravity.CENTER_HORIZONTAL:
            alignment = Layout.Alignment.ALIGN_CENTER;
            break;
        default:
            alignment = Layout.Alignment.ALIGN_NORMAL;
            break;
    }
    return alignment;
}
 
源代码16 项目: libcommon   文件: TwoPanelViewGroup.java
/**
 * 指定した子Viewの#layoutを呼び出す
 * @param child
 * @param changed
 * @param left
 * @param top
 * @param right
 * @param bottom
 */
@SuppressLint("NewApi")
private void callChildLayout(final View child, final boolean changed, final int left, final int top, final int right, final int bottom) {
	final LayoutParams lp = (LayoutParams) child.getLayoutParams();

	final int width = child.getMeasuredWidth();
	final int height = child.getMeasuredHeight();

	int childLeft;
	int childTop;

	int gravity = lp.gravity;
	if (gravity == -1) {
		gravity = DEFAULT_CHILD_GRAVITY;
	}

	final int layoutDirection = BuildCheck.isAndroid4_2() ? getLayoutDirection() : 0;
	final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
	final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;

	switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
	case Gravity.CENTER_HORIZONTAL:
		childLeft = left + (right - left - width) / 2 +
			lp.leftMargin - lp.rightMargin;
		break;
	case Gravity.RIGHT:
		childLeft = right - width - lp.rightMargin;
		break;
	case Gravity.LEFT:
	default:
		childLeft = left + lp.leftMargin;
	}

	switch (verticalGravity) {
	case Gravity.TOP:
		childTop = top + lp.topMargin;
		break;
	case Gravity.CENTER_VERTICAL:
		childTop = top + (bottom - top - height) / 2 +
			lp.topMargin - lp.bottomMargin;
		break;
	case Gravity.BOTTOM:
		childTop = bottom - height - lp.bottomMargin;
		break;
	default:
		childTop = top + lp.topMargin;
	}

	child.layout(childLeft, childTop, childLeft + width, childTop + height);
}
 
public static int getAbsoluteGravity(int gravity, int layoutDirection) {
    return Gravity.getAbsoluteGravity(gravity, layoutDirection);
}
 
@Override
public void onDrawState(EmptyStateRecyclerView rv, Canvas canvas) {
    final int width = rv.getMeasuredWidth();
    final int height = rv.getMeasuredHeight();
    configureTextLayouts(width);

    // Account for vertical text gravity
    final int verticalGravity = textGravity&Gravity.VERTICAL_GRAVITY_MASK;
    float dy;
    switch (verticalGravity) {
        case Gravity.CENTER_VERTICAL:
            dy = (height >> 1) - ((int)getFullTextHeight() >> 1);
            break;
        case Gravity.BOTTOM:
            dy = height - getFullTextHeight();
            break;
        default:
        case Gravity.TOP:
            dy = 0;
            break;
    }
    dy += getPaddingTop();

    final int horizontalGravity = Gravity.getAbsoluteGravity(textGravity,
            ViewCompat.getLayoutDirection(rv))&Gravity.HORIZONTAL_GRAVITY_MASK;

    // Draw the title text
    canvas.save();
    canvas.translate(
            getDx(width, horizontalGravity, titlePaint, titleLayout),
            dy);
    this.titleLayout.draw(canvas);
    canvas.restore();

    // Add spacing for under the text with the title spacing
    dy += titleLayout.getHeight() + titleSpacing;

    // Draw the subtitle text under the title text
    canvas.save();
    canvas.translate(
            getDx(width, horizontalGravity, subtitlePaint, subtitleLayout),
            dy);
    this.subtitleLayout.draw(canvas);
    canvas.restore();
}
 
源代码19 项目: adt-leanback-support   文件: ScaleFrameLayout.java
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    final int count = getChildCount();

    final int parentLeft, parentRight;
    if (mLayoutScaleX != 1f) {
        final float pivotX = getPivotX();
        parentLeft = getPaddingLeft() + (int)(pivotX - pivotX / mLayoutScaleX + 0.5f);
        parentRight = (int)(pivotX + (right - left - pivotX) / mLayoutScaleX + 0.5f)
                - getPaddingRight();
    } else {
        parentLeft = getPaddingLeft();
        parentRight = right - left - getPaddingRight();
    }

    final int parentTop, parentBottom;
    if (mLayoutScaleY != 1f) {
        final float pivotY = getPivotY();
        parentTop = getPaddingTop() + (int)(pivotY - pivotY / mLayoutScaleY + 0.5f);
        parentBottom = (int)(pivotY + (bottom - top - pivotY) / mLayoutScaleY + 0.5f)
                - getPaddingBottom();
    } else {
        parentTop = getPaddingTop();
        parentBottom = bottom - top - getPaddingBottom();
    }

    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != GONE) {
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();

            final int width = child.getMeasuredWidth();
            final int height = child.getMeasuredHeight();

            int childLeft;
            int childTop;

            int gravity = lp.gravity;
            if (gravity == -1) {
                gravity = DEFAULT_CHILD_GRAVITY;
            }

            final int layoutDirection = getLayoutDirection();
            final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
            final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;

            switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
                case Gravity.CENTER_HORIZONTAL:
                    childLeft = parentLeft + (parentRight - parentLeft - width) / 2 +
                            lp.leftMargin - lp.rightMargin;
                    break;
                case Gravity.RIGHT:
                    childLeft = parentRight - width - lp.rightMargin;
                    break;
                case Gravity.LEFT:
                default:
                    childLeft = parentLeft + lp.leftMargin;
            }

            switch (verticalGravity) {
                case Gravity.TOP:
                    childTop = parentTop + lp.topMargin;
                    break;
                case Gravity.CENTER_VERTICAL:
                    childTop = parentTop + (parentBottom - parentTop - height) / 2 +
                            lp.topMargin - lp.bottomMargin;
                    break;
                case Gravity.BOTTOM:
                    childTop = parentBottom - height - lp.bottomMargin;
                    break;
                default:
                    childTop = parentTop + lp.topMargin;
            }

            child.layout(childLeft, childTop, childLeft + width, childTop + height);
        }
    }
}
 
源代码20 项目: MaterialTapTargetPrompt   文件: PromptUtils.java
/**
 * Gets the absolute text alignment value based on the supplied gravity and the activities
 * layout direction.
 *
 * @param gravity The gravity to convert to absolute values
 * @return absolute layout direction
 */
@SuppressLint("RtlHardcoded")
@NonNull
public static Layout.Alignment getTextAlignment(@NonNull final Resources resources,
                                                final int gravity,
                                                @Nullable final CharSequence text)
{
    final int absoluteGravity;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
    {
        int realGravity = gravity;
        final int layoutDirection = resources.getConfiguration().getLayoutDirection();
        if (text != null && layoutDirection == View.LAYOUT_DIRECTION_RTL
                && new Bidi(text.toString(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT).isRightToLeft())
        {
            if (gravity == Gravity.START)
            {
                realGravity = Gravity.END;
            }
            else if (gravity == Gravity.END)
            {
                realGravity = Gravity.START;
            }
        }
        absoluteGravity = Gravity.getAbsoluteGravity(realGravity, layoutDirection);
    }
    else
    {
        if ((gravity & Gravity.START) == Gravity.START)
        {
            absoluteGravity = Gravity.LEFT;
        }
        else if ((gravity & Gravity.END) == Gravity.END)
        {
            absoluteGravity = Gravity.RIGHT;
        }
        else
        {
            absoluteGravity = gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
        }
    }
    final Layout.Alignment alignment;
    switch (absoluteGravity)
    {
        case Gravity.RIGHT:
            alignment = Layout.Alignment.ALIGN_OPPOSITE;
            break;
        case Gravity.CENTER_HORIZONTAL:
            alignment = Layout.Alignment.ALIGN_CENTER;
            break;
        default:
            alignment = Layout.Alignment.ALIGN_NORMAL;
            break;
    }
    return alignment;
}