Example usage for android.support.v4.view GravityCompat RELATIVE_HORIZONTAL_GRAVITY_MASK

List of usage examples for android.support.v4.view GravityCompat RELATIVE_HORIZONTAL_GRAVITY_MASK

Introduction

In this page you can find the example usage for android.support.v4.view GravityCompat RELATIVE_HORIZONTAL_GRAVITY_MASK.

Prototype

int RELATIVE_HORIZONTAL_GRAVITY_MASK

To view the source code for android.support.v4.view GravityCompat RELATIVE_HORIZONTAL_GRAVITY_MASK.

Click Source Link

Usage

From source file:com.example.android.support.transition.widget.BeginDelayedUsage.java

private void toggle() {
    TransitionManager.beginDelayedTransition(mRoot);
    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mButton.getLayoutParams();
    if ((params.gravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) == GravityCompat.END) {
        params.gravity = params.gravity ^ GravityCompat.END | GravityCompat.START;
    } else {//from  w w  w . j a  v a 2  s .  c o  m
        params.gravity = params.gravity ^ GravityCompat.START | GravityCompat.END;
    }
    mButton.setLayoutParams(params);
}

From source file:demo.design.TextInputLayout.java

private void setEditText(EditText editText) {
    // If we already have an EditText, throw an exception
    if (mEditText != null) {
        throw new IllegalArgumentException("We already have an EditText, can only have one");
    }//from  ww w  .j a va2s. c o  m

    if (!(editText instanceof TextInputEditText)) {
        Log.i(LOG_TAG,
                "EditText added is not a TextInputEditText. Please switch to using that" + " class instead.");
    }

    mEditText = editText;

    // Use the EditText's typeface, and it's text size for our expanded text
    mCollapsingTextHelper.setTypefaces(mEditText.getTypeface());
    mCollapsingTextHelper.setExpandedTextSize(mEditText.getTextSize());

    final int editTextGravity = mEditText.getGravity();
    mCollapsingTextHelper.setCollapsedTextGravity(
            Gravity.TOP | (editTextGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK));
    mCollapsingTextHelper.setExpandedTextGravity(editTextGravity);

    // Add a TextWatcher so that we know when the text input has changed
    mEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            updateLabelState(true);
            if (mCounterEnabled) {
                updateCounter(s.length());
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    });

    // Use the EditText's hint colors if we don't have one set
    if (mDefaultTextColor == null) {
        mDefaultTextColor = mEditText.getHintTextColors();
    }

    // If we do not have a valid hint, try and retrieve it from the EditText, if enabled
    if (mHintEnabled && TextUtils.isEmpty(mHint)) {
        setHint(mEditText.getHint());
        // Clear the EditText's hint as we will display it ourselves
        mEditText.setHint(null);
    }

    if (mCounterView != null) {
        updateCounter(mEditText.getText().length());
    }

    if (mIndicatorArea != null) {
        adjustIndicatorPadding();
    }

    // Update the label visibility with no animation
    updateLabelState(false);
}

From source file:ru.shmakinv.android.material.widget.CollapsingTextHelper.java

@SuppressLint("RtlHardcoded")
private void calculateBaseOffsets() {
    final float currentTextSize = mCurrentTextSize;

    // We then calculate the collapsed text size, using the same logic
    calculateUsingTextSize(mCollapsedTextSize);
    float width = mTextToDraw != null ? mTextPaint.measureText(mTextToDraw, 0, mTextToDraw.length()) : 0;
    final int collapsedAbsGravity = GravityCompat.getAbsoluteGravity(mCollapsedTextGravity,
            mIsRtl ? ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR);
    switch (collapsedAbsGravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.BOTTOM:
        mCollapsedDrawY = mCollapsedBounds.bottom;
        break;/*from   www . j  a  va 2  s  .co  m*/
    case Gravity.TOP:
        mCollapsedDrawY = mCollapsedBounds.top - mTextPaint.ascent();
        break;
    case Gravity.CENTER_VERTICAL:
    default:
        float textHeight = mTextPaint.descent() - mTextPaint.ascent();
        float textOffset = (textHeight / 2) - mTextPaint.descent();
        mCollapsedDrawY = mCollapsedBounds.centerY() + textOffset;
        break;
    }
    switch (collapsedAbsGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
    case Gravity.CENTER_HORIZONTAL:
        mCollapsedDrawX = mCollapsedBounds.centerX() - (width / 2);
        break;
    case Gravity.RIGHT:
        mCollapsedDrawX = mCollapsedBounds.right - width;
        break;
    case Gravity.LEFT:
    default:
        mCollapsedDrawX = mCollapsedBounds.left;
        break;
    }

    calculateUsingTextSize(mExpandedTextSize);
    width = mTextToDraw != null ? mTextPaint.measureText(mTextToDraw, 0, mTextToDraw.length()) : 0;
    final int expandedAbsGravity = GravityCompat.getAbsoluteGravity(mExpandedTextGravity,
            mIsRtl ? ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR);
    switch (expandedAbsGravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.BOTTOM:
        mExpandedDrawY = mExpandedBounds.bottom;
        break;
    case Gravity.TOP:
        mExpandedDrawY = mExpandedBounds.top - mTextPaint.ascent();
        break;
    case Gravity.CENTER_VERTICAL:
    default:
        float textHeight = mTextPaint.descent() - mTextPaint.ascent();
        float textOffset = (textHeight / 2) - mTextPaint.descent();
        mExpandedDrawY = mExpandedBounds.centerY() + textOffset;
        break;
    }
    switch (expandedAbsGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
    case Gravity.CENTER_HORIZONTAL:
        mExpandedDrawX = mExpandedBounds.centerX() - (width / 2);
        break;
    case Gravity.RIGHT:
        mExpandedDrawX = mExpandedBounds.right - width;
        break;
    case Gravity.LEFT:
    default:
        mExpandedDrawX = mExpandedBounds.left;
        break;
    }

    // The bounds have changed so we need to clear the texture
    clearTexture();
    // Now reset the text size back to the original
    setInterpolatedTextSize(currentTextSize);
}

From source file:android.support.design.widget.CollapsingTextHelper.java

private void calculateBaseOffsets() {
    final float currentTextSize = mCurrentTextSize;

    // We then calculate the collapsed text size, using the same logic
    calculateUsingTextSize(mCollapsedTextSize);
    float width = mTextToDraw != null ? mTextPaint.measureText(mTextToDraw, 0, mTextToDraw.length()) : 0;
    final int collapsedAbsGravity = GravityCompat.getAbsoluteGravity(mCollapsedTextGravity,
            mIsRtl ? ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR);
    switch (collapsedAbsGravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.BOTTOM:
        mCollapsedDrawY = mCollapsedBounds.bottom;
        break;//from w  w w .  j  a  va 2s.c  o m
    case Gravity.TOP:
        mCollapsedDrawY = mCollapsedBounds.top - mTextPaint.ascent();
        break;
    case Gravity.CENTER_VERTICAL:
    default:
        float textHeight = mTextPaint.descent() - mTextPaint.ascent();
        float textOffset = (textHeight / 2) - mTextPaint.descent();
        mCollapsedDrawY = mCollapsedBounds.centerY() + textOffset;
        break;
    }
    switch (collapsedAbsGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
    case Gravity.CENTER_HORIZONTAL:
        mCollapsedDrawX = mCollapsedBounds.centerX() - (width / 2);
        break;
    case Gravity.RIGHT:
        mCollapsedDrawX = mCollapsedBounds.right - width;
        break;
    case Gravity.LEFT:
    default:
        mCollapsedDrawX = mCollapsedBounds.left;
        break;
    }

    calculateUsingTextSize(mExpandedTextSize);
    width = mTextToDraw != null ? mTextPaint.measureText(mTextToDraw, 0, mTextToDraw.length()) : 0;
    final int expandedAbsGravity = GravityCompat.getAbsoluteGravity(mExpandedTextGravity,
            mIsRtl ? ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR);
    switch (expandedAbsGravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.BOTTOM:
        mExpandedDrawY = mExpandedBounds.bottom;
        break;
    case Gravity.TOP:
        mExpandedDrawY = mExpandedBounds.top - mTextPaint.ascent();
        break;
    case Gravity.CENTER_VERTICAL:
    default:
        float textHeight = mTextPaint.descent() - mTextPaint.ascent();
        float textOffset = (textHeight / 2) - mTextPaint.descent();
        mExpandedDrawY = mExpandedBounds.centerY() + textOffset;
        break;
    }
    switch (expandedAbsGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
    case Gravity.CENTER_HORIZONTAL:
        mExpandedDrawX = mExpandedBounds.centerX() - (width / 2);
        break;
    case Gravity.RIGHT:
        mExpandedDrawX = mExpandedBounds.right - width;
        break;
    case Gravity.LEFT:
    default:
        mExpandedDrawX = mExpandedBounds.left;
        break;
    }

    // The bounds have changed so we need to clear the texture
    clearTexture();
    // Now reset the text size back to the original
    setInterpolatedTextSize(currentTextSize);
}

From source file:org.buffer.android.buffertextinputlayout.BufferTextInputLayout.java

private void setEditText(EditText editText) {
    // If we already have an EditText, throw an exception
    if (this.editText != null) {
        throw new IllegalArgumentException("We already have an EditText, can only have one");
    }/*from   w  ww.  j av  a 2s .c  o  m*/
    if (!(editText instanceof TextInputEditText)) {
        Log.i(LOG_TAG,
                "EditText added is not a TextInputEditText. Please switch to using that" + " class instead.");
    }
    this.editText = editText;
    final boolean hasPasswordTransformation = hasPasswordTransformation();
    // Use the EditText's typeface, and it's text size for our expanded text
    if (!hasPasswordTransformation) {
        // We don't want a monospace font just because we have a password field
        collapsingTextHelper.setTypefaces(this.editText.getTypeface());
    }
    collapsingTextHelper.setExpandedTextSize(this.editText.getTextSize());
    final int editTextGravity = this.editText.getGravity();
    collapsingTextHelper.setCollapsedTextGravity(
            Gravity.TOP | (editTextGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK));
    collapsingTextHelper.setExpandedTextGravity(editTextGravity);
    // Add a TextWatcher so that we know when the text input has changed
    this.editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            setCounterVisible(counterVisible
                    && s.length() >= (getCounterMaxLength() - charactersRemainingUntilCounterDisplay));
            updateLabelState(true);
            if (counterEnabled) {
                updateCounter(s.length());
            }
            if (textInputListener != null)
                textInputListener.onTextChanged(s.toString());
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    });
    // Use the EditText's hint colors if we don't have one set
    if (defaultTextColor == null) {
        defaultTextColor = this.editText.getHintTextColors();
    }
    // If we do not have a valid hint, try and retrieve it from the EditText, if enabled
    if (isHintEnabled && TextUtils.isEmpty(hint)) {
        setHint(this.editText.getHint());
        // Clear the EditText's hint as we will display it ourselves
        this.editText.setHint(null);
    }
    if (counterView != null) {
        updateCounter(this.editText.getText().length());
    }
    if (indicatorArea != null) {
        adjustIndicatorPadding();
    }
    updatePasswordToggleView();
    // Update the label visibility with no animation
    updateLabelState(false);
}

From source file:org.buffer.android.buffertextinputlayout.util.CollapsingTextHelper.java

private void calculateBaseOffsets() {
    final float currentTextSize = mCurrentTextSize;
    // We then calculate the collapsed text size, using the same logic
    calculateUsingTextSize(mCollapsedTextSize);
    float width = mTextToDraw != null ? mTextPaint.measureText(mTextToDraw, 0, mTextToDraw.length()) : 0;
    final int collapsedAbsGravity = GravityCompat.getAbsoluteGravity(mCollapsedTextGravity,
            mIsRtl ? ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR);
    switch (collapsedAbsGravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.BOTTOM:
        mCollapsedDrawY = mCollapsedBounds.bottom;
        break;/* ww w. j av a  2s. com*/
    case Gravity.TOP:
        mCollapsedDrawY = mCollapsedBounds.top - mTextPaint.ascent();
        break;
    case Gravity.CENTER_VERTICAL:
    default:
        float textHeight = mTextPaint.descent() - mTextPaint.ascent();
        float textOffset = (textHeight / 2) - mTextPaint.descent();
        mCollapsedDrawY = mCollapsedBounds.centerY() + textOffset;
        break;
    }
    switch (collapsedAbsGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
    case Gravity.CENTER_HORIZONTAL:
        mCollapsedDrawX = mCollapsedBounds.centerX() - (width / 2);
        break;
    case Gravity.RIGHT:
        mCollapsedDrawX = mCollapsedBounds.right - width;
        break;
    case Gravity.LEFT:
    default:
        mCollapsedDrawX = mCollapsedBounds.left;
        break;
    }
    calculateUsingTextSize(mExpandedTextSize);
    width = mTextToDraw != null ? mTextPaint.measureText(mTextToDraw, 0, mTextToDraw.length()) : 0;
    final int expandedAbsGravity = GravityCompat.getAbsoluteGravity(mExpandedTextGravity,
            mIsRtl ? ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR);
    switch (expandedAbsGravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.BOTTOM:
        mExpandedDrawY = mExpandedBounds.bottom;
        break;
    case Gravity.TOP:
        mExpandedDrawY = mExpandedBounds.top - mTextPaint.ascent();
        break;
    case Gravity.CENTER_VERTICAL:
    default:
        float textHeight = mTextPaint.descent() - mTextPaint.ascent();
        float textOffset = (textHeight / 2) - mTextPaint.descent();
        mExpandedDrawY = mExpandedBounds.centerY() + textOffset;
        break;
    }
    switch (expandedAbsGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
    case Gravity.CENTER_HORIZONTAL:
        mExpandedDrawX = mExpandedBounds.centerX() - (width / 2);
        break;
    case Gravity.RIGHT:
        mExpandedDrawX = mExpandedBounds.right - width;
        break;
    case Gravity.LEFT:
    default:
        mExpandedDrawX = mExpandedBounds.left;
        break;
    }
    // The bounds have changed so we need to clear the texture
    clearTexture();
    // Now reset the text size back to the original
    setInterpolatedTextSize(currentTextSize);
}

From source file:com.aries.ui.view.title.util.CollapsingTextHelper.java

private void calculateBaseOffsets() {
    final float currentTextSize = mCurrentTextSize;

    // We then calculate the collapsed text size, using the same logic
    calculateUsingTextSize(mCollapsedTextSize);
    float width = mTextToDraw != null ? mTextPaint.measureText(mTextToDraw, 0, mTextToDraw.length()) : 0;
    final int collapsedAbsGravity = GravityCompat.getAbsoluteGravity(mCollapsedTextGravity,
            mIsRtl ? ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR);
    switch (collapsedAbsGravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.BOTTOM:
        mCollapsedDrawY = mCollapsedBounds.bottom - mTextPaint.descent();
        break;/*from  w  w w  .j a  va 2s  .co m*/
    case Gravity.TOP:
        mCollapsedDrawY = mCollapsedBounds.top - mTextPaint.ascent();
        break;
    case Gravity.CENTER_VERTICAL:
    default:
        float textHeight = mTextPaint.descent() - mTextPaint.ascent();
        float textOffset = (textHeight / 2) - mTextPaint.descent();
        mCollapsedDrawY = mCollapsedBounds.centerY() + textOffset;
        break;
    }
    switch (collapsedAbsGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
    case Gravity.CENTER_HORIZONTAL:
        mCollapsedDrawX = mCollapsedBounds.centerX() - (width / 2);
        break;
    case Gravity.RIGHT:
        mCollapsedDrawX = mCollapsedBounds.right - width;
        break;
    case Gravity.LEFT:
    default:
        mCollapsedDrawX = mCollapsedBounds.left;
        break;
    }

    calculateUsingTextSize(mExpandedTextSize);
    width = mTextToDraw != null ? mTextPaint.measureText(mTextToDraw, 0, mTextToDraw.length()) : 0;
    final int expandedAbsGravity = GravityCompat.getAbsoluteGravity(mExpandedTextGravity,
            mIsRtl ? ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR);
    switch (expandedAbsGravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.BOTTOM:
        mExpandedDrawY = mExpandedBounds.bottom - mTextPaint.descent();
        break;
    case Gravity.TOP:
        mExpandedDrawY = mExpandedBounds.top - mTextPaint.ascent();
        break;
    case Gravity.CENTER_VERTICAL:
    default:
        float textHeight = mTextPaint.descent() - mTextPaint.ascent();
        float textOffset = (textHeight / 2) - mTextPaint.descent();
        mExpandedDrawY = mExpandedBounds.centerY() + textOffset;
        break;
    }
    switch (expandedAbsGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
    case Gravity.CENTER_HORIZONTAL:
        mExpandedDrawX = mExpandedBounds.centerX() - (width / 2);
        break;
    case Gravity.RIGHT:
        mExpandedDrawX = mExpandedBounds.right - width;
        break;
    case Gravity.LEFT:
    default:
        mExpandedDrawX = mExpandedBounds.left;
        break;
    }

    // The bounds have changed so we need to clear the texture
    clearTexture();
    // Now reset the text size back to the original
    setInterpolatedTextSize(currentTextSize);
}

From source file:net.opacapp.multilinecollapsingtoolbar.CollapsingTextHelper.java

private void calculateBaseOffsets() {
    final float currentTextSize = mCurrentTextSize;
    // We then calculate the collapsed text size, using the same logic
    calculateUsingTextSize(mCollapsedTextSize);

    // BEGIN MODIFICATION: set mTextToDrawCollapsed and calculate width using it
    mTextToDrawCollapsed = mTextToDraw;//  w w  w. j  a v a 2s.  co m
    float width = mTextToDrawCollapsed != null
            ? mTextPaint.measureText(mTextToDrawCollapsed, 0, mTextToDrawCollapsed.length())
            : 0;
    // END MODIFICATION

    final int collapsedAbsGravity = GravityCompat.getAbsoluteGravity(mCollapsedTextGravity,
            mIsRtl ? ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR);

    // BEGIN MODIFICATION: calculate height and Y position using mTextLayout
    float textHeight = mTextLayout != null ? mTextLayout.getHeight() : 0;

    switch (collapsedAbsGravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.BOTTOM:
        mCollapsedDrawY = mCollapsedBounds.bottom - textHeight;
        break;
    case Gravity.TOP:
        mCollapsedDrawY = mCollapsedBounds.top;
        break;
    case Gravity.CENTER_VERTICAL:
    default:
        float textOffset = (textHeight / 2);
        mCollapsedDrawY = mCollapsedBounds.centerY() - textOffset;
        break;
    }
    // END MODIFICATION

    switch (collapsedAbsGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
    case Gravity.CENTER_HORIZONTAL:
        mCollapsedDrawX = mCollapsedBounds.centerX() - (width / 2);
        break;
    case Gravity.RIGHT:
        mCollapsedDrawX = mCollapsedBounds.right - width;
        break;
    case Gravity.LEFT:
    default:
        mCollapsedDrawX = mCollapsedBounds.left;
        break;
    }

    calculateUsingTextSize(mExpandedTextSize);

    // BEGIN MODIFICATION: calculate width using mTextLayout based on first line and store that padding
    width = mTextLayout != null ? mTextLayout.getLineWidth(0) : 0;
    mExpandedFirstLineDrawX = mTextLayout != null ? mTextLayout.getLineLeft(0) : 0;
    // END MODIFICATION

    final int expandedAbsGravity = GravityCompat.getAbsoluteGravity(mExpandedTextGravity,
            mIsRtl ? ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR);

    // BEGIN MODIFICATION: calculate height and Y position using mTextLayout
    textHeight = mTextLayout != null ? mTextLayout.getHeight() : 0;
    switch (expandedAbsGravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.BOTTOM:
        mExpandedDrawY = mExpandedBounds.bottom - textHeight;
        break;
    case Gravity.TOP:
        mExpandedDrawY = mExpandedBounds.top;
        break;
    case Gravity.CENTER_VERTICAL:
    default:
        float textOffset = (textHeight / 2);
        mExpandedDrawY = mExpandedBounds.centerY() - textOffset;
        break;
    }
    // END MODIFICATION

    switch (expandedAbsGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
    case Gravity.CENTER_HORIZONTAL:
        mExpandedDrawX = mExpandedBounds.centerX() - (width / 2);
        break;
    case Gravity.RIGHT:
        mExpandedDrawX = mExpandedBounds.right - width;
        break;
    case Gravity.LEFT:
    default:
        mExpandedDrawX = mExpandedBounds.left;
        break;
    }

    // The bounds have changed so we need to clear the texture
    clearTexture();
    // Now reset the text size back to the original
    setInterpolatedTextSize(currentTextSize);
}

From source file:net.grobas.widget.AutoLinearLayout.java

public void setHorizontalGravity(int horizontalGravity) {
    final int gravity = horizontalGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK;
    if ((mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) != gravity) {
        mGravity = (mGravity & ~GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) | gravity;
        requestLayout();/*  w w w.j a  va  2  s .  co m*/
    }
}

From source file:net.opacapp.multilinecollapsingtoolbar.CollapsingTextHelper.java

private void calculateUsingTextSize(final float textSize) {
    if (mText == null)
        return;/*  www .j a  v a  2s  .co  m*/
    final float availableWidth;
    final float newTextSize;
    boolean updateDrawText = false;
    // BEGIN MODIFICATION: Add maxLines variable
    int maxLines;
    // END MODIFICATION
    if (isClose(textSize, mCollapsedTextSize)) {
        availableWidth = mCollapsedBounds.width();
        newTextSize = mCollapsedTextSize;
        mScale = 1f;
        if (mCurrentTypeface != mCollapsedTypeface) {
            mCurrentTypeface = mCollapsedTypeface;
            updateDrawText = true;
        }
        // BEGIN MODIFICATION: Set maxLines variable
        maxLines = 1;
        // END MODIFICATION
    } else {
        availableWidth = mExpandedBounds.width();
        newTextSize = mExpandedTextSize;
        if (mCurrentTypeface != mExpandedTypeface) {
            mCurrentTypeface = mExpandedTypeface;
            updateDrawText = true;
        }
        if (isClose(textSize, mExpandedTextSize)) {
            // If we're close to the expanded text size, snap to it and use a scale of 1
            mScale = 1f;
        } else {
            // Else, we'll scale down from the expanded text size
            mScale = textSize / mExpandedTextSize;
        }
        // BEGIN MODIFICATION: Set maxLines variable
        maxLines = this.maxLines;
        // END MODIFICATION
    }
    if (availableWidth > 0) {
        updateDrawText = (mCurrentTextSize != newTextSize) || mBoundsChanged || updateDrawText;
        mCurrentTextSize = newTextSize;
        mBoundsChanged = false;
    }
    if (mTextToDraw == null || updateDrawText) {
        mTextPaint.setTextSize(mCurrentTextSize);
        mTextPaint.setTypeface(mCurrentTypeface);

        // BEGIN MODIFICATION: Text layout creation and text truncation
        StaticLayout layout = new StaticLayout(mText, mTextPaint, (int) availableWidth,
                Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
        CharSequence truncatedText;
        if (layout.getLineCount() > maxLines) {
            int lastLine = maxLines - 1;
            CharSequence textBefore = lastLine > 0 ? mText.subSequence(0, layout.getLineEnd(lastLine - 1)) : "";
            CharSequence lineText = mText.subSequence(layout.getLineStart(lastLine),
                    layout.getLineEnd(lastLine));
            // if last char in line is space, move it behind the ellipsis
            CharSequence lineEnd = "";
            if (lineText.charAt(lineText.length() - 1) == ' ') {
                lineEnd = lineText.subSequence(lineText.length() - 1, lineText.length());
                lineText = lineText.subSequence(0, lineText.length() - 1);
            }
            // insert ellipsis character
            lineText = TextUtils.concat(lineText, "\u2026", lineEnd);
            // if the text is too long, truncate it
            CharSequence truncatedLineText = TextUtils.ellipsize(lineText, mTextPaint, availableWidth,
                    TextUtils.TruncateAt.END);
            truncatedText = TextUtils.concat(textBefore, truncatedLineText);

        } else {
            truncatedText = mText;
        }
        if (!TextUtils.equals(truncatedText, mTextToDraw)) {
            mTextToDraw = truncatedText;
            mIsRtl = calculateIsRtl(mTextToDraw);
        }

        final Layout.Alignment alignment;

        // Don't rectify gravity for RTL languages, Layout.Alignment does it already.
        switch (mExpandedTextGravity & GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
        case Gravity.CENTER_HORIZONTAL:
            alignment = Layout.Alignment.ALIGN_CENTER;
            break;
        case Gravity.RIGHT:
        case Gravity.END:
            alignment = Layout.Alignment.ALIGN_OPPOSITE;
            break;
        case Gravity.LEFT:
        case Gravity.START:
        default:
            alignment = Layout.Alignment.ALIGN_NORMAL;
            break;
        }

        mTextLayout = new StaticLayout(mTextToDraw, mTextPaint, (int) availableWidth, alignment, 1, 0, false);
        // END MODIFICATION
    }
}