Example usage for android.graphics Canvas drawText

List of usage examples for android.graphics Canvas drawText

Introduction

In this page you can find the example usage for android.graphics Canvas drawText.

Prototype

public void drawText(@NonNull CharSequence text, int start, int end, float x, float y, @NonNull Paint paint) 

Source Link

Document

Draw the specified range of text, specified by start/end, with its origin at (x,y), in the specified Paint.

Usage

From source file:com.yek.keyboard.keyboards.views.CandidateView.java

/**
 * If the canvas is null, then only touch calculations are performed to pick
 * the target candidate.//from   ww w. j  a v a 2 s.co  m
 */
@Override
protected void onDraw(Canvas canvas) {
    if (canvas != null) {
        super.onDraw(canvas);
    }
    mTotalWidth = 0;

    final int height = getHeight();
    if (mBgPadding == null) {
        mBgPadding = new Rect(0, 0, 0, 0);
        if (getBackground() != null) {
            getBackground().getPadding(mBgPadding);
        }
        mDivider.setBounds(0, 0, mDivider.getIntrinsicWidth(), mDivider.getIntrinsicHeight());
    }

    final int dividerYOffset = (height - mDivider.getMinimumHeight()) / 2;
    final int count = mSuggestions.size();
    final Rect bgPadding = mBgPadding;
    final Paint paint = mPaint;
    final int touchX = mTouchX;
    final int scrollX = getScrollX();
    final boolean scrolled = mScrolled;
    final boolean typedWordValid = mTypedWordValid;

    int x = 0;
    for (int i = 0; i < count; i++) {
        CharSequence suggestion = mSuggestions.get(i);
        if (suggestion == null)
            continue;
        final int wordLength = suggestion.length();

        paint.setColor(mColorNormal);
        if (mHaveMinimalSuggestion && ((i == 1 && !typedWordValid) || (i == 0 && typedWordValid))) {
            paint.setTypeface(Typeface.DEFAULT_BOLD);
            paint.setColor(mColorRecommended);
            // existsAutoCompletion = true;
        } else if (i != 0 || (wordLength == 1 && count > 1)) {
            // HACK: even if i == 0, we use mColorOther when this
            // suggestion's length is 1 and
            // there are multiple suggestions, such as the default
            // punctuation list.
            paint.setColor(mColorOther);
        }

        // now that we set the typeFace, we can measure
        int wordWidth;
        if ((wordWidth = mWordWidth[i]) == 0) {
            float textWidth = paint.measureText(suggestion, 0, wordLength);
            // wordWidth = Math.max(0, (int) textWidth + X_GAP * 2);
            wordWidth = (int) (textWidth + mXGap * 2);
            mWordWidth[i] = wordWidth;
        }

        mWordX[i] = x;

        if (touchX != OUT_OF_BOUNDS_X_CORD && !scrolled && touchX + scrollX >= x
                && touchX + scrollX < x + wordWidth) {
            if (canvas != null && !mShowingAddToDictionary) {
                canvas.translate(x, 0);
                mSelectionHighlight.setBounds(0, bgPadding.top, wordWidth, height);
                mSelectionHighlight.draw(canvas);
                canvas.translate(-x, 0);
            }
            mSelectedString = suggestion;
            mSelectedIndex = i;
        }

        if (canvas != null) {
            // (+)This is the trick to get RTL/LTR text correct
            if (AnyApplication.getConfig().workaround_alwaysUseDrawText()) {
                final int y = (int) (height + paint.getTextSize() - paint.descent()) / 2;
                canvas.drawText(suggestion, 0, wordLength, x + wordWidth / 2, y, paint);
            } else {
                final int y = (int) (height - paint.getTextSize() + paint.descent()) / 2;
                // no matter what: StaticLayout
                float textX = x + (wordWidth / 2) - mXGap;
                float textY = y - bgPadding.bottom - bgPadding.top;

                canvas.translate(textX, textY);
                mTextPaint.setTypeface(paint.getTypeface());
                mTextPaint.setColor(paint.getColor());

                StaticLayout suggestionText = new StaticLayout(suggestion, mTextPaint, wordWidth,
                        Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
                suggestionText.draw(canvas);

                canvas.translate(-textX, -textY);
            }
            // (-)
            paint.setColor(mColorOther);
            canvas.translate(x + wordWidth, 0);
            // Draw a divider unless it's after the hint
            //or the last suggested word
            if (count > 1 && (!mShowingAddToDictionary) && i != (count - 1)) {
                canvas.translate(0, dividerYOffset);
                mDivider.draw(canvas);
                canvas.translate(0, -dividerYOffset);
            }
            canvas.translate(-x - wordWidth, 0);
        }
        paint.setTypeface(Typeface.DEFAULT);
        x += wordWidth;
    }
    mTotalWidth = x;
    if (mTargetScrollX != scrollX) {
        scrollToTarget();
    }
}

From source file:com.anysoftkeyboard.keyboards.views.CandidateView.java

/**
 * If the canvas is null, then only touch calculations are performed to pick
 * the target candidate./*from   ww w  .j a v a2 s .c o  m*/
 */
@Override
protected void onDraw(Canvas canvas) {
    if (canvas != null) {
        super.onDraw(canvas);
    }
    mTotalWidth = 0;

    final int height = getHeight();
    if (mBgPadding == null) {
        mBgPadding = new Rect(0, 0, 0, 0);
        if (getBackground() != null) {
            getBackground().getPadding(mBgPadding);
        }
        mDivider.setBounds(0, 0, mDivider.getIntrinsicWidth(), mDivider.getIntrinsicHeight());
    }

    final int dividerYOffset = (height - mDivider.getMinimumHeight()) / 2;
    final int count = mSuggestions.size();
    final Rect bgPadding = mBgPadding;
    final Paint paint = mPaint;
    final int touchX = mTouchX;
    final int scrollX = getScrollX();
    final boolean scrolled = mScrolled;
    final boolean typedWordValid = mTypedWordValid;

    int x = 0;
    for (int i = 0; i < count; i++) {
        CharSequence suggestion = mSuggestions.get(i);
        if (suggestion == null)
            continue;
        final int wordLength = suggestion.length();

        paint.setColor(mColorNormal);
        if (mHaveMinimalSuggestion && ((i == 1 && !typedWordValid) || (i == 0 && typedWordValid))) {
            paint.setTypeface(Typeface.DEFAULT_BOLD);
            paint.setColor(mColorRecommended);
            // existsAutoCompletion = true;
        } else if (i != 0 || (wordLength == 1 && count > 1)) {
            // HACK: even if i == 0, we use mColorOther when this
            // suggestion's length is 1 and
            // there are multiple suggestions, such as the default
            // punctuation list.
            paint.setColor(mColorOther);
        }

        // now that we set the typeFace, we can measure
        int wordWidth;
        if ((wordWidth = mWordWidth[i]) == 0) {
            float textWidth = paint.measureText(suggestion, 0, wordLength);
            // wordWidth = Math.max(0, (int) textWidth + X_GAP * 2);
            wordWidth = (int) (textWidth + mHorizontalGap * 2);
            mWordWidth[i] = wordWidth;
        }

        mWordX[i] = x;

        if (touchX != OUT_OF_BOUNDS_X_CORD && !scrolled && touchX + scrollX >= x
                && touchX + scrollX < x + wordWidth) {
            if (canvas != null && !mShowingAddToDictionary) {
                canvas.translate(x, 0);
                mSelectionHighlight.setBounds(0, bgPadding.top, wordWidth, height);
                mSelectionHighlight.draw(canvas);
                canvas.translate(-x, 0);
            }
            mSelectedString = suggestion;
            mSelectedIndex = i;
        }

        if (canvas != null) {
            // (+)This is the trick to get RTL/LTR text correct
            if (AnyApplication.getConfig().workaround_alwaysUseDrawText()) {
                final int y = (int) (height + paint.getTextSize() - paint.descent()) / 2;
                canvas.drawText(suggestion, 0, wordLength, x + wordWidth / 2, y, paint);
            } else {
                final int y = (int) (height - paint.getTextSize() + paint.descent()) / 2;
                // no matter what: StaticLayout
                float textX = x + (wordWidth / 2) - mHorizontalGap;
                float textY = y - bgPadding.bottom - bgPadding.top;

                canvas.translate(textX, textY);
                mTextPaint.setTypeface(paint.getTypeface());
                mTextPaint.setColor(paint.getColor());

                StaticLayout suggestionText = new StaticLayout(suggestion, mTextPaint, wordWidth,
                        Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
                suggestionText.draw(canvas);

                canvas.translate(-textX, -textY);
            }
            // (-)
            paint.setColor(mColorOther);
            canvas.translate(x + wordWidth, 0);
            // Draw a divider unless it's after the hint
            //or the last suggested word
            if (count > 1 && (!mShowingAddToDictionary) && i != (count - 1)) {
                canvas.translate(0, dividerYOffset);
                mDivider.draw(canvas);
                canvas.translate(0, -dividerYOffset);
            }
            canvas.translate(-x - wordWidth, 0);
        }
        paint.setTypeface(Typeface.DEFAULT);
        x += wordWidth;
    }
    mTotalWidth = x;
    if (mTargetScrollX != scrollX) {
        scrollToTarget();
    }
}

From source file:cn.androidy.androiddevelopmentpatterns.interactivechart.InteractiveLineGraphView.java

/**
 * Draws the chart axes and labels onto the canvas.
 *///from  w  w w  . j  a va2  s .co  m
private void drawAxes(Canvas canvas) {
    // Computes axis stops (in terms of numerical value and position on screen)
    int i;

    computeAxisStops(mCurrentViewport.left, mCurrentViewport.right, mContentRect.width() / mMaxLabelWidth / 2,
            mXStopsBuffer);
    computeAxisStops(mCurrentViewport.top, mCurrentViewport.bottom, mContentRect.height() / mLabelHeight / 2,
            mYStopsBuffer);

    // Avoid unnecessary allocations during drawing. Re-use allocated
    // arrays and only reallocate if the number of stops grows.
    if (mAxisXPositionsBuffer.length < mXStopsBuffer.numStops) {
        mAxisXPositionsBuffer = new float[mXStopsBuffer.numStops];
    }
    if (mAxisYPositionsBuffer.length < mYStopsBuffer.numStops) {
        mAxisYPositionsBuffer = new float[mYStopsBuffer.numStops];
    }
    if (mAxisXLinesBuffer.length < mXStopsBuffer.numStops * 4) {
        mAxisXLinesBuffer = new float[mXStopsBuffer.numStops * 4];
    }
    if (mAxisYLinesBuffer.length < mYStopsBuffer.numStops * 4) {
        mAxisYLinesBuffer = new float[mYStopsBuffer.numStops * 4];
    }

    // Compute positions
    for (i = 0; i < mXStopsBuffer.numStops; i++) {
        mAxisXPositionsBuffer[i] = getDrawX(mXStopsBuffer.stops[i]);
    }
    for (i = 0; i < mYStopsBuffer.numStops; i++) {
        mAxisYPositionsBuffer[i] = getDrawY(mYStopsBuffer.stops[i]);
    }

    // Draws grid lines using drawLines (faster than individual drawLine calls)
    for (i = 0; i < mXStopsBuffer.numStops; i++) {
        mAxisXLinesBuffer[i * 4 + 0] = (float) Math.floor(mAxisXPositionsBuffer[i]);
        mAxisXLinesBuffer[i * 4 + 1] = mContentRect.top;
        mAxisXLinesBuffer[i * 4 + 2] = (float) Math.floor(mAxisXPositionsBuffer[i]);
        mAxisXLinesBuffer[i * 4 + 3] = mContentRect.bottom;
    }
    canvas.drawLines(mAxisXLinesBuffer, 0, mXStopsBuffer.numStops * 4, mGridPaint);

    for (i = 0; i < mYStopsBuffer.numStops; i++) {
        mAxisYLinesBuffer[i * 4 + 0] = mContentRect.left;
        mAxisYLinesBuffer[i * 4 + 1] = (float) Math.floor(mAxisYPositionsBuffer[i]);
        mAxisYLinesBuffer[i * 4 + 2] = mContentRect.right;
        mAxisYLinesBuffer[i * 4 + 3] = (float) Math.floor(mAxisYPositionsBuffer[i]);
    }
    canvas.drawLines(mAxisYLinesBuffer, 0, mYStopsBuffer.numStops * 4, mGridPaint);

    // Draws X labels
    int labelOffset;
    int labelLength;
    mLabelTextPaint.setTextAlign(Paint.Align.CENTER);
    for (i = 0; i < mXStopsBuffer.numStops; i++) {
        // Do not use String.format in high-performance code such as onDraw code.
        labelLength = formatFloat(mLabelBuffer, mXStopsBuffer.stops[i], mXStopsBuffer.decimals);
        labelOffset = mLabelBuffer.length - labelLength;
        canvas.drawText(mLabelBuffer, labelOffset, labelLength, mAxisXPositionsBuffer[i],
                mContentRect.bottom + mLabelHeight + mLabelSeparation, mLabelTextPaint);
    }

    // Draws Y labels
    mLabelTextPaint.setTextAlign(Paint.Align.RIGHT);
    for (i = 0; i < mYStopsBuffer.numStops; i++) {
        // Do not use String.format in high-performance code such as onDraw code.
        labelLength = formatFloat(mLabelBuffer, mYStopsBuffer.stops[i], mYStopsBuffer.decimals);
        labelOffset = mLabelBuffer.length - labelLength;
        canvas.drawText(mLabelBuffer, labelOffset, labelLength, mContentRect.left - mLabelSeparation,
                mAxisYPositionsBuffer[i] + mLabelHeight / 2, mLabelTextPaint);
    }
}

From source file:com.android.ex.chips.RecipientEditTextView.java

private Bitmap createUnselectedChip(final RecipientEntry contact, final TextPaint paint,
        final boolean leaveBlankIconSpacer) {
    // Ellipsize the text so that it takes AT MOST the entire width of the
    // autocomplete text entry area. Make sure to leave space for padding
    // on the sides.
    final int height = (int) mChipHeight;
    int iconWidth = height;
    final float[] widths = new float[1];
    paint.getTextWidths(" ", widths);
    final float availableWidth = calculateAvailableWidth();
    final String chipDisplayText = createChipDisplayText(contact);
    final CharSequence ellipsizedText = ellipsizeText(chipDisplayText, paint,
            availableWidth - iconWidth - widths[0]);
    // Make sure there is a minimum chip width so the user can ALWAYS
    // tap a chip without difficulty.
    final int width = Math.max(iconWidth * 2,
            (int) Math.floor(paint.measureText(ellipsizedText, 0, ellipsizedText.length())) + mChipPadding * 2
                    + iconWidth);/*from  w ww .j a  va 2  s  .com*/
    // Create the background of the chip.
    final Bitmap tmpBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(tmpBitmap);
    final Drawable background = getChipBackground(contact);
    if (background != null) {
        background.setBounds(0, 0, width, height);
        background.draw(canvas);
        // Don't draw photos for recipients that have been typed in OR generated on the fly.
        final long contactId = contact.getContactId();
        final boolean drawPhotos = isPhoneQuery() ? contactId != RecipientEntry.INVALID_CONTACT
                : contactId != RecipientEntry.INVALID_CONTACT && contactId != RecipientEntry.GENERATED_CONTACT
                        && !TextUtils.isEmpty(contact.getDisplayName());
        if (drawPhotos) {
            byte[] photoBytes = contact.getPhotoBytes();
            // There may not be a photo yet if anything but the first contact address
            // was selected.
            if (photoBytes == null && contact.getPhotoThumbnailUri() != null) {
                // TODO: cache this in the recipient entry?
                getAdapter().fetchPhoto(contact, contact.getPhotoThumbnailUri());
                photoBytes = contact.getPhotoBytes();
            }
            Bitmap photo;
            if (photoBytes != null)
                photo = BitmapFactory.decodeByteArray(photoBytes, 0, photoBytes.length);
            else // TODO: can the scaled down default photo be cached?
                photo = mDefaultContactPhoto;
            // Draw the photo on the left side.
            if (photo != null) {
                final RectF src = new RectF(0, 0, photo.getWidth(), photo.getHeight());
                final Rect backgroundPadding = new Rect();
                mChipBackground.getPadding(backgroundPadding);
                final RectF dst = new RectF(width - iconWidth + backgroundPadding.left,
                        0 + backgroundPadding.top, width - backgroundPadding.right,
                        height - backgroundPadding.bottom);
                final Matrix matrix = new Matrix();
                matrix.setRectToRect(src, dst, Matrix.ScaleToFit.FILL);
                canvas.drawBitmap(photo, matrix, paint);
            }
        } else if (!leaveBlankIconSpacer || isPhoneQuery())
            iconWidth = 0;
        paint.setColor(ContextCompat.getColor(getContext(), android.R.color.black));
        // Vertically center the text in the chip.
        canvas.drawText(ellipsizedText, 0, ellipsizedText.length(), mChipPadding,
                getTextYOffset((String) ellipsizedText, paint, height), paint);
    } else
        Log.w(TAG, "Unable to draw a background for the chips as it was never set");
    return tmpBitmap;
}

From source file:com.ex.english.viewpagerindicator.TitlePageIndicator.java

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

    if (mViewPager == null) {
        return;//w  w  w  . j  ava2s .c om
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    // mCurrentPage is -1 on first start and after orientation changed. If so, retrieve the correct index from viewpager.
    if (mCurrentPage == -1 && mViewPager != null)
        mCurrentPage = mViewPager.getCurrentItem();

    //Calculate views bounds
    ArrayList<Rect> bounds = calculateAllBounds(mPaintText);
    final int boundsSize = bounds.size();

    //Make sure we're on a page that still exists
    if (mCurrentPage >= boundsSize) {
        setCurrentItem(boundsSize - 1);
        return;
    }

    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    final int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;

    int page = mCurrentPage;
    float offsetPercent;
    if (mPageOffset <= 0.5) {
        offsetPercent = mPageOffset;
    } else {
        page += 1;
        offsetPercent = 1 - mPageOffset;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;

    //Verify if the current view must be clipped to the screen
    Rect curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        //Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        //Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }

    //Left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            Rect bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                //Except if there's an intersection with the right view
                Rect rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    //Right views starting from the current position
    if (mCurrentPage < countMinusOne) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            Rect bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                //Except if there's an intersection with the left view
                Rect leftBound = bounds.get(i - 1);
                //Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = (int) (leftBound.right + mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }

    //Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        //Get the title
        Rect bound = bounds.get(i);
        //Only if one side is visible
        if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) {
            final boolean currentPage = (i == page);
            final CharSequence pageTitle = getTitle(i);

            //Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);

            //Draw text as unselected
            mPaintText.setColor(mColorText);
            if (currentPage && currentSelected) {
                //Fade out/in unselected text as the selected text fades in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }
            canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                    mPaintText);

            //If we are within the selected bounds draw the selected text
            if (currentPage && currentSelected) {
                mPaintText.setColor(mColorSelected);
                mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent));
                canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                        mPaintText);
            }
        }
    }

    //Draw the footer line
    mPath.reset();
    mPath.moveTo(0, height - mFooterLineHeight / 2f);
    mPath.lineTo(width, height - mFooterLineHeight / 2f);
    mPath.close();
    canvas.drawPath(mPath, mPaintFooterLine);

    switch (mFooterIndicatorStyle) {
    case Triangle:
        mPath.reset();
        mPath.moveTo(halfWidth, height - mFooterLineHeight - mFooterIndicatorHeight);
        mPath.lineTo(halfWidth + mFooterIndicatorHeight, height - mFooterLineHeight);
        mPath.lineTo(halfWidth - mFooterIndicatorHeight, height - mFooterLineHeight);
        mPath.close();
        canvas.drawPath(mPath, mPaintFooterIndicator);
        break;

    case Underline:
        if (!currentSelected || page >= boundsSize) {
            break;
        }

        Rect underlineBounds = bounds.get(page);
        mPath.reset();
        mPath.moveTo(underlineBounds.left - mFooterIndicatorUnderlinePadding, height - mFooterLineHeight);
        mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding, height - mFooterLineHeight);
        mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding,
                height - mFooterLineHeight - mFooterIndicatorHeight);
        mPath.lineTo(underlineBounds.left - mFooterIndicatorUnderlinePadding,
                height - mFooterLineHeight - mFooterIndicatorHeight);
        mPath.close();

        mPaintFooterIndicator.setAlpha((int) (0xFF * selectedPercent));
        canvas.drawPath(mPath, mPaintFooterIndicator);
        mPaintFooterIndicator.setAlpha(0xFF);
        break;
    }
}

From source file:com.viewpagerindicator.TitlePageIndicator.java

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

    if (mViewPager == null) {
        return;/*ww  w  .  j  a  v  a2s . c  o m*/
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    // mCurrentPage is -1 on first start and after orientation changed. If so, retrieve the correct index from viewpager.
    if (mCurrentPage == -1 && mViewPager != null)
        mCurrentPage = mViewPager.getCurrentItem();

    //Calculate views bounds
    ArrayList<Rect> bounds = calculateAllBounds(mPaintText);
    final int boundsSize = bounds.size();

    //Make sure we're on a page that still exists
    if (mCurrentPage >= boundsSize) {
        setCurrentItem(boundsSize - 1);
        return;
    }

    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    final int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;

    int page = mCurrentPage;
    float offsetPercent;
    if (mPageOffset <= 0.5) {
        offsetPercent = mPageOffset;
    } else {
        page += 1;
        offsetPercent = 1 - mPageOffset;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;

    //Verify if the current view must be clipped to the screen
    Rect curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        //Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        //Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }

    //Left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            Rect bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                //Except if there's an intersection with the right view
                Rect rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    //Right views starting from the current position
    if (mCurrentPage < countMinusOne) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            Rect bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                //Except if there's an intersection with the left view
                Rect leftBound = bounds.get(i - 1);
                //Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = (int) (leftBound.right + mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }

    //Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        //Get the title
        Rect bound = bounds.get(i);
        //Only if one side is visible
        if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) {
            final boolean currentPage = (i == page);
            final CharSequence pageTitle = getTitle(i);

            //Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);

            //Draw text as unselected
            mPaintText.setColor(mColorText);
            if (currentPage && currentSelected) {
                //Fade out/in unselected text as the selected text fades in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }
            canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                    mPaintText);

            //If we are within the selected bounds draw the selected text
            if (currentPage && currentSelected) {
                mPaintText.setColor(mColorSelected);
                mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent));
                canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                        mPaintText);
            }
        }
    }

    //Draw the footer line
    mPath.reset();
    mPath.moveTo(0, height - mFooterLineHeight / 2f);
    mPath.lineTo(width, height - mFooterLineHeight / 2f);
    mPath.close();
    canvas.drawPath(mPath, mPaintFooterLine);

    switch (mFooterIndicatorStyle) {
    case Triangle:
        mPath.reset();
        mPath.moveTo(halfWidth, height - mFooterLineHeight - mFooterIndicatorHeight);
        mPath.lineTo(halfWidth + mFooterIndicatorHeight, height - mFooterLineHeight);
        mPath.lineTo(halfWidth - mFooterIndicatorHeight, height - mFooterLineHeight);
        mPath.close();
        canvas.drawPath(mPath, mPaintFooterIndicator);
        break;

    case Underline:
        if (!currentSelected || page >= boundsSize) {
            break;
        }

        Rect underlineBounds = bounds.get(page);
        mPath.reset();
        mPath.moveTo(underlineBounds.left - mFooterIndicatorUnderlinePadding, height - mFooterLineHeight);
        mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding, height - mFooterLineHeight);
        mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding,
                height - mFooterLineHeight - mFooterIndicatorHeight);
        mPath.lineTo(underlineBounds.left - mFooterIndicatorUnderlinePadding,
                height - mFooterLineHeight - mFooterIndicatorHeight);
        mPath.close();

        mPaintFooterIndicator.setAlpha((int) (0xFF * selectedPercent));
        canvas.drawPath(mPath, mPaintFooterIndicator);
        mPaintFooterIndicator.setAlpha(0xFF);
        break;
    case None:
    default:
        break; // To keep the Linter happy
    }
}

From source file:indrora.atomic.indicator.ConversationTitlePageIndicator.java

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

    if (mViewPager == null) {
        return;/*from w ww.  j av a 2 s .c om*/
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    // mCurrentPage is -1 on first start and after orientation changed. If so, retrieve the correct index from viewpager.
    if (mCurrentPage == -1 && mViewPager != null) {
        mCurrentPage = mViewPager.getCurrentItem();
    }

    //Calculate views bounds
    ArrayList<Rect> bounds = calculateAllBounds(mPaintText);
    final int boundsSize = bounds.size();

    //Make sure we're on a page that still exists
    if (mCurrentPage >= boundsSize) {
        setCurrentItem(boundsSize - 1);
        return;
    }

    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    final int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;

    int page = mCurrentPage;
    float offsetPercent;
    if (mPageOffset <= 0.5) {
        offsetPercent = mPageOffset;
    } else {
        page += 1;
        offsetPercent = 1 - mPageOffset;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;

    //Verify if the current view must be clipped to the screen
    Rect curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        //Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        //Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }

    //Left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            Rect bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                //Except if there's an intersection with the right view
                Rect rightBound = bounds.get(i + 1);
                //Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    //Right views starting from the current position
    if (mCurrentPage < countMinusOne) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            Rect bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                //Except if there's an intersection with the left view
                Rect leftBound = bounds.get(i - 1);
                //Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = (int) (leftBound.right + mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }

    //Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        //Get the title
        Rect bound = bounds.get(i);
        //Only if one side is visible
        if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) {
            final boolean currentPage = (i == page);
            final CharSequence pageTitle = getTitle(i);

            //Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);

            //Draw text as unselected
            mPaintText.setColor(currentPage ? mColorText : mStateProvider.getColorAt(i));
            if (currentPage && currentSelected) {
                //Fade out/in unselected text as the selected text fades in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }
            canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                    mPaintText);

            //If we are within the selected bounds draw the selected text
            if (currentPage && currentSelected) {
                mPaintText.setColor(mColorSelected);
                mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent));
                canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                        mPaintText);
            }
        }
    }

    //Draw the footer line
    mPath.reset();
    mPath.moveTo(0, height - mFooterLineHeight / 2f);
    mPath.lineTo(width, height - mFooterLineHeight / 2f);
    mPath.close();
    canvas.drawPath(mPath, mPaintFooterLine);

    switch (mFooterIndicatorStyle) {
    case Triangle:
        mPath.reset();
        mPath.moveTo(halfWidth, height - mFooterLineHeight - mFooterIndicatorHeight);
        mPath.lineTo(halfWidth + mFooterIndicatorHeight, height - mFooterLineHeight);
        mPath.lineTo(halfWidth - mFooterIndicatorHeight, height - mFooterLineHeight);
        mPath.close();
        canvas.drawPath(mPath, mPaintFooterIndicator);
        break;

    case Underline:
        if (!currentSelected || page >= boundsSize) {
            break;
        }

        Rect underlineBounds = bounds.get(page);
        mPath.reset();
        mPath.moveTo(underlineBounds.left - mFooterIndicatorUnderlinePadding, height - mFooterLineHeight);
        mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding, height - mFooterLineHeight);
        mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding,
                height - mFooterLineHeight - mFooterIndicatorHeight);
        mPath.lineTo(underlineBounds.left - mFooterIndicatorUnderlinePadding,
                height - mFooterLineHeight - mFooterIndicatorHeight);
        mPath.close();

        mPaintFooterIndicator.setAlpha((int) (0xFF * selectedPercent));
        canvas.drawPath(mPath, mPaintFooterIndicator);
        mPaintFooterIndicator.setAlpha(0xFF);
        break;

    case None:
        // Nothing to do here.
        break;
    }
}

From source file:me.futuretechnology.util.ui.TitlePageIndicator.java

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

    if (mViewPager == null) {
        return;/* www . ja  v a2  s .  c  o  m*/
    }

    int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }

    // mCurrentPage is -1 on first start and after orientation changed.
    // if so, retrieve the correct index from viewpager.
    if (mCurrentPage == -1 && mViewPager != null) {
        mCurrentPage = mViewPager.getCurrentItem();
    }

    // calculate views bounds
    ArrayList<Rect> bounds = calculateAllBounds(mPaintText);
    int boundsSize = bounds.size();

    // make sure we're on a page that still exists
    if (mCurrentPage >= boundsSize) {
        setCurrentItem(boundsSize - 1);
        return;
    }

    int left = getLeft();
    float leftClip = left + mClipPadding;
    int width = getWidth();
    int height = getHeight();
    // float halfWidth = width / 2f;
    int right = left + width;
    float rightClip = right - mClipPadding;

    int page = mCurrentPage;
    float offsetPercent;
    if (mPageOffset <= 0.5) {
        offsetPercent = mPageOffset;
    } else {
        page += 1;
        offsetPercent = 1 - mPageOffset;
    }
    boolean currentSelected = offsetPercent <= SELECTION_FADE_PERCENTAGE;
    boolean currentBold = offsetPercent <= BOLD_FADE_PERCENTAGE;
    float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;

    // Verify if the current view must be clipped to the screen
    Rect curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        // Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        // Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }

    // left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            Rect bound = bounds.get(i);
            // if left side is outside the screen
            if (bound.left < leftClip) {
                int w = bound.right - bound.left;
                // try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                // except if there's an intersection with the right view
                Rect rightBound = bounds.get(i + 1);
                // intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    // right views starting from the current position
    if (mCurrentPage < count - 1) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            Rect bound = bounds.get(i);
            // if right side is outside the screen
            if (bound.right > rightClip) {
                int w = bound.right - bound.left;
                // try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                // except if there's an intersection with the left view
                Rect leftBound = bounds.get(i - 1);
                // intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = (int) (leftBound.right + mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }

    // Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        // Get the title
        Rect bound = bounds.get(i);
        // Only if one side is visible
        if (bound.left > left && bound.left < right || bound.right > left && bound.right < right) {
            boolean currentPage = i == page;
            CharSequence pageTitle = getTitle(i);

            // Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);

            // Draw text as unselected
            mPaintText.setColor(mColorText);
            if (currentPage && currentSelected) {
                // Fade out/in unselected text as the selected text fades in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }

            // Except if there's an intersection with the right view
            if (i < boundsSize - 1) {
                Rect rightBound = bounds.get(i + 1);
                // Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    int w = bound.right - bound.left;
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }

            canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                    mPaintText);

            // If we are within the selected bounds draw the selected text
            if (currentPage && currentSelected) {
                mPaintText.setColor(mColorSelected);
                mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent));
                canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                        mPaintText);
            }
        }
    }

    // Draw the footer line
    if (mFooterLineHeight > 0) {
        mPath.reset();
        mPath.moveTo(0, height - mFooterLineHeight / 2f);
        mPath.lineTo(width, height - mFooterLineHeight / 2f);
        mPath.close();
        canvas.drawPath(mPath, mPaintFooterLine);
    }

    if (mFooterIndicatorHeight > 0) {
        if (!currentSelected || page >= boundsSize) {
            return;
        }

        Rect underlineBounds = bounds.get(page);
        float tempHeight = mFooterIndicatorHeight * selectedPercent
                - ((1 - selectedPercent) * mFooterIndicatorHeight) * 0.5f;

        mPath.reset();
        mPath.moveTo(underlineBounds.left - mFooterIndicatorUnderlinePadding, height - mFooterLineHeight);
        mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding, height - mFooterLineHeight);
        mPath.lineTo(underlineBounds.right + mFooterIndicatorUnderlinePadding,
                height - mFooterLineHeight - tempHeight);
        mPath.lineTo(underlineBounds.left - mFooterIndicatorUnderlinePadding,
                height - mFooterLineHeight - tempHeight);
        mPath.close();

        // mPaintFooterIndicator.setAlpha((int) (0xFF * selectedPercent));
        canvas.drawPath(mPath, mPaintFooterIndicator);
        mPaintFooterIndicator.setAlpha(0xFF);
    }
}

From source file:com.benefit.buy.library.viewpagerindicator.TitlePageIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mViewPager == null) {
        return;//from w ww . ja  v a 2s  .c  o m
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }
    // mCurrentPage is -1 on first start and after orientation changed. If so, retrieve the correct index from viewpager.
    if ((mCurrentPage == -1) && (mViewPager != null)) {
        mCurrentPage = mViewPager.getCurrentItem();
    }
    //Calculate views bounds
    ArrayList<Rect> bounds = calculateAllBounds(mPaintText);
    final int boundsSize = bounds.size();
    //Make sure we're on a page that still exists
    if (mCurrentPage >= boundsSize) {
        setCurrentItem(boundsSize - 1);
        return;
    }
    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;
    int page = mCurrentPage;
    float offsetPercent;
    if (mPageOffset <= 0.5) {
        offsetPercent = mPageOffset;
    } else {
        page += 1;
        offsetPercent = 1 - mPageOffset;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;
    //Verify if the current view must be clipped to the screen
    Rect curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        //Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        //Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }
    //Left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            Rect bound = bounds.get(i);
            //Is left side is outside the screen
            if (bound.left < leftClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                //Except if there's an intersection with the right view
                Rect rightBound = bounds.get(i + 1);
                //Intersection
                if ((bound.right + mTitlePadding) > rightBound.left) {
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    //Right views starting from the current position
    if (mCurrentPage < countMinusOne) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            Rect bound = bounds.get(i);
            //If right side is outside the screen
            if (bound.right > rightClip) {
                int w = bound.right - bound.left;
                //Try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                //Except if there's an intersection with the left view
                Rect leftBound = bounds.get(i - 1);
                //Intersection
                if ((bound.left - mTitlePadding) < leftBound.right) {
                    bound.left = (int) (leftBound.right + mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    //Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        //Get the title
        Rect bound = bounds.get(i);
        //Only if one side is visible
        if (((bound.left > left) && (bound.left < right)) || ((bound.right > left) && (bound.right < right))) {
            final boolean currentPage = (i == page);
            final CharSequence pageTitle = getTitle(i);
            //Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);
            //Draw text as unselected
            mPaintText.setColor(mColorText);
            if (currentPage && currentSelected) {
                //Fade out/in unselected text as the selected text fades in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }
            //Except if there's an intersection with the right view
            if (i < (boundsSize - 1)) {
                Rect rightBound = bounds.get(i + 1);
                //Intersection
                if ((bound.right + mTitlePadding) > rightBound.left) {
                    int w = bound.right - bound.left;
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
            canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                    mPaintText);
            //If we are within the selected bounds draw the selected text
            if (currentPage && currentSelected) {
                mPaintText.setColor(mColorSelected);
                mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent));
                canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                        mPaintText);
            }
        }
    }
    //If we want the line on the top change height to zero and invert the line height to trick the drawing code
    float footerLineHeight = mFooterLineHeight;
    float footerIndicatorLineHeight = mFooterIndicatorHeight;
    if (mLinePosition == LinePosition.Top) {
        height = 0;
        footerLineHeight = -footerLineHeight;
        footerIndicatorLineHeight = -footerIndicatorLineHeight;
    }
    //Draw the footer line
    mPath.reset();
    mPath.moveTo(0, height - (footerLineHeight / 2f));
    mPath.lineTo(width, height - (footerLineHeight / 2f));
    mPath.close();
    canvas.drawPath(mPath, mPaintFooterLine);
    float heightMinusLine = height - footerLineHeight;
    switch (mFooterIndicatorStyle) {
    case Triangle:
        mPath.reset();
        mPath.moveTo(halfWidth, heightMinusLine - footerIndicatorLineHeight);
        mPath.lineTo(halfWidth + footerIndicatorLineHeight, heightMinusLine);
        mPath.lineTo(halfWidth - footerIndicatorLineHeight, heightMinusLine);
        mPath.close();
        canvas.drawPath(mPath, mPaintFooterIndicator);
        break;
    case Underline:
        if (!currentSelected || (page >= boundsSize)) {
            break;
        }
        Rect underlineBounds = bounds.get(page);
        final float rightPlusPadding = underlineBounds.right + mFooterIndicatorUnderlinePadding;
        final float leftMinusPadding = underlineBounds.left - mFooterIndicatorUnderlinePadding;
        final float heightMinusLineMinusIndicator = heightMinusLine - footerIndicatorLineHeight;
        mPath.reset();
        mPath.moveTo(leftMinusPadding, heightMinusLine);
        mPath.lineTo(rightPlusPadding, heightMinusLine);
        mPath.lineTo(rightPlusPadding, heightMinusLineMinusIndicator);
        mPath.lineTo(leftMinusPadding, heightMinusLineMinusIndicator);
        mPath.close();
        mPaintFooterIndicator.setAlpha((int) (0xFF * selectedPercent));
        canvas.drawPath(mPath, mPaintFooterIndicator);
        mPaintFooterIndicator.setAlpha(0xFF);
        break;
    }
}

From source file:com.lovebridge.library.view.viewpagerindicator.TitlePageIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mViewPager == null) {
        return;//ww w.  j  a  v  a  2 s .  c  o m
    }
    final int count = mViewPager.getAdapter().getCount();
    if (count == 0) {
        return;
    }
    // mCurrentPage is -1 on first start and after orientation changed. If
    // so, retrieve the correct index from viewpager.
    if (mCurrentPage == -1 && mViewPager != null) {
        mCurrentPage = mViewPager.getCurrentItem();
    }
    // Calculate views bounds
    ArrayList<Rect> bounds = calculateAllBounds(mPaintText);
    final int boundsSize = bounds.size();
    // Make sure we're on a page that still exists
    if (mCurrentPage >= boundsSize) {
        setCurrentItem(boundsSize - 1);
        return;
    }
    final int countMinusOne = count - 1;
    final float halfWidth = getWidth() / 2f;
    final int left = getLeft();
    final float leftClip = left + mClipPadding;
    final int width = getWidth();
    int height = getHeight();
    final int right = left + width;
    final float rightClip = right - mClipPadding;
    int page = mCurrentPage;
    float offsetPercent;
    if (mPageOffset <= 0.5) {
        offsetPercent = mPageOffset;
    } else {
        page += 1;
        offsetPercent = 1 - mPageOffset;
    }
    final boolean currentSelected = (offsetPercent <= SELECTION_FADE_PERCENTAGE);
    final boolean currentBold = (offsetPercent <= BOLD_FADE_PERCENTAGE);
    final float selectedPercent = (SELECTION_FADE_PERCENTAGE - offsetPercent) / SELECTION_FADE_PERCENTAGE;
    // Verify if the current view must be clipped to the screen
    Rect curPageBound = bounds.get(mCurrentPage);
    float curPageWidth = curPageBound.right - curPageBound.left;
    if (curPageBound.left < leftClip) {
        // Try to clip to the screen (left side)
        clipViewOnTheLeft(curPageBound, curPageWidth, left);
    }
    if (curPageBound.right > rightClip) {
        // Try to clip to the screen (right side)
        clipViewOnTheRight(curPageBound, curPageWidth, right);
    }
    // Left views starting from the current position
    if (mCurrentPage > 0) {
        for (int i = mCurrentPage - 1; i >= 0; i--) {
            Rect bound = bounds.get(i);
            // Is left side is outside the screen
            if (bound.left < leftClip) {
                int w = bound.right - bound.left;
                // Try to clip to the screen (left side)
                clipViewOnTheLeft(bound, w, left);
                // Except if there's an intersection with the right view
                Rect rightBound = bounds.get(i + 1);
                // Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    // Right views starting from the current position
    if (mCurrentPage < countMinusOne) {
        for (int i = mCurrentPage + 1; i < count; i++) {
            Rect bound = bounds.get(i);
            // If right side is outside the screen
            if (bound.right > rightClip) {
                int w = bound.right - bound.left;
                // Try to clip to the screen (right side)
                clipViewOnTheRight(bound, w, right);
                // Except if there's an intersection with the left view
                Rect leftBound = bounds.get(i - 1);
                // Intersection
                if (bound.left - mTitlePadding < leftBound.right) {
                    bound.left = (int) (leftBound.right + mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
        }
    }
    // Now draw views
    int colorTextAlpha = mColorText >>> 24;
    for (int i = 0; i < count; i++) {
        // Get the title
        Rect bound = bounds.get(i);
        // Only if one side is visible
        if ((bound.left > left && bound.left < right) || (bound.right > left && bound.right < right)) {
            final boolean currentPage = (i == page);
            final CharSequence pageTitle = getTitle(i);
            // Only set bold if we are within bounds
            mPaintText.setFakeBoldText(currentPage && currentBold && mBoldText);
            // Draw text as unselected
            mPaintText.setColor(mColorText);
            if (currentPage && currentSelected) {
                // Fade out/in unselected text as the selected text fades
                // in/out
                mPaintText.setAlpha(colorTextAlpha - (int) (colorTextAlpha * selectedPercent));
            }
            // Except if there's an intersection with the right view
            if (i < boundsSize - 1) {
                Rect rightBound = bounds.get(i + 1);
                // Intersection
                if (bound.right + mTitlePadding > rightBound.left) {
                    int w = bound.right - bound.left;
                    bound.left = (int) (rightBound.left - w - mTitlePadding);
                    bound.right = bound.left + w;
                }
            }
            canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                    mPaintText);
            // If we are within the selected bounds draw the selected text
            if (currentPage && currentSelected) {
                mPaintText.setColor(mColorSelected);
                mPaintText.setAlpha((int) ((mColorSelected >>> 24) * selectedPercent));
                canvas.drawText(pageTitle, 0, pageTitle.length(), bound.left, bound.bottom + mTopPadding,
                        mPaintText);
            }
        }
    }
    // If we want the line on the top change height to zero and invert the
    // line height to trick the drawing code
    float footerLineHeight = mFooterLineHeight;
    float footerIndicatorLineHeight = mFooterIndicatorHeight;
    if (mLinePosition == LinePosition.Top) {
        height = 0;
        footerLineHeight = -footerLineHeight;
        footerIndicatorLineHeight = -footerIndicatorLineHeight;
    }
    // Draw the footer line
    mPath.reset();
    mPath.moveTo(0, height - footerLineHeight / 2f);
    mPath.lineTo(width, height - footerLineHeight / 2f);
    mPath.close();
    canvas.drawPath(mPath, mPaintFooterLine);
    float heightMinusLine = height - footerLineHeight;
    switch (mFooterIndicatorStyle) {
    case Triangle:
        mPath.reset();
        mPath.moveTo(halfWidth, heightMinusLine - footerIndicatorLineHeight);
        mPath.lineTo(halfWidth + footerIndicatorLineHeight, heightMinusLine);
        mPath.lineTo(halfWidth - footerIndicatorLineHeight, heightMinusLine);
        mPath.close();
        canvas.drawPath(mPath, mPaintFooterIndicator);
        break;
    case Underline:
        if (!currentSelected || page >= boundsSize) {
            break;
        }
        Rect underlineBounds = bounds.get(page);
        final float rightPlusPadding = underlineBounds.right + mFooterIndicatorUnderlinePadding;
        final float leftMinusPadding = underlineBounds.left - mFooterIndicatorUnderlinePadding;
        final float heightMinusLineMinusIndicator = heightMinusLine - footerIndicatorLineHeight;
        mPath.reset();
        mPath.moveTo(leftMinusPadding, heightMinusLine);
        mPath.lineTo(rightPlusPadding, heightMinusLine);
        mPath.lineTo(rightPlusPadding, heightMinusLineMinusIndicator);
        mPath.lineTo(leftMinusPadding, heightMinusLineMinusIndicator);
        mPath.close();
        mPaintFooterIndicator.setAlpha((int) (0xFF * selectedPercent));
        canvas.drawPath(mPath, mPaintFooterIndicator);
        mPaintFooterIndicator.setAlpha(0xFF);
        break;
    }
}