Example usage for android.text Layout getWidth

List of usage examples for android.text Layout getWidth

Introduction

In this page you can find the example usage for android.text Layout getWidth.

Prototype

public final int getWidth() 

Source Link

Document

Return the width of this layout.

Usage

From source file:com.facebook.litho.widget.TextSpec.java

@OnMeasure
static void onMeasure(ComponentContext context, ComponentLayout layout, int widthSpec, int heightSpec,
        Size size, @Prop(resType = ResType.STRING) CharSequence text,
        @Prop(optional = true) TruncateAt ellipsize,
        @Prop(optional = true, resType = ResType.BOOL) boolean shouldIncludeFontPadding,
        @Prop(optional = true, resType = ResType.INT) int minLines,
        @Prop(optional = true, resType = ResType.INT) int maxLines,
        @Prop(optional = true, resType = ResType.INT) int minEms,
        @Prop(optional = true, resType = ResType.INT) int maxEms,
        @Prop(optional = true, resType = ResType.DIMEN_SIZE) int minWidth,
        @Prop(optional = true, resType = ResType.DIMEN_SIZE) int maxWidth,
        @Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowRadius,
        @Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowDx,
        @Prop(optional = true, resType = ResType.DIMEN_OFFSET) float shadowDy,
        @Prop(optional = true, resType = ResType.COLOR) int shadowColor,
        @Prop(optional = true, resType = ResType.BOOL) boolean isSingleLine,
        @Prop(optional = true, resType = ResType.COLOR) int textColor,
        @Prop(optional = true) ColorStateList textColorStateList,
        @Prop(optional = true, resType = ResType.COLOR) int linkColor,
        @Prop(optional = true, resType = ResType.DIMEN_TEXT) int textSize,
        @Prop(optional = true, resType = ResType.DIMEN_OFFSET) float extraSpacing,
        @Prop(optional = true, resType = ResType.FLOAT) float spacingMultiplier,
        @Prop(optional = true) int textStyle, @Prop(optional = true) Typeface typeface,
        @Prop(optional = true) Alignment textAlignment, @Prop(optional = true) boolean glyphWarming,
        @Prop(optional = true) TextDirectionHeuristicCompat textDirection, Output<Layout> measureLayout,
        Output<Integer> measuredWidth, Output<Integer> measuredHeight) {

    if (TextUtils.isEmpty(text)) {
        measureLayout.set(null);//from ww  w  . j a va 2  s .co  m
        size.width = 0;
        size.height = 0;
        return;
    }

    Layout newLayout = createTextLayout(widthSpec, ellipsize, shouldIncludeFontPadding, maxLines, shadowRadius,
            shadowDx, shadowDy, shadowColor, isSingleLine, text, textColor, textColorStateList, linkColor,
            textSize, extraSpacing, spacingMultiplier, textStyle, typeface, textAlignment, glyphWarming,
            layout.getResolvedLayoutDirection(), minEms, maxEms, minWidth, maxWidth, textDirection);

    measureLayout.set(newLayout);

    size.width = SizeSpec.resolveSize(widthSpec, newLayout.getWidth());

    // Adjust height according to the minimum number of lines.
    int preferredHeight = LayoutMeasureUtil.getHeight(newLayout);
    final int lineCount = newLayout.getLineCount();
    if (lineCount < minLines) {
        final TextPaint paint = newLayout.getPaint();

        final int lineHeight = Math.round(paint.getFontMetricsInt(null) * spacingMultiplier + extraSpacing);
        preferredHeight += lineHeight * (minLines - lineCount);
    }

    size.height = SizeSpec.resolveSize(heightSpec, preferredHeight);

    // Some devices seem to be returning negative sizes in some cases.
    if (size.width < 0 || size.height < 0) {
        size.width = Math.max(size.width, 0);
        size.height = Math.max(size.height, 0);

        final ComponentsLogger logger = context.getLogger();
        if (logger != null) {
            final LogEvent event = logger.newEvent(EVENT_ERROR);
            event.addParam(PARAM_MESSAGE, "Text layout measured to less than 0 pixels");
            logger.log(event);
        }
    }

    measuredWidth.set(size.width);
    measuredHeight.set(size.height);
}

From source file:lewa.support.v7.widget.SwitchCompat.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    final Rect padding = mTempRect;
    final Drawable trackDrawable = mTrackDrawable;
    if (trackDrawable != null) {
        trackDrawable.getPadding(padding);
    } else {/*w  ww. ja  v a  2s  .  co  m*/
        padding.setEmpty();
    }

    final int switchTop = mSwitchTop;
    final int switchBottom = mSwitchBottom;
    final int switchInnerTop = switchTop + padding.top;
    final int switchInnerBottom = switchBottom - padding.bottom;

    final Drawable thumbDrawable = mThumbDrawable;
    if (trackDrawable != null) {
        trackDrawable.draw(canvas);
    }

    final int saveCount = canvas.save();

    if (thumbDrawable != null) {
        thumbDrawable.draw(canvas);
    }

    final Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
    if (switchText != null) {
        final int drawableState[] = getDrawableState();
        if (mTextColors != null) {
            mTextPaint.setColor(mTextColors.getColorForState(drawableState, 0));
        }
        mTextPaint.drawableState = drawableState;

        final int cX;
        if (thumbDrawable != null) {
            final Rect bounds = thumbDrawable.getBounds();
            cX = bounds.left + bounds.right;
        } else {
            cX = getWidth();
        }

        final int left = cX / 2 - switchText.getWidth() / 2;
        final int top = (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2;
        canvas.translate(left, top);
        switchText.draw(canvas);
    }

    canvas.restoreToCount(saveCount);
}

From source file:android.support.v7.widget.SwitchCompat.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    final Rect padding = mTempRect;
    final Drawable trackDrawable = mTrackDrawable;
    if (trackDrawable != null) {
        trackDrawable.getPadding(padding);
    } else {/*from w  ww .  j ava2 s.  c om*/
        padding.setEmpty();
    }

    final int switchTop = mSwitchTop;
    final int switchBottom = mSwitchBottom;
    final int switchInnerTop = switchTop + padding.top;
    final int switchInnerBottom = switchBottom - padding.bottom;

    final Drawable thumbDrawable = mThumbDrawable;
    if (trackDrawable != null) {
        if (mSplitTrack && thumbDrawable != null) {
            final Rect insets = DrawableUtils.getOpticalBounds(thumbDrawable);
            thumbDrawable.copyBounds(padding);
            padding.left += insets.left;
            padding.right -= insets.right;

            final int saveCount = canvas.save();
            canvas.clipRect(padding, Region.Op.DIFFERENCE);
            trackDrawable.draw(canvas);
            canvas.restoreToCount(saveCount);
        } else {
            trackDrawable.draw(canvas);
        }
    }

    final int saveCount = canvas.save();

    if (thumbDrawable != null) {
        thumbDrawable.draw(canvas);
    }

    final Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
    if (switchText != null) {
        final int drawableState[] = getDrawableState();
        if (mTextColors != null) {
            mTextPaint.setColor(mTextColors.getColorForState(drawableState, 0));
        }
        mTextPaint.drawableState = drawableState;

        final int cX;
        if (thumbDrawable != null) {
            final Rect bounds = thumbDrawable.getBounds();
            cX = bounds.left + bounds.right;
        } else {
            cX = getWidth();
        }

        final int left = cX / 2 - switchText.getWidth() / 2;
        final int top = (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2;
        canvas.translate(left, top);
        switchText.draw(canvas);
    }

    canvas.restoreToCount(saveCount);
}

From source file:rikka.akashitoolkit.ui.widget.IconSwitchCompat.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    final Rect padding = mTempRect;
    final Drawable trackDrawable = mTrackDrawable;
    if (trackDrawable != null) {
        trackDrawable.getPadding(padding);
    } else {/*  w w  w. j  a  v  a 2 s. c  om*/
        padding.setEmpty();
    }

    final int switchTop = mSwitchTop;
    final int switchBottom = mSwitchBottom;
    final int switchInnerTop = switchTop + padding.top;
    final int switchInnerBottom = switchBottom - padding.bottom;

    final Drawable thumbDrawable = mThumbDrawable;
    if (trackDrawable != null) {
        if (mSplitTrack && thumbDrawable != null) {
            final Rect insets = DrawableUtils.getOpticalBounds(thumbDrawable);
            thumbDrawable.copyBounds(padding);
            padding.left += insets.left;
            padding.right -= insets.right;

            final int saveCount = canvas.save();
            canvas.clipRect(padding, Region.Op.DIFFERENCE);
            trackDrawable.draw(canvas);
            canvas.restoreToCount(saveCount);
        } else {
            trackDrawable.draw(canvas);
        }
    }

    final int saveCount = canvas.save();

    if (thumbDrawable != null) {
        thumbDrawable.draw(canvas);
    }

    final Drawable iconDrawable = mIconDrawable;
    if (iconDrawable != null) {
        if (mSplitTrack) {
            final Rect insets = DrawableUtils.getOpticalBounds(iconDrawable);
            iconDrawable.copyBounds(padding);
            padding.left += insets.left;
            padding.right -= insets.right;

            //final int saveCount = canvas.save();
            canvas.clipRect(padding, Region.Op.DIFFERENCE);
            iconDrawable.draw(canvas);
            canvas.restoreToCount(saveCount);
        } else {
            iconDrawable.draw(canvas);
        }
    }

    final Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
    if (switchText != null) {
        final int drawableState[] = getDrawableState();
        if (mTextColors != null) {
            mTextPaint.setColor(mTextColors.getColorForState(drawableState, 0));
        }
        mTextPaint.drawableState = drawableState;

        final int cX;
        if (thumbDrawable != null) {
            final Rect bounds = thumbDrawable.getBounds();
            cX = bounds.left + bounds.right;
        } else {
            cX = getWidth();
        }

        final int left = cX / 2 - switchText.getWidth() / 2;
        final int top = (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2;
        canvas.translate(left, top);
        switchText.draw(canvas);
    }

    canvas.restoreToCount(saveCount);
}

From source file:com.mixiaoxiao.support.widget.SmoothSwitch.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (!isEnabled()) {//wangbin added this line
        if (mThumbDrawable != null) {
            mThumbDrawable.setColor(getDisableColor(isChecked() ? mThumbColorOn : mThumbColorOff));
        }//from  w  w w . j  a  va 2 s .  c  o  m
        if (mTrackDrawable != null) {
            mTrackDrawable.setColor(getDisableColor(isChecked() ? mTrackColorOn : mTrackColorOff));
        }
    }
    final Rect padding = mTempRect;
    final Drawable trackDrawable = mTrackDrawable;
    if (trackDrawable != null) {
        trackDrawable.getPadding(padding);
    } else {
        padding.setEmpty();
    }

    final int switchTop = mSwitchTop;
    final int switchBottom = mSwitchBottom;
    final int switchInnerTop = switchTop + padding.top;
    final int switchInnerBottom = switchBottom - padding.bottom;

    final Drawable thumbDrawable = mThumbDrawable;
    if (trackDrawable != null) {
        trackDrawable.draw(canvas);
    }

    final int saveCount = canvas.save();

    if (thumbDrawable != null) {
        thumbDrawable.draw(canvas);
    }

    final Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
    if (switchText != null) {
        final int drawableState[] = getDrawableState();
        if (mTextColors != null) {
            mTextPaint.setColor(mTextColors.getColorForState(drawableState, 0));
        }
        mTextPaint.drawableState = drawableState;

        final int cX;
        if (thumbDrawable != null) {
            final Rect bounds = thumbDrawable.getBounds();
            cX = bounds.left + bounds.right;
        } else {
            cX = getWidth();
        }

        final int left = cX / 2 - switchText.getWidth() / 2;
        final int top = (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2;
        canvas.translate(left, top);
        switchText.draw(canvas);
    }

    canvas.restoreToCount(saveCount);
}