Java Code Examples for android.view.Gravity#VERTICAL_GRAVITY_MASK

The following examples show how to use android.view.Gravity#VERTICAL_GRAVITY_MASK . 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: Label.java    From Carbon with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    ensureLayout();

    int saveCount = canvas.save();
    if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.CENTER_VERTICAL) {
        canvas.translate(getPaddingLeft(), (getHeight() - getPaddingTop() - getPaddingBottom() - layout.getHeight()) / 2.0f + getPaddingTop());
    } else if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
        canvas.translate(getPaddingLeft(), getHeight() - getPaddingBottom() - layout.getHeight());
    } else {
        canvas.translate(getPaddingLeft(), getPaddingTop());
    }
    if (textColor != null)
        paint.setColor(textColor.getColorForState(getDrawableState(), textColor.getDefaultColor()));
    layout.draw(canvas);
    canvas.restoreToCount(saveCount);
}
 
Example 2
Source File: ForegroundImageView.java    From ProjectX with Apache License 2.0 6 votes vote down vote up
/**
 * Describes how the foreground is positioned. Defaults to START and TOP.
 *
 * @param gravity see {@link android.view.Gravity}
 * @see #getForegroundGravity()
 */
public void setForegroundGravity(int gravity) {
    if (isForegroundEnable()) {
        super.setForegroundGravity(gravity);
        return;
    }
    if (mForegroundInfo == null)
        mForegroundInfo = new ForegroundInfo();
    if (mForegroundInfo.mGravity != gravity) {
        if ((gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0)
            gravity |= Gravity.START;
        if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == 0)
            gravity |= Gravity.TOP;
        mForegroundInfo.mGravity = gravity;
        requestLayout();
    }
}
 
Example 3
Source File: ForegroundDelegate.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
public void setForegroundGravity(View view, int foregroundGravity) {
    if (view != null) {
        if (mForegroundGravity != foregroundGravity) {
            if ((foregroundGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) {
                foregroundGravity |= Gravity.START;
            }

            if ((foregroundGravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) {
                foregroundGravity |= Gravity.TOP;
            }

            mForegroundGravity = foregroundGravity;

            if (mForegroundGravity == Gravity.FILL && mForeground != null) {
                Rect padding = new Rect();
                mForeground.getPadding(padding);
            }

            view.requestLayout();
        }
    }
}
 
Example 4
Source File: LinearLayout.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Describes how the child views are positioned. Defaults to GRAVITY_TOP. If
 * this layout has a VERTICAL orientation, this controls where all the child
 * views are placed if there is extra vertical space. If this layout has a
 * HORIZONTAL orientation, this controls the alignment of the children.
 *
 * @param gravity See {@link android.view.Gravity}
 *
 * @attr ref android.R.styleable#LinearLayout_gravity
 */
@android.view.RemotableViewMethod
public void setGravity(int gravity) {
    if (mGravity != gravity) {
        if ((gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) {
            gravity |= Gravity.START;
        }

        if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) {
            gravity |= Gravity.TOP;
        }

        mGravity = gravity;
        requestLayout();
    }
}
 
Example 5
Source File: Toolbar.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public boolean expandItemActionView(MenuBuilder menu, MenuItemImpl item) {
    ensureCollapseButtonView();
    if (mCollapseButtonView.getParent() != Toolbar.this) {
        addView(mCollapseButtonView);
    }
    mExpandedActionView = item.getActionView();
    mCurrentExpandedItem = item;
    if (mExpandedActionView.getParent() != Toolbar.this) {
        final LayoutParams lp = generateDefaultLayoutParams();
        lp.gravity = Gravity.START | (mButtonGravity & Gravity.VERTICAL_GRAVITY_MASK);
        lp.mViewType = LayoutParams.EXPANDED;
        mExpandedActionView.setLayoutParams(lp);
        addView(mExpandedActionView);
    }

    removeChildrenForExpandedActionView();
    requestLayout();
    item.setActionViewExpanded(true);

    if (mExpandedActionView instanceof CollapsibleActionView) {
        ((CollapsibleActionView) mExpandedActionView).onActionViewExpanded();
    }

    return true;
}
 
Example 6
Source File: ViewHelper.java    From tns-core-modules-widgets with Apache License 2.0 5 votes vote down vote up
public static void setHorizontalAlignment(android.view.View view, String value) {
    ViewGroup.LayoutParams params = view.getLayoutParams();
    // Initialize if empty.
    if (params == null) {
        params = new CommonLayoutParams();
    }

    // Set margins only if params are of the correct type.
    if (params instanceof FrameLayout.LayoutParams) {
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) params;
        switch (value) {
            case "left":
                lp.gravity = Gravity.LEFT | (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK);
                break;
            case "center":
            case "middle":
                lp.gravity = Gravity.CENTER_HORIZONTAL | (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK);
                break;
            case "right":
                lp.gravity = Gravity.RIGHT | (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK);
                break;
            case "stretch":
                lp.gravity = Gravity.FILL_HORIZONTAL | (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK);
                break;
        }
        view.setLayoutParams(params);
    }
}
 
Example 7
Source File: FrameLayoutCompat.java    From Mover with Apache License 2.0 5 votes vote down vote up
/**
 * Describes how the foreground is positioned. Defaults to START and TOP.
 *
 * @param foregroundGravity See {@link android.view.Gravity}
 *
 * @see #getForegroundGravity()
 *
 * @attr ref android.R.styleable#FrameLayout_foregroundGravity
 */
public void setForegroundGravity(int foregroundGravity) {
    if (mForegroundGravity != foregroundGravity) {
        if ((foregroundGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) {
            foregroundGravity |= Gravity.START;
        }

        if ((foregroundGravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) {
            foregroundGravity |= Gravity.TOP;
        }

        mForegroundGravity = foregroundGravity;


        if (mForegroundGravity == Gravity.FILL && mForeground != null) {
            Rect padding = new Rect();
            if (mForeground.getPadding(padding)) {
                mForegroundPaddingLeft = padding.left;
                mForegroundPaddingTop = padding.top;
                mForegroundPaddingRight = padding.right;
                mForegroundPaddingBottom = padding.bottom;
            }
        } else {
            mForegroundPaddingLeft = 0;
            mForegroundPaddingTop = 0;
            mForegroundPaddingRight = 0;
            mForegroundPaddingBottom = 0;
        }

        requestLayout();
    }
}
 
Example 8
Source File: HoloCircularProgressBar.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * Compute insets.
 * 
 * <pre>
 *  ______________________
 * |_________dx/2_________|
 * |......| /'''''\|......|
 * |-dx/2-|| View ||-dx/2-|
 * |______| \_____/|______|
 * |________ dx/2_________|
 * </pre>
 * 
 * @param dx
 *            the dx the horizontal unfilled space
 * @param dy
 *            the dy the horizontal unfilled space
 */
@SuppressLint("NewApi")
private void computeInsets(final int dx, final int dy) {
	final int layoutDirection;
	int absoluteGravity = mGravity;
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
		layoutDirection = getLayoutDirection();
		absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection);
	}

	switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
	case Gravity.LEFT:
		mHorizontalInset = 0;
		break;
	case Gravity.RIGHT:
		mHorizontalInset = dx;
		break;
	case Gravity.CENTER_HORIZONTAL:
	default:
		mHorizontalInset = dx / 2;
		break;
	}
	switch (absoluteGravity & Gravity.VERTICAL_GRAVITY_MASK) {
	case Gravity.TOP:
		mVerticalInset = 0;
		break;
	case Gravity.BOTTOM:
		mVerticalInset = dy;
		break;
	case Gravity.CENTER_VERTICAL:
	default:
		mVerticalInset = dy / 2;
		break;
	}
}
 
Example 9
Source File: TextResize.java    From animation-samples with Apache License 2.0 5 votes vote down vote up
public SwitchBitmapDrawable(TextView view, int gravity,
                            Bitmap startBitmap, float startFontSize, float startWidth,
                            Bitmap endBitmap, float endFontSize, float endWidth) {
    this.view = view;
    this.horizontalGravity = gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
    this.verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
    this.startBitmap = startBitmap;
    this.endBitmap = endBitmap;
    this.startFontSize = startFontSize;
    this.endFontSize = endFontSize;
    this.startWidth = startWidth;
    this.endWidth = endWidth;
}
 
Example 10
Source File: VerticalTextView.java    From SwipeBack with Apache License 2.0 5 votes vote down vote up
public VerticalTextView(Context context, AttributeSet attrs){
    super(context, attrs);
    final int gravity = getGravity();
    if(Gravity.isVertical(gravity) && (gravity&Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
        setGravity((gravity&Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP);
        topDown = false;
    }else
        topDown = true;
}
 
Example 11
Source File: VerticalTextView.java    From Blackbulb with GNU General Public License v3.0 5 votes vote down vote up
public VerticalTextView(Context context, AttributeSet attrs){
    super(context, attrs);
    final int gravity = getGravity();
    if (Gravity.isVertical(gravity) && (gravity&Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
        setGravity((gravity&Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP);
        topDown = false;
    } else {
        topDown = true;
    }
}
 
Example 12
Source File: VerticalButton.java    From SlideLayout with GNU General Public License v2.0 5 votes vote down vote up
public VerticalButton(Context context, AttributeSet attrs){
	super(context, attrs);
	final int gravity = getGravity();
	if(Gravity.isVertical(gravity) && (gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
		setGravity((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP);
		topDown = true;
	}
	else
		topDown = false;
}
 
Example 13
Source File: Mixture.java    From frenchtoast with Apache License 2.0 5 votes vote down vote up
@MainThread public void show() {
  assertMainThread();
  View view = toast.getView();
  if (view == null) {
    throw new IllegalStateException("Can't show a Toast with no View.");
  }

  Context context = toast.getView().getContext();

  WindowManager windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);

  // We can resolve the Gravity here by using the Locale for getting
  // the layout direction
  Configuration config = view.getContext().getResources().getConfiguration();
  int gravity = toast.getGravity();
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    gravity = Gravity.getAbsoluteGravity(gravity, config.getLayoutDirection());
  }
  params.gravity = gravity;
  if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) {
    params.horizontalWeight = 1.0f;
  }
  if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) {
    params.verticalWeight = 1.0f;
  }
  params.x = toast.getXOffset();
  params.y = toast.getYOffset();
  params.verticalMargin = toast.getVerticalMargin();
  params.horizontalMargin = toast.getHorizontalMargin();
  params.packageName = context.getPackageName();
  if (view.getParent() != null) {
    windowManager.removeView(view);
  }
  windowManager.addView(view, params);
  trySendAccessibilityEvent(view);
}
 
Example 14
Source File: VerticalTextView.java    From date_picker_converter with Apache License 2.0 5 votes vote down vote up
public VerticalTextView(Context context, AttributeSet attrs){
    super(context, attrs);
    final int gravity = getGravity();
    if (Gravity.isVertical(gravity) && (gravity&Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) {
        setGravity((gravity&Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP);
        topDown = false;
    } else {
        topDown = true;
    }
}
 
Example 15
Source File: BGABanner.java    From JD-Test with Apache License 2.0 4 votes vote down vote up
private void initView(Context context) {
    RelativeLayout pointContainerRl = new RelativeLayout(context);
    if (Build.VERSION.SDK_INT >= 16) {
        pointContainerRl.setBackground(mPointContainerBackgroundDrawable);
    } else {
        pointContainerRl.setBackgroundDrawable(mPointContainerBackgroundDrawable);
    }
    pointContainerRl.setPadding(mPointContainerLeftRightPadding, mPointTopBottomMargin, mPointContainerLeftRightPadding, mPointTopBottomMargin);
    LayoutParams pointContainerLp = new LayoutParams(RMP, RWC);
    // 处理圆点在顶部还是底部
    if ((mPointGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.TOP) {
        pointContainerLp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    } else {
        pointContainerLp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    }
    addView(pointContainerRl, pointContainerLp);


    LayoutParams indicatorLp = new LayoutParams(RWC, RWC);
    indicatorLp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    if (mIsNumberIndicator) {
        mNumberIndicatorTv = new TextView(context);
        mNumberIndicatorTv.setId(R.id.banner_indicatorId);
        mNumberIndicatorTv.setGravity(Gravity.CENTER_VERTICAL);
        mNumberIndicatorTv.setSingleLine(true);
        mNumberIndicatorTv.setEllipsize(TextUtils.TruncateAt.END);
        mNumberIndicatorTv.setTextColor(mNumberIndicatorTextColor);
        mNumberIndicatorTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, mNumberIndicatorTextSize);
        mNumberIndicatorTv.setVisibility(View.INVISIBLE);
        if (mNumberIndicatorBackground != null) {
            if (Build.VERSION.SDK_INT >= 16) {
                mNumberIndicatorTv.setBackground(mNumberIndicatorBackground);
            } else {
                mNumberIndicatorTv.setBackgroundDrawable(mNumberIndicatorBackground);
            }
        }
        pointContainerRl.addView(mNumberIndicatorTv, indicatorLp);
    } else {
        mPointRealContainerLl = new LinearLayout(context);
        mPointRealContainerLl.setId(R.id.banner_indicatorId);
        mPointRealContainerLl.setOrientation(LinearLayout.HORIZONTAL);
        mPointRealContainerLl.setGravity(Gravity.CENTER_VERTICAL);


        pointContainerRl.addView(mPointRealContainerLl, indicatorLp);
    }

    LayoutParams tipLp = new LayoutParams(RMP, RWC);
    tipLp.addRule(CENTER_VERTICAL);
    mTipTv = new TextView(context);
    mTipTv.setGravity(Gravity.CENTER_VERTICAL);
    mTipTv.setSingleLine(true);
    mTipTv.setEllipsize(TextUtils.TruncateAt.END);
    mTipTv.setTextColor(mTipTextColor);
    mTipTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTipTextSize);
    pointContainerRl.addView(mTipTv, tipLp);

    int horizontalGravity = mPointGravity & Gravity.HORIZONTAL_GRAVITY_MASK;
    // 处理圆点在左边、右边还是水平居中
    if (horizontalGravity == Gravity.LEFT) {
        indicatorLp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        tipLp.addRule(RelativeLayout.RIGHT_OF, R.id.banner_indicatorId);
        mTipTv.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
    } else if (horizontalGravity == Gravity.RIGHT) {
        indicatorLp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        tipLp.addRule(RelativeLayout.LEFT_OF, R.id.banner_indicatorId);
    } else {
        indicatorLp.addRule(RelativeLayout.CENTER_HORIZONTAL);
        tipLp.addRule(RelativeLayout.LEFT_OF, R.id.banner_indicatorId);
    }

    showPlaceholder();
}
 
Example 16
Source File: PagerTitleStrip.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
void updateTextPositions(int position, float positionOffset, boolean force) {
    if (position != mLastKnownCurrentPage) {
        updateText(position, mPager.getAdapter());
    } else if (!force && positionOffset == mLastKnownPositionOffset) {
        return;
    }

    mUpdatingPositions = true;

    final int prevWidth = mPrevText.getMeasuredWidth();
    final int currWidth = mCurrText.getMeasuredWidth();
    final int nextWidth = mNextText.getMeasuredWidth();
    final int halfCurrWidth = currWidth / 2;

    final int stripWidth = getWidth();
    final int stripHeight = getHeight();
    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    final int paddingTop = getPaddingTop();
    final int paddingBottom = getPaddingBottom();
    final int textPaddedLeft = paddingLeft + halfCurrWidth;
    final int textPaddedRight = paddingRight + halfCurrWidth;
    final int contentWidth = stripWidth - textPaddedLeft - textPaddedRight;

    float currOffset = positionOffset + 0.5f;
    if (currOffset > 1.f) {
        currOffset -= 1.f;
    }
    final int currCenter = stripWidth - textPaddedRight - (int) (contentWidth * currOffset);
    final int currLeft = currCenter - currWidth / 2;
    final int currRight = currLeft + currWidth;

    final int prevBaseline = mPrevText.getBaseline();
    final int currBaseline = mCurrText.getBaseline();
    final int nextBaseline = mNextText.getBaseline();
    final int maxBaseline = Math.max(Math.max(prevBaseline, currBaseline), nextBaseline);
    final int prevTopOffset = maxBaseline - prevBaseline;
    final int currTopOffset = maxBaseline - currBaseline;
    final int nextTopOffset = maxBaseline - nextBaseline;
    final int alignedPrevHeight = prevTopOffset + mPrevText.getMeasuredHeight();
    final int alignedCurrHeight = currTopOffset + mCurrText.getMeasuredHeight();
    final int alignedNextHeight = nextTopOffset + mNextText.getMeasuredHeight();
    final int maxTextHeight = Math.max(Math.max(alignedPrevHeight, alignedCurrHeight),
            alignedNextHeight);

    final int vgrav = mGravity & Gravity.VERTICAL_GRAVITY_MASK;

    int prevTop;
    int currTop;
    int nextTop;
    switch (vgrav) {
        default:
        case Gravity.TOP:
            prevTop = paddingTop + prevTopOffset;
            currTop = paddingTop + currTopOffset;
            nextTop = paddingTop + nextTopOffset;
            break;
        case Gravity.CENTER_VERTICAL:
            final int paddedHeight = stripHeight - paddingTop - paddingBottom;
            final int centeredTop = (paddedHeight - maxTextHeight) / 2;
            prevTop = centeredTop + prevTopOffset;
            currTop = centeredTop + currTopOffset;
            nextTop = centeredTop + nextTopOffset;
            break;
        case Gravity.BOTTOM:
            final int bottomGravTop = stripHeight - paddingBottom - maxTextHeight;
            prevTop = bottomGravTop + prevTopOffset;
            currTop = bottomGravTop + currTopOffset;
            nextTop = bottomGravTop + nextTopOffset;
            break;
    }

    mCurrText.layout(currLeft, currTop, currRight,
            currTop + mCurrText.getMeasuredHeight());

    final int prevLeft = Math.min(paddingLeft, currLeft - mScaledTextSpacing - prevWidth);
    mPrevText.layout(prevLeft, prevTop, prevLeft + prevWidth,
            prevTop + mPrevText.getMeasuredHeight());

    final int nextLeft = Math.max(stripWidth - paddingRight - nextWidth,
            currRight + mScaledTextSpacing);
    mNextText.layout(nextLeft, nextTop, nextLeft + nextWidth,
            nextTop + mNextText.getMeasuredHeight());

    mLastKnownPositionOffset = positionOffset;
    mUpdatingPositions = false;
}
 
Example 17
Source File: CheckedTextView.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    final Drawable checkMarkDrawable = mCheckMarkDrawable;
    if (checkMarkDrawable != null) {
        final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
        final int height = checkMarkDrawable.getIntrinsicHeight();

        int y = 0;

        switch (verticalGravity) {
            case Gravity.BOTTOM:
                y = getHeight() - height;
                break;
            case Gravity.CENTER_VERTICAL:
                y = (getHeight() - height) / 2;
                break;
        }

        final boolean checkMarkAtStart = isCheckMarkAtStart();
        final int width = getWidth();
        final int top = y;
        final int bottom = top + height;
        final int left;
        final int right;
        if (checkMarkAtStart) {
            left = mBasePadding;
            right = left + mCheckMarkWidth;
        } else {
            right = width - mBasePadding;
            left = right - mCheckMarkWidth;
        }
        checkMarkDrawable.setBounds(mScrollX + left, top, mScrollX + right, bottom);
        checkMarkDrawable.draw(canvas);

        final Drawable background = getBackground();
        if (background != null) {
            background.setHotspotBounds(mScrollX + left, top, mScrollX + right, bottom);
        }
    }
}
 
Example 18
Source File: CommonLayoutParams.java    From tns-core-modules-widgets with Apache License 2.0 4 votes vote down vote up
private static int getMeasureSpec(View view, int parentMeasureSpec, boolean horizontal) {

        int parentLength = MeasureSpec.getSize(parentMeasureSpec);
        int parentSpecMode = MeasureSpec.getMode(parentMeasureSpec);

        CommonLayoutParams lp = (CommonLayoutParams) view.getLayoutParams();
        final int margins = horizontal ? lp.leftMargin + lp.rightMargin : lp.topMargin + lp.bottomMargin;

        int resultSize = 0;
        int resultMode = MeasureSpec.UNSPECIFIED;

        int measureLength = Math.max(0, parentLength - margins);
        int childLength = horizontal ? lp.width : lp.height;

        // We want a specific size... let be it.
        if (childLength >= 0) {
            if (parentSpecMode != MeasureSpec.UNSPECIFIED) {
                resultSize = Math.min(parentLength, childLength);
            } else {
                resultSize = childLength;
            }

            resultMode = MeasureSpec.EXACTLY;
        } else {
            switch (parentSpecMode) {
                // Parent has imposed an exact size on us
                case MeasureSpec.EXACTLY:
                    resultSize = measureLength;
                    int gravity = LayoutBase.getGravity(view);
                    boolean stretched;
                    if (horizontal) {
                        final int horizontalGravity = Gravity.getAbsoluteGravity(gravity, view.getLayoutDirection()) & Gravity.HORIZONTAL_GRAVITY_MASK;
                        stretched = horizontalGravity == Gravity.FILL_HORIZONTAL;
                    } else {
                        final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
                        stretched = verticalGravity == Gravity.FILL_VERTICAL;
                    }

                    // if stretched - view wants to be our size. So be it.
                    // else - view wants to determine its own size. It can't be bigger than us.
                    resultMode = stretched ? MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
                    break;

                // Parent has imposed a maximum size on us
                case MeasureSpec.AT_MOST:
                    resultSize = measureLength;
                    resultMode = MeasureSpec.AT_MOST;
                    break;

                case MeasureSpec.UNSPECIFIED:
                    resultSize = 0;
                    resultMode = MeasureSpec.UNSPECIFIED;
                    break;
            }
        }

        return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
    }
 
Example 19
Source File: PagerTitleStrip.java    From V.FlyoutTest with MIT License 4 votes vote down vote up
void updateTextPositions(int position, float positionOffset, boolean force) {
    if (position != mLastKnownCurrentPage) {
        updateText(position, mPager.getAdapter());
    } else if (!force && positionOffset == mLastKnownPositionOffset) {
        return;
    }

    mUpdatingPositions = true;

    final int prevWidth = mPrevText.getMeasuredWidth();
    final int currWidth = mCurrText.getMeasuredWidth();
    final int nextWidth = mNextText.getMeasuredWidth();
    final int halfCurrWidth = currWidth / 2;

    final int stripWidth = getWidth();
    final int stripHeight = getHeight();
    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    final int paddingTop = getPaddingTop();
    final int paddingBottom = getPaddingBottom();
    final int textPaddedLeft = paddingLeft + halfCurrWidth;
    final int textPaddedRight = paddingRight + halfCurrWidth;
    final int contentWidth = stripWidth - textPaddedLeft - textPaddedRight;

    float currOffset = positionOffset + 0.5f;
    if (currOffset > 1.f) {
        currOffset -= 1.f;
    }
    final int currCenter = stripWidth - textPaddedRight - (int) (contentWidth * currOffset);
    final int currLeft = currCenter - currWidth / 2;
    final int currRight = currLeft + currWidth;

    final int prevBaseline = mPrevText.getBaseline();
    final int currBaseline = mCurrText.getBaseline();
    final int nextBaseline = mNextText.getBaseline();
    final int maxBaseline = Math.max(Math.max(prevBaseline, currBaseline), nextBaseline);
    final int prevTopOffset = maxBaseline - prevBaseline;
    final int currTopOffset = maxBaseline - currBaseline;
    final int nextTopOffset = maxBaseline - nextBaseline;
    final int alignedPrevHeight = prevTopOffset + mPrevText.getMeasuredHeight();
    final int alignedCurrHeight = currTopOffset + mCurrText.getMeasuredHeight();
    final int alignedNextHeight = nextTopOffset + mNextText.getMeasuredHeight();
    final int maxTextHeight = Math.max(Math.max(alignedPrevHeight, alignedCurrHeight),
            alignedNextHeight);

    final int vgrav = mGravity & Gravity.VERTICAL_GRAVITY_MASK;

    int prevTop;
    int currTop;
    int nextTop;
    switch (vgrav) {
        default:
        case Gravity.TOP:
            prevTop = paddingTop + prevTopOffset;
            currTop = paddingTop + currTopOffset;
            nextTop = paddingTop + nextTopOffset;
            break;
        case Gravity.CENTER_VERTICAL:
            final int paddedHeight = stripHeight - paddingTop - paddingBottom;
            final int centeredTop = (paddedHeight - maxTextHeight) / 2;
            prevTop = centeredTop + prevTopOffset;
            currTop = centeredTop + currTopOffset;
            nextTop = centeredTop + nextTopOffset;
            break;
        case Gravity.BOTTOM:
            final int bottomGravTop = stripHeight - paddingBottom - maxTextHeight;
            prevTop = bottomGravTop + prevTopOffset;
            currTop = bottomGravTop + currTopOffset;
            nextTop = bottomGravTop + nextTopOffset;
            break;
    }

    mCurrText.layout(currLeft, currTop, currRight,
            currTop + mCurrText.getMeasuredHeight());

    final int prevLeft = Math.min(paddingLeft, currLeft - mScaledTextSpacing - prevWidth);
    mPrevText.layout(prevLeft, prevTop, prevLeft + prevWidth,
            prevTop + mPrevText.getMeasuredHeight());

    final int nextLeft = Math.max(stripWidth - paddingRight - nextWidth,
            currRight + mScaledTextSpacing);
    mNextText.layout(nextLeft, nextTop, nextLeft + nextWidth,
            nextTop + mNextText.getMeasuredHeight());

    mLastKnownPositionOffset = positionOffset;
    mUpdatingPositions = false;
}
 
Example 20
Source File: ToggleButton.java    From ToggleButtons with MIT License 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
	// If there's text of any sort resort to CompoundButton#onDraw
	if (getText() != null && getText().length() > 0 ||
		getTextOff() != null && getTextOff().length() > 0 ||
		getTextOff() != null && getTextOn().length() > 0) {
		super.onDraw(canvas);
	}
	// Otherwise override CompoundButton#onDraw entirely to allow properly aligned image toggles
	else {
		final Drawable buttonDrawable = CompoundButtonCompat.getButtonDrawable(this);
		if (buttonDrawable != null) {
			final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
			final int horizontalGravity = getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK;
			final int drawableHeight = buttonDrawable.getIntrinsicHeight();
			final int drawableWidth = buttonDrawable.getIntrinsicWidth();

			final int top;
			switch (verticalGravity) {
				case Gravity.BOTTOM:
					top = getHeight() - drawableHeight;
					break;
				case Gravity.CENTER_VERTICAL:
					top = (getHeight() - drawableHeight) / 2;
					break;
				default:
					top = 0;
			}

			final int left;
			switch (horizontalGravity) {
				case Gravity.RIGHT:
				case Gravity.END:
					left = getWidth() - drawableWidth;
					break;
				case Gravity.CENTER_HORIZONTAL:
					left = (getWidth() - drawableWidth) / 2;
					break;
				default:
					left = 0;
			}

			final int bottom = top + drawableHeight;
			final int right = left + drawableWidth;

			buttonDrawable.setBounds(left, top, right, bottom);

			final Drawable background = getBackground();
			if (Build.VERSION.SDK_INT > 21 && background != null) {
				background.setHotspotBounds(left, top, right, bottom);
			}

			buttonDrawable.draw(canvas);
		}
	}
}