Java Code Examples for android.view.Gravity#getAbsoluteGravity()

The following examples show how to use android.view.Gravity#getAbsoluteGravity() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FloatingNavigationView.java    From Floating-Navigation-View with Apache License 2.0 6 votes vote down vote up
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));
    }
 
Example 2
Source File: DrawerLayout.java    From Dashchan with Apache License 2.0 5 votes vote down vote up
/**
 * 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
	}
}
 
Example 3
Source File: FullWidthDrawerLayout.java    From barterli_android with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 4
Source File: Compat.java    From ProjectX with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 5
Source File: DrawerLayout.java    From Dashchan with Apache License 2.0 5 votes vote down vote up
/**
 * @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;
}
 
Example 6
Source File: FloatLabel.java    From AndroidFloatLabel with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 7
Source File: Toolbar.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 8
Source File: DrawerLayout.java    From Dashchan with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 9
Source File: TableRow.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * {@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);
    }
}
 
Example 10
Source File: FrameLayout.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
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);
        }
    }
}
 
Example 11
Source File: ArcOrigin.java    From ArcLayout with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static int getAbsoluteOrigin(int origin, int layoutDirection) {
  return Gravity.getAbsoluteGravity(origin, layoutDirection);
}
 
Example 12
Source File: Spinner.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 13
Source File: PromptUtils.java    From styT with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 14
Source File: TwoPanelViewGroup.java    From libcommon with Apache License 2.0 4 votes vote down vote up
/**
 * 指定した子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);
}
 
Example 15
Source File: GravityCompatJellybeanMr1.java    From guideshow with MIT License 4 votes vote down vote up
public static int getAbsoluteGravity(int gravity, int layoutDirection) {
    return Gravity.getAbsoluteGravity(gravity, layoutDirection);
}
 
Example 16
Source File: GravityCompatJellybeanMr1.java    From adt-leanback-support with Apache License 2.0 4 votes vote down vote up
public static int getAbsoluteGravity(int gravity, int layoutDirection) {
    return Gravity.getAbsoluteGravity(gravity, layoutDirection);
}
 
Example 17
Source File: Toast.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
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 */
        }
    }
}
 
Example 18
Source File: TextStateDisplay.java    From empty-state-recyclerview with MIT License 4 votes vote down vote up
@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();
}
 
Example 19
Source File: ScaleFrameLayout.java    From adt-leanback-support with Apache License 2.0 4 votes vote down vote up
@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);
        }
    }
}
 
Example 20
Source File: PromptUtils.java    From MaterialTapTargetPrompt with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}